I’m looking at 1.7.3’s HTTP::Client and I was wondering if this change could be made? Not to one function, probably to most of the functions in the file.
def exec(method : String, path, headers : HTTP::Headers? = nil, body : BodyType = nil)
- exec(new_request(method, path, headers, body)) do |response|
- yield response, request
- end
+ request = new_request(method, path, headers, body)
+ exec(request) do |response|
+ yield response, request
+ end
end
I have a scenario where I’d like to do some stuff where I’m able to use the request object in some of my error reporting
request = HTTP::Request.new("GET", "/api/example-endpoint")
response = client.exec request
ok_or_raise request, response
# do other stuff
and it would be nicer to write this as:
client.get "/api/example-endpoint" do |response, request|
ok_or_raise response, request
# do other stuff
end
both samples using this macro:
macro ok_or_raise(request, response)
unless {{response.id}}.success?
%msg = "#{{{response.id}}.status_code} #{{{response.id}}.status} #{{{request.id}}.path} "
Log.error { "Unexpected status code: #{{{response.id}}.status_code}" }
Log.error { %msg }
Log.error { {{response.id}}.body }
raise Exception.new(%msg)
end
end
the order of |response, request|
is backwards from what most people are use to in this example, I only have it that way for backwards compatibility. Also, all my samples are a rough draft, I barely know this language and never used ruby, so maybe there is some obvious utility I’m not aware exists.