Class: HTTP::Client

Inherits:
Object
  • Object
show all
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

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

encode64

Constructor Details

#initialize(default_options = nil) ⇒ HTTP::Client

Initialize a new HTTP Client

Examples:

client = HTTP::Client.new(headers: {"Accept" => "application/json"})

Parameters:

  • default_options (HTTP::Options, nil) (defaults to: nil)

    existing options instance

  • options (Hash)

    keyword options (see HTTP::Options#initialize)



30
31
32
33
34
# File 'lib/http/client.rb', line 30

def initialize(default_options = nil, **)
  @default_options = HTTP::Options.new(default_options, **)
  @connection = nil
  @state = :clean
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close the connection and reset state

Examples:

client.close


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

Examples:

client.perform(request, options)

Parameters:

Returns:



88
89
90
91
92
93
94
# File 'lib/http/client.rb', line 88

def perform(req, options)
  if options.retriable
    perform_with_retry(req, options)
  else
    perform_once(req, options)
  end
end

#persistent?Boolean

Indicate whether the client has persistent connections

Examples:

client.persistent?

Returns:

  • (Boolean)

    whenever client is persistent

See Also:



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

Examples:

client.request(:get, "https://example.com")

Parameters:

  • verb (Symbol)

    the HTTP method

  • uri (#to_s)

    the URI to request

Returns:



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: 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