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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/wrest/native/request.rb', line 44 def initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, = {}) @uri = wrest_uri @headers = headers.stringify_keys @parameters = parameters @body = body @options = .clone @username = @options[:username] @password = @options[:password] @follow_redirects = (@options[:follow_redirects] ||= false) @follow_redirects_count = (@options[:follow_redirects_count] ||= 0) @follow_redirects_limit = (@options[:follow_redirects_limit] ||= 5) @timeout = @options[:timeout] @connection = @options[:connection] @http_request = self.build_request(http_request_klass, @uri, @parameters, @headers) @cache_store = @options[:cache_store] @verify_mode = @options[:verify_mode] @ca_path = @options[:ca_path] @detailed_http_logging = [:detailed_http_logging] @callback = @options[:callback] || Wrest::Callback.new({}) @callback = @callback.merge(Wrest::Callback.new(@options[:callback_block] || {})) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def body @body end |
#ca_path ⇒ Object (readonly)
Returns the value of attribute ca_path.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def ca_path @ca_path end |
#cache_store ⇒ Object (readonly)
Returns the value of attribute cache_store.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def cache_store @cache_store end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def connection @connection end |
#follow_redirects ⇒ Object (readonly)
Returns the value of attribute follow_redirects.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def follow_redirects @follow_redirects end |
#follow_redirects_count ⇒ Object (readonly)
Returns the value of attribute follow_redirects_count.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def follow_redirects_count @follow_redirects_count end |
#follow_redirects_limit ⇒ Object (readonly)
Returns the value of attribute follow_redirects_limit.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def follow_redirects_limit @follow_redirects_limit end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def headers @headers end |
#http_request ⇒ Object (readonly)
Returns the value of attribute http_request.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def http_request @http_request end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def @options end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def parameters @parameters end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def password @password end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def timeout @timeout end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def uri @uri end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def username @username end |
#verify_mode ⇒ Object (readonly)
Returns the value of attribute verify_mode.
15 16 17 |
# File 'lib/wrest/native/request.rb', line 15 def verify_mode @verify_mode end |
Instance Method Details
#build_request(request_klass, uri, parameters, headers) ⇒ Object
:nodoc:
105 106 107 108 109 110 111 |
# File 'lib/wrest/native/request.rb', line 105 def build_request(request_klass, uri, parameters, headers) if(!uri.query.empty?) request_klass.new(parameters.empty? ? "#{uri.uri_path}?#{uri.query}" : "#{uri.uri_path}?#{uri.query}&#{parameters.to_query}", headers) else request_klass.new(parameters.empty? ? "#{uri.uri_path}" : "#{uri.uri_path}?#{parameters.to_query}", headers) end end |
#do_request ⇒ Object
:nodoc:
114 115 116 |
# File 'lib/wrest/native/request.rb', line 114 def do_request @connection.request(@http_request, @body) end |
#execute_callback_if_any(actual_response) ⇒ Object
:nodoc:
119 120 121 |
# File 'lib/wrest/native/request.rb', line 119 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.
This is followed by the response code, the payload size and the time taken.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/wrest/native/request.rb', line 85 def invoke response = nil @connection ||= @uri.create_connection(:timeout => timeout, :verify_mode => verify_mode, :ca_path => ca_path) @connection.set_debug_output @detailed_http_logging http_request.basic_auth username, password unless username.nil? || password.nil? prefix = "#{http_request.method} #{self.hash} #{@connection.hash} #{Thread.current.object_id}" Wrest.logger.debug "<- (#{prefix}) #{@uri.protocol}://#{@uri.host}:#{@uri.port}#{@http_request.path}" Wrest.logger.debug "<- Body: #{@body}" time = Benchmark.realtime { response = Wrest::Native::Response.new( do_request ) } execute_callback_if_any(response) @follow_redirects ? response.follow(@options) : response rescue Timeout::Error => e raise Wrest::Exceptions::Timeout.new(e) end |