Module: HTTP::Chainable
- Defined in:
- lib/http/chainable.rb,
lib/http/chainable/verbs.rb,
lib/http/chainable/helpers.rb,
sig/http.rbs
Overview
HTTP verb methods and client configuration DSL
Defined Under Namespace
Modules: Verbs
Constant Summary collapse
- PROXY_ARG_MAP =
Mapping of proxy argument positions to hash keys and expected types
[ [:proxy_address, 0, String], [:proxy_port, 1, Integer], [:proxy_username, 2, String], [:proxy_password, 3, String], [:proxy_headers, 2, Hash], [:proxy_headers, 4, Hash] ].freeze
Instance Method Summary collapse
-
#accept(type) ⇒ HTTP::Session
Accept the given MIME type(s).
-
#auth(value) ⇒ HTTP::Session
Make a request with the given Authorization header.
-
#base_uri(uri) ⇒ HTTP::Session
Set a base URI for resolving relative request paths.
-
#basic_auth(user:, pass:) ⇒ HTTP::Session
Make a request with the given Basic authorization header.
-
#branch(options) ⇒ HTTP::Session
private
Create a new session with the given options.
-
#build_proxy_hash(proxy) ⇒ Hash
private
Build proxy configuration hash from positional arguments.
-
#cookies(cookies) ⇒ HTTP::Session
Make a request with the given cookies.
-
#default_options ⇒ HTTP::Options
Get options for HTTP.
-
#default_options=(opts) ⇒ HTTP::Options
Set options for HTTP.
-
#digest_auth(user:, pass:) ⇒ HTTP::Session
Enable HTTP Digest authentication.
-
#encoding(encoding) ⇒ HTTP::Session
Force a specific encoding for response body.
-
#follow(strict: nil, max_hops: nil, on_redirect: nil) ⇒ HTTP::Session
Make client follow redirects.
-
#headers(headers) ⇒ HTTP::Session
Make a request with the given headers.
-
#make_client(options) ⇒ HTTP::Client
private
Create a new client for executing a request.
-
#nodelay ⇒ HTTP::Session
Set TCP_NODELAY on the socket.
-
#persistent(host = nil, timeout: 5) ⇒ HTTP::Session, Object
Open a persistent connection to a host.
-
#request(verb, uri) {|response| ... } ⇒ HTTP::Response, Object
Make an HTTP request with the given verb.
-
#resolve_global_only(global) ⇒ Array(Class, Hash)
private
Build options for a global-only timeout from a hash.
-
#resolve_timeout_hash(options) ⇒ Array(Class, Hash)
private
Resolve a timeout hash into a timeout class and normalized options.
-
#retriable(tries: nil, delay: nil, exceptions: nil, retry_statuses: nil, on_retry: nil, max_delay: nil, should_retry: nil) ⇒ HTTP::Session
Return a retriable session that retries on failure.
-
#timeout(options) ⇒ HTTP::Session
Set timeout on the request.
-
#use(*features) ⇒ HTTP::Session
Enable one or more features.
-
#via(*proxy) ⇒ HTTP::Session
(also: #through)
Make a request through an HTTP proxy.
Methods included from Verbs
#connect, #delete, #get, #head, #options, #patch, #post, #put, #trace
Methods included from Base64
Instance Method Details
#accept(type) ⇒ HTTP::Session
Accept the given MIME type(s)
238 239 240 |
# File 'lib/http/chainable.rb', line 238 def accept(type) headers Headers::ACCEPT => MimeType.normalize(type) end |
#auth(value) ⇒ HTTP::Session
Make a request with the given Authorization header
250 251 252 |
# File 'lib/http/chainable.rb', line 250 def auth(value) headers Headers::AUTHORIZATION => value.to_s end |
#base_uri(uri) ⇒ HTTP::Session
Set a base URI for resolving relative request paths
The first call must use an absolute URI that includes a scheme (e.g. "https://example.com"). Once a base URI is set, subsequent chained calls may use relative paths that are resolved against the existing base.
87 88 89 |
# File 'lib/http/chainable.rb', line 87 def base_uri(uri) branch .with_base_uri(uri) end |
#basic_auth(user:, pass:) ⇒ HTTP::Session
Make a request with the given Basic authorization header
264 265 266 |
# File 'lib/http/chainable.rb', line 264 def basic_auth(user:, pass:) auth("Basic #{encode64("#{user}:#{pass}")}") end |
#branch(options) ⇒ HTTP::Session
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new session with the given options
364 365 366 |
# File 'lib/http/chainable.rb', line 364 def branch() HTTP::Session.new() end |
#build_proxy_hash(proxy) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build proxy configuration hash from positional arguments
23 24 25 26 27 28 29 |
# File 'lib/http/chainable/helpers.rb', line 23 def build_proxy_hash(proxy) result = {} #: Hash[Symbol, untyped] PROXY_ARG_MAP.each do |key, index, type| result[key] = proxy[index] if proxy[index].is_a?(type) end result end |
#cookies(cookies) ⇒ HTTP::Session
Make a request with the given cookies
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/http/chainable.rb', line 205 def () value = .map do |entry| case entry when HTTP::Cookie then entry. else name, val = entry HTTP::Cookie.new(name.to_s, val.to_s). end end.join("; ") headers(Headers::COOKIE => value) end |
#default_options ⇒ HTTP::Options
Get options for HTTP
292 293 294 |
# File 'lib/http/chainable.rb', line 292 def @default_options ||= HTTP::Options.new end |
#default_options=(opts) ⇒ HTTP::Options
Set options for HTTP
304 305 306 |
# File 'lib/http/chainable.rb', line 304 def (opts) @default_options = HTTP::Options.new(opts) end |
#digest_auth(user:, pass:) ⇒ HTTP::Session
Enable HTTP Digest authentication
Automatically handles 401 Digest challenges by computing the digest response and retrying the request with proper credentials.
281 282 283 |
# File 'lib/http/chainable.rb', line 281 def digest_auth(user:, pass:) use(digest_auth: { user: user, pass: pass }) end |
#encoding(encoding) ⇒ HTTP::Session
Force a specific encoding for response body
226 227 228 |
# File 'lib/http/chainable.rb', line 226 def encoding(encoding) branch .with_encoding(encoding) end |
#follow(strict: nil, max_hops: nil, on_redirect: nil) ⇒ HTTP::Session
Make client follow redirects
180 181 182 183 |
# File 'lib/http/chainable.rb', line 180 def follow(strict: nil, max_hops: nil, on_redirect: nil) opts = { strict: strict, max_hops: max_hops, on_redirect: on_redirect }.compact branch .with_follow(opts) end |
#headers(headers) ⇒ HTTP::Session
Make a request with the given headers
193 194 195 |
# File 'lib/http/chainable.rb', line 193 def headers(headers) branch .with_headers(headers) end |
#make_client(options) ⇒ HTTP::Client
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new client for executing a request
373 374 375 |
# File 'lib/http/chainable.rb', line 373 def make_client() HTTP::Client.new() end |
#nodelay ⇒ HTTP::Session
Set TCP_NODELAY on the socket
315 316 317 |
# File 'lib/http/chainable.rb', line 315 def nodelay branch .with_nodelay(true) end |
#persistent(host = nil, timeout: 5) ⇒ HTTP::Session #persistent(host = nil, timeout: 5) {|session| ... } ⇒ Object #persistent(host, timeout:) ⇒ Session #persistent ⇒ void
Open a persistent connection to a host
Returns an Session that pools persistent HTTP::Client instances by origin. This allows connection reuse within the same origin and transparent cross-origin redirect handling.
When no host is given, the origin is derived from the configured base URI.
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/http/chainable.rb', line 138 def persistent(host = nil, timeout: 5) host ||= .base_uri&.origin raise ArgumentError, "host is required for persistent connections" unless host = .merge(keep_alive_timeout: timeout).with_persistent(host) session = branch() return session unless block_given? yield session ensure session&.close if block_given? end |
#request(verb, uri) {|response| ... } ⇒ HTTP::Response, Object
Make an HTTP request with the given verb
26 27 28 29 30 31 32 33 34 |
# File 'lib/http/chainable.rb', line 26 def request(verb, uri, **, &block) client = make_client() response = client.request(verb, uri, **) return response unless block yield response ensure client&.close if block end |
#resolve_global_only(global) ⇒ Array(Class, Hash)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build options for a global-only timeout from a hash
56 57 58 59 60 |
# File 'lib/http/chainable/helpers.rb', line 56 def resolve_global_only(global) raise ArgumentError, "no timeout options given" unless global [HTTP::Timeout::Global, { global_timeout: global }] end |
#resolve_timeout_hash(options) ⇒ Array(Class, Hash)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolve a timeout hash into a timeout class and normalized options
40 41 42 43 44 45 46 47 48 |
# File 'lib/http/chainable/helpers.rb', line 40 def resolve_timeout_hash() remaining = .dup global = HTTP::Timeout::PerOperation.send(:extract_global_timeout!, remaining) return resolve_global_only(global) if remaining.empty? per_op = HTTP::Timeout::PerOperation.(remaining) global ? [HTTP::Timeout::Global, per_op.merge(global_timeout: global)] : [HTTP::Timeout::PerOperation, per_op] end |
#retriable(tries: nil, delay: nil, exceptions: nil, retry_statuses: nil, on_retry: nil, max_delay: nil, should_retry: nil) ⇒ HTTP::Session
Return a retriable session that retries on failure
350 351 352 353 354 355 |
# File 'lib/http/chainable.rb', line 350 def retriable(tries: nil, delay: nil, exceptions: nil, retry_statuses: nil, on_retry: nil, max_delay: nil, should_retry: nil) opts = { tries: tries, delay: delay, exceptions: exceptions, retry_statuses: retry_statuses, on_retry: on_retry, max_delay: max_delay, should_retry: should_retry }.compact branch .with_retriable(opts.empty? || opts) end |
#timeout(options = {}) ⇒ HTTP::Session #timeout(global_timeout) ⇒ HTTP::Session
Set timeout on the request
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/http/chainable.rb', line 53 def timeout() klass, = case when Numeric then [HTTP::Timeout::Global, { global_timeout: }] when Hash then resolve_timeout_hash() when :null then [HTTP::Timeout::Null, {}] else raise ArgumentError, "Use `.timeout(:null)`, " \ "`.timeout(global_timeout_in_seconds)` or " \ "`.timeout(connect: x, write: y, read: z)`." end branch .merge( timeout_class: klass, timeout_options: ) end |
#use(*features) ⇒ HTTP::Session
Enable one or more features
327 328 329 |
# File 'lib/http/chainable.rb', line 327 def use(*features) branch .with_features(features) end |
#via(*proxy) ⇒ HTTP::Session Also known as: through
Make a request through an HTTP proxy
160 161 162 163 164 165 166 |
# File 'lib/http/chainable.rb', line 160 def via(*proxy) proxy_hash = build_proxy_hash(proxy) raise(RequestError, "invalid HTTP proxy: #{proxy_hash}") unless (2..5).cover?(proxy_hash.keys.size) branch .with_proxy(proxy_hash) end |