Class: Wrest::Native::Request
- Inherits:
-
Object
- Object
- Wrest::Native::Request
- Defined in:
- lib/wrest/native/request.rb,
lib/wrest/test/request_patches.rb
Overview
This represents a HTTP request. Typically you will never need to instantiate one of these yourself - you can use one of the more conveient APIs via Wrest::Uri or Wrest::Native::Get etc. instead.
Direct Known Subclasses
Delete, Get, Options, Patch, Post, PostMultipart, Put, PutMultipart
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#ca_path ⇒ Object
readonly
Returns the value of attribute ca_path.
-
#cache_store ⇒ Object
readonly
Returns the value of attribute cache_store.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#follow_redirects ⇒ Object
readonly
Returns the value of attribute follow_redirects.
-
#follow_redirects_count ⇒ Object
readonly
Returns the value of attribute follow_redirects_count.
-
#follow_redirects_limit ⇒ Object
readonly
Returns the value of attribute follow_redirects_limit.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http_request ⇒ Object
readonly
Returns the value of attribute http_request.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
-
#verify_mode ⇒ Object
readonly
Returns the value of attribute verify_mode.
Instance Method Summary collapse
-
#build_request(request_klass, uri, parameters, headers) ⇒ Object
:nodoc:.
-
#do_request ⇒ Object
:nodoc:.
-
#execute_callback_if_any(actual_response) ⇒ Object
:nodoc:.
-
#initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, options = {}) ⇒ Request
constructor
Valid tuples for the options are: :username => String, defaults to nil :password => String, defaults to nil :follow_redirects => Boolean, defaults to true for Get, false for anything else :follow_redirects_limit => Integer, defaults to 5.
-
#invoke ⇒ Object
Makes a request, runs the appropriate callback if any and returns a Wrest::Native::Response.
Constructor Details
#initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, options = {}) ⇒ Request
Valid tuples for the options are:
:username => String, defaults to nil
:password => String, defaults to nil
:follow_redirects => Boolean, defaults to true for Get, false for anything else
:follow_redirects_limit => Integer, defaults to 5. This is the number of redirects
that Wrest will automatically follow before raising an
Wrest::Exceptions::AutoRedirectLimitExceeded exception.
For example, if you set this to 1, the very first redirect
will raise the exception.
:follow_redirects_count => Integer, defaults to 0. This is a count of the hops made to
get to this request and increases by one for every redirect
until the follow_redirects_limit is hit. You should never set
this option yourself.
:timeout => The period, in seconds, after which a Timeout::Error is raised
in the event of a connection failing to open. Defaulted to 60 by Uri#create_connection.
:connection => The HTTP Connection object to use. This is how a keep-alive connection can be
used for multiple requests.
:cache_store => The object which should be used as cache store for cacheable responses. If not supplied, caching will be disabled.
:callback => A Hash whose keys are the response codes (or Range of response codes),
and the values are the callback functions to be executed.
eg: { <response code> => lambda { |response| some_operation } }
The following options are Net::HTTP specific config options
:detailed_http_logging => nil/$stdout/$stderr or File/Logger/IO object. Defaults to nil (recommended).
*WARNING* : detailed_http_logging causes a serious security hole. Never use it in production code.
:verify_mode => The verification mode to be used for Net::HTTP https connections. Defaults to OpenSSL::SSL::VERIFY_PEER
:ca_path => The path to the certificates
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/wrest/native/request.rb', line 48 def initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, = {}) # rubocop:disable Metrics/ParameterLists setup_request_state!(body, headers, parameters, wrest_uri) () @detailed_http_logging = [:detailed_http_logging] @follow_redirects = (@options[:follow_redirects] ||= false) @follow_redirects_count = (@options[:follow_redirects_count] ||= 0) @follow_redirects_limit = (@options[:follow_redirects_limit] ||= 5) @callback = @options[:callback] || Wrest::Callback.new({}) @callback = @callback.merge(Wrest::Callback.new(@options[:callback_block] || {})) @http_request = build_request(http_request_klass, @uri, @parameters, @headers) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def body @body end |
#ca_path ⇒ Object (readonly)
Returns the value of attribute ca_path.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def ca_path @ca_path end |
#cache_store ⇒ Object (readonly)
Returns the value of attribute cache_store.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def cache_store @cache_store end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def connection @connection end |
#follow_redirects ⇒ Object (readonly)
Returns the value of attribute follow_redirects.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def follow_redirects @follow_redirects end |
#follow_redirects_count ⇒ Object (readonly)
Returns the value of attribute follow_redirects_count.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def follow_redirects_count @follow_redirects_count end |
#follow_redirects_limit ⇒ Object (readonly)
Returns the value of attribute follow_redirects_limit.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def follow_redirects_limit @follow_redirects_limit end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def headers @headers end |
#http_request ⇒ Object (readonly)
Returns the value of attribute http_request.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def http_request @http_request end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def @options end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def parameters @parameters end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def password @password end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def timeout @timeout end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def uri @uri end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def username @username end |
#verify_mode ⇒ Object (readonly)
Returns the value of attribute verify_mode.
18 19 20 |
# File 'lib/wrest/native/request.rb', line 18 def verify_mode @verify_mode end |
Instance Method Details
#build_request(request_klass, uri, parameters, headers) ⇒ Object
:nodoc:
95 96 97 98 99 100 101 102 103 |
# File 'lib/wrest/native/request.rb', line 95 def build_request(request_klass, uri, parameters, headers) if uri.query.empty? request_klass.new(parameters.empty? ? uri.uri_path.to_s : "#{uri.uri_path}?#{Utils.hash_to_param(parameters)}", headers) else request_klass.new( parameters.empty? ? "#{uri.uri_path}?#{uri.query}" : "#{uri.uri_path}?#{uri.query}&#{Utils.hash_to_param(parameters)}", headers ) end end |
#do_request ⇒ Object
:nodoc:
106 107 108 |
# File 'lib/wrest/native/request.rb', line 106 def do_request @connection.request(@http_request, @body) end |
#execute_callback_if_any(actual_response) ⇒ Object
:nodoc:
111 112 113 |
# File 'lib/wrest/native/request.rb', line 111 def execute_callback_if_any(actual_response) @callback.execute(actual_response) end |
#invoke ⇒ Object
Makes a request, runs the appropriate callback if any and returns a Wrest::Native::Response.
Data about the request is and logged to Wrest.logger The log entry contains the following information:
<- indicates a request
-> indicates a response
The type of request is mentioned in caps, followed by a hash uniquely identifying a particular request/response pair. In a multi-process or multi-threaded scenario, this can be used to identify request-response pairs.
The request hash is followed by a connection hash; requests using the same connection (effectively a keep-alive connection) will have the same connection hash.
Passing nil for either username or password will skip HTTP authentication
This is followed by the response code, the payload size and the time taken.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/wrest/native/request.rb', line 81 def invoke response = nil setup_connection! response = execute_request(response) execute_callback_if_any(response) @follow_redirects ? response.follow(@options) : response rescue Timeout::Error => e raise Wrest::Exceptions::Timeout, e end |