2 # Used to setup urls, params, headers, and the request body in a sane manner.
4 # @connection.post do |req|
5 # req.url 'http://localhost', 'a' => '1' # 'http://localhost?a=1'
6 # req.headers['b'] = '2' # Header
7 # req.params['c'] = '3' # GET Param
8 # req['b'] = '2' # also Header
12 class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
13 extend MiddlewareRegistry
15 register_middleware File.expand_path('../request', __FILE__),
16 :url_encoded => [:UrlEncoded, 'url_encoded'],
17 :multipart => [:Multipart, 'multipart'],
18 :retry => [:Retry, 'retry'],
19 :authorization => [:Authorization, 'authorization'],
20 :basic_auth => [:BasicAuthentication, 'basic_authentication'],
21 :token_auth => [:TokenAuthentication, 'token_authentication'],
22 :instrumentation => [:Instrumentation, 'instrumentation']
24 def self.create(request_method)
25 new(request_method).tap do |request|
26 yield(request) if block_given?
30 # Public: Replace params, preserving the existing hash type
39 # Public: Replace request headers, preserving the existing hash type
48 def url(path, params = nil)
49 if path.respond_to? :query
55 path, query = path.split('?', 2)
58 self.params.merge_query query, options.params_encoder
59 self.params.update(params) if params
71 # :method - a symbolized request method (:get, :post)
72 # :body - the request body that will eventually be converted to a string.
73 # :url - URI instance for the current request.
74 # :status - HTTP response status code
75 # :request_headers - hash of HTTP Headers to be sent to the server
76 # :response_headers - Hash of HTTP headers from the server
77 # :parallel_manager - sent if the connection is in parallel mode
78 # :request - Hash of options for configuring the request.
79 # :timeout - open/read timeout Integer in seconds
80 # :open_timeout - read timeout Integer in seconds
81 # :proxy - Hash of proxy options
82 # :uri - Proxy Server URI
83 # :user - Proxy server username
84 # :password - Proxy server password
85 # :ssl - Hash of options for configuring SSL requests.
86 def to_env(connection)
87 Env.new(method, body, connection.build_exclusive_url(path, params),
88 options, headers, connection.ssl, connection.parallel_manager)