lib/httpx/session.rb in httpx-1.2.6 vs lib/httpx/session.rb in httpx-1.3.0

- old
+ new

@@ -1,18 +1,18 @@ # frozen_string_literal: true module HTTPX + EMPTY_HASH = {}.freeze + # 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 class Session include Loggable include Chainable - EMPTY_HASH = {}.freeze - # 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. def initialize(options = EMPTY_HASH, &blk) @@ -63,14 +63,14 @@ # 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" }) # - def request(*args, **options) + def request(*args, **params) raise ArgumentError, "must perform at least one request" if args.empty? - requests = args.first.is_a?(Request) ? args : build_requests(*args, options) + requests = args.first.is_a?(Request) ? args : build_requests(*args, params) responses = send_requests(*requests) return responses.first if responses.size == 1 responses end @@ -79,14 +79,13 @@ # 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) - def build_request(verb, uri, options = EMPTY_HASH) - rklass = @options.request_class - options = @options.merge(options) unless options.is_a?(Options) - request = rklass.new(verb, uri, options) + def build_request(verb, uri, params = EMPTY_HASH, options = @options) + rklass = options.request_class + request = rklass.new(verb, uri, options, params) request.persistent = @persistent set_request_callbacks(request) request end @@ -131,11 +130,11 @@ connection = find_connection(request, connections, options) connection.send(request) end return unless error.is_a?(Error) - request.emit(:response, ErrorResponse.new(request, error, options)) + request.emit(:response, ErrorResponse.new(request, error)) end # sets the callbacks on the +connection+ required to process certain specific # connection lifecycle events which deal with request rerouting. def set_connection_callbacks(connection, connections, options, cloned: false) @@ -190,25 +189,29 @@ altsvc["noop"] = true nil end # returns a set of HTTPX::Request objects built from the given +args+ and +options+. - def build_requests(*args, options) - request_options = @options.merge(options) - + def build_requests(*args, params) requests = if args.size == 1 reqs = args.first - reqs.map do |verb, uri, opts = EMPTY_HASH| - build_request(verb, uri, request_options.merge(opts)) + # TODO: find a way to make requests share same options object + reqs.map do |verb, uri, ps = EMPTY_HASH| + request_params = params + request_params = request_params.merge(ps) unless ps.empty? + build_request(verb, uri, request_params) end else verb, uris = args if uris.respond_to?(:each) - uris.enum_for(:each).map do |uri, opts = EMPTY_HASH| - build_request(verb, uri, request_options.merge(opts)) + # TODO: find a way to make requests share same options object + uris.enum_for(:each).map do |uri, ps = EMPTY_HASH| + request_params = params + request_params = request_params.merge(ps) unless ps.empty? + build_request(verb, uri, request_params) end else - [build_request(verb, uris, request_options)] + [build_request(verb, uris, params)] end end raise ArgumentError, "wrong number of URIs (given 0, expect 1..+1)" if requests.empty? requests