Class: Philiprehberger::HttpClient::Client
- Inherits:
-
Object
- Object
- Philiprehberger::HttpClient::Client
- Includes:
- Connection
- Defined in:
- lib/philiprehberger/http_client/client.rb
Constant Summary
Constants included from Connection
Philiprehberger::HttpClient::Connection::NETWORK_ERRORS, Philiprehberger::HttpClient::Connection::REDIRECT_CODES, Philiprehberger::HttpClient::Connection::RETRYABLE_ERRORS, Philiprehberger::HttpClient::Connection::TIMEOUT_ERRORS
Constants included from Retries
Instance Attribute Summary collapse
-
#cache ⇒ Cache?
readonly
Returns the response cache (nil if caching is disabled).
-
#cookie_jar ⇒ CookieJar?
readonly
Returns the cookie jar (nil if cookies are disabled).
-
#pool ⇒ Pool?
readonly
Returns the connection pool (nil if pooling is disabled).
-
#request_count ⇒ Integer
readonly
Returns the total number of requests executed.
Class Method Summary collapse
-
.open(**opts) {|Client| ... } ⇒ Object
Create a client, yield it to the block, and ensure it is closed afterward.
Instance Method Summary collapse
-
#basic_auth(username, password) ⇒ self
Set Basic auth credentials for all subsequent requests.
-
#bearer_token(token) ⇒ self
Set a Bearer token for all subsequent requests.
-
#clear_cache! ⇒ void
Flush the response cache.
-
#close ⇒ void
Drain the connection pool.
-
#delete(path, headers: {}, expect: nil, request_id: nil, **timeout_opts) ⇒ Response
Perform a DELETE request.
-
#get(path, params: {}, headers: {}, expect: nil, request_id: nil, **timeout_opts) {|String| ... } ⇒ Response
Perform a GET request.
-
#head(path, params: {}, headers: {}, expect: nil, request_id: nil, **timeout_opts) ⇒ Response
Perform a HEAD request.
-
#initialize(base_url:, headers: {}, timeout: 30, **opts) ⇒ Client
constructor
A new instance of Client.
-
#patch(path) ⇒ Response
Perform a PATCH request.
-
#post(path) ⇒ Response
Perform a POST request.
-
#put(path) ⇒ Response
Perform a PUT request.
-
#use {|Hash| ... } ⇒ self
Register a request/response interceptor.
Constructor Details
#initialize(base_url:, headers: {}, timeout: 30, **opts) ⇒ Client
Returns a new instance of Client.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/philiprehberger/http_client/client.rb', line 34 def initialize(base_url:, headers: {}, timeout: 30, **opts) @base_url = base_url.chomp('/') @default_headers = headers @timeout = timeout validate_timeout!(:timeout, timeout) assign_timeout_opts(opts) assign_retry_opts(opts) validate_config! (opts) assign_proxy_opts(opts) assign_redirect_opts(opts) assign_pool_opts(opts) assign_cache_opts(opts) @on_request = opts[:on_request] @interceptors = [] @request_count = 0 end |
Instance Attribute Details
#cache ⇒ Cache? (readonly)
Returns the response cache (nil if caching is disabled).
70 71 72 |
# File 'lib/philiprehberger/http_client/client.rb', line 70 def cache @cache end |
#cookie_jar ⇒ CookieJar? (readonly)
Returns the cookie jar (nil if cookies are disabled).
60 61 62 |
# File 'lib/philiprehberger/http_client/client.rb', line 60 def @cookie_jar end |
#pool ⇒ Pool? (readonly)
Returns the connection pool (nil if pooling is disabled).
65 66 67 |
# File 'lib/philiprehberger/http_client/client.rb', line 65 def pool @pool end |
#request_count ⇒ Integer (readonly)
Returns the total number of requests executed.
55 56 57 |
# File 'lib/philiprehberger/http_client/client.rb', line 55 def request_count @request_count end |
Class Method Details
.open(**opts) {|Client| ... } ⇒ Object
Create a client, yield it to the block, and ensure it is closed afterward.
224 225 226 227 228 229 |
# File 'lib/philiprehberger/http_client/client.rb', line 224 def self.open(**opts) client = new(**opts) yield client ensure client&.close end |
Instance Method Details
#basic_auth(username, password) ⇒ self
Set Basic auth credentials for all subsequent requests.
199 200 201 202 203 |
# File 'lib/philiprehberger/http_client/client.rb', line 199 def basic_auth(username, password) encoded = Base64.strict_encode64("#{username}:#{password}") @default_headers['authorization'] = "Basic #{encoded}" self end |
#bearer_token(token) ⇒ self
Set a Bearer token for all subsequent requests.
189 190 191 192 |
# File 'lib/philiprehberger/http_client/client.rb', line 189 def bearer_token(token) @default_headers['authorization'] = "Bearer #{token}" self end |
#clear_cache! ⇒ void
This method returns an undefined value.
Flush the response cache. No-op if caching is disabled.
208 209 210 |
# File 'lib/philiprehberger/http_client/client.rb', line 208 def clear_cache! @cache&.clear! end |
#close ⇒ void
This method returns an undefined value.
Drain the connection pool. No-op if pooling is disabled.
215 216 217 |
# File 'lib/philiprehberger/http_client/client.rb', line 215 def close @pool&.drain end |
#delete(path, headers: {}, expect: nil, request_id: nil, **timeout_opts) ⇒ Response
Perform a DELETE request.
179 180 181 182 183 |
# File 'lib/philiprehberger/http_client/client.rb', line 179 def delete(path, headers: {}, expect: nil, request_id: nil, **timeout_opts) uri = build_uri(path) request = Net::HTTP::Delete.new(uri) execute(uri, request, headers, expect: expect, request_id: request_id, **timeout_opts) end |
#get(path, params: {}, headers: {}, expect: nil, request_id: nil, **timeout_opts) {|String| ... } ⇒ Response
Perform a GET request.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/philiprehberger/http_client/client.rb', line 98 def get(path, params: {}, headers: {}, expect: nil, request_id: nil, **timeout_opts, &block) uri = build_uri(path, params) request = Net::HTTP::Get.new(uri) if @cache && !block cached = lookup_cache(uri, headers) return cached if cached end execute(uri, request, headers, expect: expect, request_id: request_id, **timeout_opts, &block) end |
#head(path, params: {}, headers: {}, expect: nil, request_id: nil, **timeout_opts) ⇒ Response
Perform a HEAD request.
121 122 123 124 125 |
# File 'lib/philiprehberger/http_client/client.rb', line 121 def head(path, params: {}, headers: {}, expect: nil, request_id: nil, **timeout_opts) uri = build_uri(path, params) request = Net::HTTP::Head.new(uri) execute(uri, request, headers, expect: expect, request_id: request_id, **timeout_opts) end |
#patch(path) ⇒ Response
Perform a PATCH request.
165 166 167 |
# File 'lib/philiprehberger/http_client/client.rb', line 165 def patch(path, ...) request_with_body(Net::HTTP::Patch, path, ...) end |
#post(path) ⇒ Response
Perform a POST request.
137 138 139 |
# File 'lib/philiprehberger/http_client/client.rb', line 137 def post(path, ...) request_with_body(Net::HTTP::Post, path, ...) end |
#put(path) ⇒ Response
Perform a PUT request.
151 152 153 |
# File 'lib/philiprehberger/http_client/client.rb', line 151 def put(path, ...) request_with_body(Net::HTTP::Put, path, ...) end |
#use {|Hash| ... } ⇒ self
Register a request/response interceptor.
The block receives a Hash with :request and, after the request completes, :response. It is called twice: once before the request (with :request only) and once after (with both :request and :response).
80 81 82 83 |
# File 'lib/philiprehberger/http_client/client.rb', line 80 def use(&block) @interceptors << block self end |