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
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.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/philiprehberger/http_client/client.rb', line 31 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).
67 68 69 |
# File 'lib/philiprehberger/http_client/client.rb', line 67 def cache @cache end |
#cookie_jar ⇒ CookieJar? (readonly)
Returns the cookie jar (nil if cookies are disabled).
57 58 59 |
# File 'lib/philiprehberger/http_client/client.rb', line 57 def @cookie_jar end |
#pool ⇒ Pool? (readonly)
Returns the connection pool (nil if pooling is disabled).
62 63 64 |
# File 'lib/philiprehberger/http_client/client.rb', line 62 def pool @pool end |
#request_count ⇒ Integer (readonly)
Returns the total number of requests executed.
52 53 54 |
# File 'lib/philiprehberger/http_client/client.rb', line 52 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.
221 222 223 224 225 226 |
# File 'lib/philiprehberger/http_client/client.rb', line 221 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.
196 197 198 199 200 |
# File 'lib/philiprehberger/http_client/client.rb', line 196 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.
186 187 188 189 |
# File 'lib/philiprehberger/http_client/client.rb', line 186 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.
205 206 207 |
# File 'lib/philiprehberger/http_client/client.rb', line 205 def clear_cache! @cache&.clear! end |
#close ⇒ void
This method returns an undefined value.
Drain the connection pool. No-op if pooling is disabled.
212 213 214 |
# File 'lib/philiprehberger/http_client/client.rb', line 212 def close @pool&.drain end |
#delete(path, headers: {}, expect: nil, request_id: nil, **timeout_opts) ⇒ Response
Perform a DELETE request.
176 177 178 179 180 |
# File 'lib/philiprehberger/http_client/client.rb', line 176 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.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/philiprehberger/http_client/client.rb', line 95 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.
118 119 120 121 122 |
# File 'lib/philiprehberger/http_client/client.rb', line 118 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.
162 163 164 |
# File 'lib/philiprehberger/http_client/client.rb', line 162 def patch(path, ...) request_with_body(Net::HTTP::Patch, path, ...) end |
#post(path) ⇒ Response
Perform a POST request.
134 135 136 |
# File 'lib/philiprehberger/http_client/client.rb', line 134 def post(path, ...) request_with_body(Net::HTTP::Post, path, ...) end |
#put(path) ⇒ Response
Perform a PUT request.
148 149 150 |
# File 'lib/philiprehberger/http_client/client.rb', line 148 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).
77 78 79 80 |
# File 'lib/philiprehberger/http_client/client.rb', line 77 def use(&block) @interceptors << block self end |