Class: HTTP::Client
- Inherits:
-
Object
- Object
- HTTP::Client
- Extended by:
- Forwardable
- Includes:
- Chainable
- Defined in:
- lib/http/client.rb
Overview
Clients make requests and receive responses
Constant Summary
Constants included from Chainable
HTTP::Chainable::PROXY_ARG_MAP
Instance Method Summary collapse
-
#close ⇒ void
Close the connection and reset state.
-
#initialize(default_options = nil) ⇒ HTTP::Client
constructor
Initialize a new HTTP Client.
-
#perform(req, options) ⇒ HTTP::Response
Perform a single (no follow) HTTP request.
-
#persistent? ⇒ Boolean
Indicate whether the client has persistent connections.
-
#request(verb, uri, headers: nil, params: nil, form: nil, json: nil, body: nil, response: nil, encoding: nil, follow: nil, ssl: nil, ssl_context: nil, proxy: nil, nodelay: nil, features: nil, retriable: nil, socket_class: nil, ssl_socket_class: nil, timeout_class: nil, timeout_options: nil, keep_alive_timeout: nil, base_uri: nil, persistent: nil) ⇒ HTTP::Response
Make an HTTP request.
Methods included from Chainable
#accept, #auth, #base_uri, #basic_auth, #cookies, #default_options, #default_options=, #digest_auth, #encoding, #follow, #headers, #nodelay, #persistent, #retriable, #timeout, #use, #via
Methods included from HTTP::Chainable::Verbs
#connect, #delete, #get, #head, #options, #patch, #post, #put, #trace
Methods included from Base64
Constructor Details
#initialize(default_options = nil) ⇒ HTTP::Client
Initialize a new HTTP Client
30 31 32 33 34 |
# File 'lib/http/client.rb', line 30 def initialize( = nil, **) @default_options = HTTP::Options.new(, **) @connection = nil @state = :clean end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the connection and reset state
103 104 105 106 107 |
# File 'lib/http/client.rb', line 103 def close @connection&.close @connection = nil @state = :clean end |
#perform(req, options) ⇒ HTTP::Response
Perform a single (no follow) HTTP request
88 89 90 91 92 93 94 |
# File 'lib/http/client.rb', line 88 def perform(req, ) if .retriable perform_with_retry(req, ) else perform_once(req, ) end end |
#persistent? ⇒ Boolean
Indicate whether the client has persistent connections
77 |
# File 'lib/http/client.rb', line 77 def_delegator :default_options, :persistent? |
#request(verb, uri, headers: nil, params: nil, form: nil, json: nil, body: nil, response: nil, encoding: nil, follow: nil, ssl: nil, ssl_context: nil, proxy: nil, nodelay: nil, features: nil, retriable: nil, socket_class: nil, ssl_socket_class: nil, timeout_class: nil, timeout_options: nil, keep_alive_timeout: nil, base_uri: nil, persistent: nil) ⇒ HTTP::Response
Make an HTTP request
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/http/client.rb', line 45 def request(verb, uri, headers: nil, params: nil, form: nil, json: nil, body: nil, response: nil, encoding: nil, follow: nil, ssl: nil, ssl_context: nil, proxy: nil, nodelay: nil, features: nil, retriable: nil, socket_class: nil, ssl_socket_class: nil, timeout_class: nil, timeout_options: nil, keep_alive_timeout: nil, base_uri: nil, persistent: nil) opts = { headers: headers, params: params, form: form, json: json, body: body, response: response, encoding: encoding, follow: follow, ssl: ssl, ssl_context: ssl_context, proxy: proxy, nodelay: nodelay, features: features, retriable: retriable, socket_class: socket_class, ssl_socket_class: ssl_socket_class, timeout_class: timeout_class, timeout_options: , keep_alive_timeout: keep_alive_timeout, base_uri: base_uri, persistent: persistent }.compact opts = @default_options.merge(opts) builder = Request::Builder.new(opts) req = builder.build(verb, uri) res = perform(req, opts) return res unless opts.follow Redirector.new(**opts.follow).perform(req, res) do |request| perform(builder.wrap(request), opts) end end |