Module: HTTP::Chainable
- Defined in:
- lib/http/chainable.rb,
lib/http/chainable/verbs.rb,
lib/http/chainable/helpers.rb
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.
-
#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.
-
#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.
-
#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. “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 |
#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 |
#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
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 |
#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 |