Class Wrest::Native::Request

  1. lib/wrest/native/request.rb
  2. lib/wrest/test/request_patches.rb
  3. show all
Parent: Object

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.

Methods

public class

  1. new

public instance

  1. build_request
  2. do_request
  3. execute_callback_if_any
  4. invoke
  5. invoke

Attributes

body [R]
cache_store [R]
connection [R]
follow_redirects [R]
follow_redirects_count [R]
follow_redirects_limit [R]
headers [R]
http_request [R]
options [R]
parameters [R]
password [R]
timeout [R]
uri [R]
username [R]
verify_mode [R]

Public class methods

new (wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, options = {})

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.
:verify_mode => The  verification mode to be used for Net::HTTP https connections. Defaults to OpenSSL::SSL::VERIFY_PEER
:cache_store => The object which should be used as cache store for cacheable responses. If not supplied, caching will be disabled.
:detailed_http_logging => nil/$stdout/$stderr or File/Logger/IO object. Defaults to nil (recommended).
: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 } }

WARNING : detailed_http_logging causes a serious security hole. Never use it in production code.

[show source]
    # File lib/wrest/native/request.rb, line 43
43:     def initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, options = {})
44:       @uri = wrest_uri
45:       @headers = headers.stringify_keys
46:       @parameters = parameters
47:       @body = body
48:       @options = options.clone
49:       @username = @options[:username]
50:       @password = @options[:password]
51:       @follow_redirects = (@options[:follow_redirects] ||= false)
52:       @follow_redirects_count = (@options[:follow_redirects_count] ||= 0)
53:       @follow_redirects_limit = (@options[:follow_redirects_limit] ||= 5)
54:       @timeout = @options[:timeout]
55:       @connection = @options[:connection]
56:       @http_request = self.build_request(http_request_klass, @uri, @parameters, @headers)
57:       @cache_store = options[:cache_store]
58:       @verify_mode = @options[:verify_mode]
59:       @detailed_http_logging = options[:detailed_http_logging]
60:       @uri_callback = Wrest::Callback.new(@options[:callback] || {})
61:       @request_callback = Wrest::Callback.new(@options[:callback_block] || {})
62:     end

Public instance methods

build_request (request_klass, uri, parameters, headers)

:nodoc:

[show source]
     # File lib/wrest/native/request.rb, line 103
103:     def build_request(request_klass, uri, parameters, headers)
104:       if(!uri.query.empty?)
105:         request_klass.new(parameters.empty? ? "#{uri.uri_path}?#{uri.query}" : "#{uri.uri_path}?#{uri.query}&#{parameters.to_query}", headers)
106:       else
107:         request_klass.new(parameters.empty? ? "#{uri.uri_path}" : "#{uri.uri_path}?#{parameters.to_query}", headers)
108:       end
109:     end
do_request ()

:nodoc:

[show source]
     # File lib/wrest/native/request.rb, line 112
112:     def do_request
113:       @connection.request(@http_request, @body)
114:     end
execute_callback_if_any (actual_response)

:nodoc:

[show source]
     # File lib/wrest/native/request.rb, line 117
117:     def execute_callback_if_any(actual_response)
118:       @uri_callback.merge(@request_callback).execute(actual_response)
119:     end
invoke ()

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.

[show source]
     # File lib/wrest/native/request.rb, line 83
 83:     def invoke
 84:       response = nil
 85:       @connection ||= @uri.create_connection({:timeout => timeout, :verify_mode => @verify_mode})
 86:       @connection.set_debug_output @detailed_http_logging
 87:       http_request.basic_auth username, password unless username.nil? || password.nil?
 88: 
 89:       prefix = "#{http_request.method} #{self.hash} #{@connection.hash}"
 90:       
 91:       Wrest.logger.debug "<- (#{prefix}) #{@uri.protocol}://#{@uri.host}:#{@uri.port}#{@http_request.path}"
 92:       time = Benchmark.realtime { response = Wrest::Native::Response.new( do_request ) }
 93:       Wrest.logger.debug "-> (#{prefix}) %d %s (%d bytes %.2fs)" % [response.code, response.message, response.body ? response.body.length : 0, time]
 94: 
 95:       execute_callback_if_any(response)
 96:       
 97:       @follow_redirects ? response.follow(@options) : response
 98:     rescue Timeout::Error => e
 99:       raise Wrest::Exceptions::Timeout.new(e)
100:     end
invoke ()
[show source]
   # File lib/wrest/test/request_patches.rb, line 2
2:   def invoke
3:     raise Wrest::Exceptions::RealRequestMadeInTestEnvironmet, 'A real HTTP request was made while running tests. Please avoid using live HTTP connections while testing and replace them with mocks.'
4:   end