Skip to content

Commit 07baa35

Browse files
Set line info for @client macro as call site (#981)
1 parent a2da061 commit 07baa35

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ julia = "1.6"
3333
BufferedStreams = "e1450e63-4bb3-523b-b2a4-4ffa8c0fd77d"
3434
Deno_jll = "04572ae6-984a-583e-9378-9577a1c2574d"
3535
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
36+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
3637
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
3738
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3839
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
3940

4041
[targets]
41-
test = ["BufferedStreams", "Deno_jll", "Distributed", "JSON", "Test", "Unitful"]
42+
test = ["BufferedStreams", "Deno_jll", "Distributed", "InteractiveUtils", "JSON", "Test", "Unitful"]

src/HTTP.jl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ function request(stack::Base.Callable, method, url, h=Header[], b=nobody, q=noth
422422
return stack(string(method), request_uri(url, query), mkheaders(headers), body; kw...)
423423
end
424424

425+
macro remove_linenums!(expr)
426+
return esc(Base.remove_linenums!(expr))
427+
end
428+
425429
"""
426430
HTTP.@client requestlayers
427431
HTTP.@client requestlayers streamlayers
@@ -456,16 +460,18 @@ hard-codes the value of the `retry` and `redirect` keyword arguments. When we pa
456460
effectively over-writing the default and any user-provided keyword arguments for `retry` or `redirect`.
457461
"""
458462
macro client(requestlayers, streamlayers=[])
459-
esc(quote
460-
get(a...; kw...) = request("GET", a...; kw...)
461-
put(a...; kw...) = request("PUT", a...; kw...)
462-
post(a...; kw...) = request("POST", a...; kw...)
463-
patch(a...; kw...) = request("PATCH", a...; kw...)
464-
head(a...; kw...) = request("HEAD", a...; kw...)
465-
delete(a...; kw...) = request("DELETE", a...; kw...)
466-
open(f, a...; kw...) = request(a...; iofunction=f, kw...)
467-
request(method, url, h=HTTP.Header[], b=HTTP.nobody; headers=h, body=b, query=nothing, kw...)::HTTP.Response =
463+
return @remove_linenums! esc(quote
464+
get(a...; kw...) = ($__source__; request("GET", a...; kw...))
465+
put(a...; kw...) = ($__source__; request("PUT", a...; kw...))
466+
post(a...; kw...) = ($__source__; request("POST", a...; kw...))
467+
patch(a...; kw...) = ($__source__; request("PATCH", a...; kw...))
468+
head(a...; kw...) = ($__source__; request("HEAD", a...; kw...))
469+
delete(a...; kw...) = ($__source__; request("DELETE", a...; kw...))
470+
open(f, a...; kw...) = ($__source__; request(a...; iofunction=f, kw...))
471+
function request(method, url, h=HTTP.Header[], b=HTTP.nobody; headers=h, body=b, query=nothing, kw...)::HTTP.Response
472+
$__source__
468473
HTTP.request(HTTP.stack($requestlayers, $streamlayers), method, url, headers, body, query; kw...)
474+
end
469475
end)
470476
end
471477

test/client.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@ using Sockets
99
using JSON
1010
using Test
1111
using URIs
12+
using InteractiveUtils: @which
1213

1314
# test we can adjust default_connection_limit
1415
HTTP.set_default_connection_limit!(12)
1516

17+
@testset "@client macro" begin
18+
@eval module MyClient
19+
using HTTP
20+
HTTP.@client () ()
21+
end
22+
# Test the `@client` sets the location info to the definition site, i.e. this file.
23+
meth = @which MyClient.get()
24+
file = String(meth.file)
25+
@test file == @__FILE__
26+
end
27+
1628
@testset "Custom HTTP Stack" begin
1729
@testset "Low-level Request" begin
1830
wasincluded = Ref(false)

0 commit comments

Comments
 (0)