-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set default host in HTTP::Client#exec #6481
Set default host in HTTP::Client#exec #6481
Conversation
Just note that even though this is a good change, the original issue is not solved: the use case was to send a request to multiple hosts by reusing the request instance. Because the host is only set if it's not set already, that won't work. Maybe we should remove the exec methods from being public. I don't think there's a sane way to reuse request objects, unless we dup/clone them in exec, but that's of course not ideal. |
Yeah, I figured as much. But the original issue can be solved by dup-ing the request when providing it to Obviously, this is not ideal, but it works fine for that use case. I'd suggest to merge this fix and continue with #6011 to redesign HTTP client. |
I would recommend then documenting this behavior, mentioning that reusing an |
An alternative solution would be to modify |
@asterite re: removing For what it's worth, I've made good use of this method in libs. It's been helpful to construct a namespace of Basically: # requests.cr
module Request
extend self
def get_resource(id : ID)
HTTP::Request.new("GET", "/resource?id=#{id}")
end
end
# client.cr
class Client
@http_client : HTTP::Client
def exec(request : HTTP::Request)
@http_client.exec(request)
end
def get_resource(id : ID)
response = Request.get_resource(id)
Resource.from_json(response.body)
end
end
# request_spec.cr
it "#get_resource" do
request = Request.get_resource(1)
request.method.should eq "GET"
request.path.should eq "/resource"
request.query.should eq "id=1"
end I could replace this with my own types if removed with a slightly more complicated interface, but having Maybe mock connections via #6011 would provide a equally simple solution, but that's still an abstract bullet point on the list, so I don't know |
Correct me if I'm wrong, by removing |
A reusable |
Fixes #6480