Class implementing the APIs being used publicly.
HTTPX.get(..) #=> delegating to an internal HTTPX::Session object. HTTPX.plugin(..).get(..) #=> creating an intermediate HTTPX::Session with plugin, then sending the GET request
Public Class methods
initializes the session with a set of options
, which will be shared by all requests sent from it.
When pass a block, it’ll yield itself to it, then closes after the block is evaluated.
# File lib/httpx/session.rb 18 def initialize(options = EMPTY_HASH, &blk) 19 @options = self.class.default_options.merge(options) 20 @responses = {} 21 @persistent = @options.persistent 22 wrap(&blk) if blk 23 end
Public Instance methods
returns a HTTP::Request instance built from the HTTP verb
, the request uri
, and the optional set of request-specific options
. This request must be sent through the same session it was built from.
req = session.build_request("GET", "https://server.com") resp = session.request(req)
# File lib/httpx/session.rb 84 def build_request(verb, uri, params = EMPTY_HASH, options = @options) 85 rklass = options.request_class 86 request = rklass.new(verb, uri, options, params) 87 request.persistent = @persistent 88 set_request_callbacks(request) 89 request 90 end
closes all the active connections from the session
# File lib/httpx/session.rb 44 def close(*args) 45 pool.close(*args) 46 end
performs one, or multple requests; it accepts:
-
one or multiple
HTTPX::Request
objects; -
an HTTP verb, then a sequence of URIs or URI/options tuples;
-
one or multiple HTTP verb / uri / (optional) options tuples;
when present, the set of options
kwargs is applied to all of the sent requests.
respectively returns a single HTTPX::Response
response, or all of them in an Array, in the same order.
resp1 = session.request(req1) resp1, resp2 = session.request(req1, req2) resp1 = session.request("GET", "https://server.org/a") resp1, resp2 = session.request("GET", ["https://server.org/a", "https://server.org/b"]) resp1, resp2 = session.request(["GET", "https://server.org/a"], ["GET", "https://server.org/b"]) resp1 = session.request("POST", "https://server.org/a", form: { "foo" => "bar" }) resp1, resp2 = session.request(["POST", "https://server.org/a", form: { "foo" => "bar" }], ["GET", "https://server.org/b"]) resp1, resp2 = session.request("GET", ["https://server.org/a", "https://server.org/b"], headers: { "x-api-token" => "TOKEN" })
# File lib/httpx/session.rb 68 def request(*args, **params) 69 raise ArgumentError, "must perform at least one request" if args.empty? 70 71 requests = args.first.is_a?(Request) ? args : build_requests(*args, params) 72 responses = send_requests(*requests) 73 return responses.first if responses.size == 1 74 75 responses 76 end
Yields itself the block, then closes it after the block is evaluated.
session.wrap do |http| http.get("https://wikipedia.com") end # wikipedia connection closes here
# File lib/httpx/session.rb 30 def wrap 31 prev_persistent = @persistent 32 @persistent = true 33 pool.wrap do 34 begin 35 yield self 36 ensure 37 @persistent = prev_persistent 38 close unless @persistent 39 end 40 end 41 end