introducing base_path option

This should complement the `:origin` option, in order to provide good
defaults to build REST SDKs around of.

Ex:

```ruby
HTTPX.with(origin: "https://api.this-product.com", base_path: "/v3.1")
```
This commit is contained in:
HoneyryderChuck 2022-05-08 17:43:26 +01:00
parent e3191f0d6c
commit 0d01f728aa
3 changed files with 16 additions and 1 deletions

View File

@ -132,6 +132,7 @@ module HTTPX
def freeze
super
@origin.freeze
@base_path.freeze
@timeout.freeze
@headers.freeze
@addresses.freeze
@ -141,6 +142,10 @@ module HTTPX
URI(value)
end
def option_base_path(value)
String(value)
end
def option_headers(value)
Headers.new(value)
end

View File

@ -49,7 +49,9 @@ module HTTPX
origin = @options.origin
raise(Error, "invalid URI: #{@uri}") unless origin
@uri = origin.merge(@uri)
base_path = @options.base_path
@uri = origin.merge("#{base_path}#{@uri}")
end
raise(Error, "unknown method: #{verb}") unless METHODS.include?(@verb)

View File

@ -20,6 +20,14 @@ module Requests
verify_body_length(response)
end
def test_http_get_option_origin_base_path
status_uri = URI(build_uri("/status"))
http = HTTPX.with(origin: status_uri.origin, base_path: status_uri.request_uri)
response = http.get("/200")
verify_status(response, 200)
assert response.uri.request_uri == "#{status_uri.request_uri}/200"
end
def test_http_request
uri = build_uri("/get")
response = HTTPX.request(:get, uri)