Class: AWS::Core::Http::HTTPartyHandler

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/aws/core/http/httparty_handler.rb

Overview

Makes HTTP requests using HTTParty. This is the default handler, so you don’t need to do anything special to configure it. However, you can directly instantiate this class in order to send extra options to HTTParty, for example to enable an HTTP proxy:

AWS.config(
  :http_handler => AWS::Http::HTTPartyHandler.new(
    :http_proxyaddr => "http://myproxy.com",
    :http_proxyport => 80
  )
)

Direct Known Subclasses

Http::HTTPartyHandler

Defined Under Namespace

Classes: NoOpParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HTTPartyHandler

Constructs a new HTTP handler using HTTParty.

Parameters:

  • options (Hash) (defaults to: {})

    Default options to send to HTTParty on each request. These options will be sent to get, post, head, put, or delete when a request is made. Note that :body, :headers, :parser, and :ssl_ca_file are ignored. If you need to set the CA file, you should use the :ssl_ca_file option to AWS.config or Configuration instead.



48
49
50
# File 'lib/aws/core/http/httparty_handler.rb', line 48

def initialize options = {}
  @default_request_options = options
end

Instance Attribute Details

#default_request_optionsHash (readonly)

Returns The default options to send to HTTParty on each request.

Returns:

  • (Hash)

    The default options to send to HTTParty on each request.



37
38
39
# File 'lib/aws/core/http/httparty_handler.rb', line 37

def default_request_options
  @default_request_options
end

Instance Method Details

#handle(request, response) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/aws/core/http/httparty_handler.rb', line 58

def handle(request, response)
  
  opts = default_request_options.merge({
    :body => request.body,
    :parser => NoOpParser
  })
  
  if request.proxy_uri
    opts[:http_proxyaddr] = request.proxy_uri.host
    opts[:http_proxyport] = request.proxy_uri.port
  end
  
  if request.use_ssl?
    protocol = 'https'
    opts[:ssl_ca_file] = request.ssl_ca_file if request.ssl_verify_peer?
  else
    protocol = 'http'
  end

  url = "#{protocol}://#{request.host}:#{request.port}#{request.uri}"
  
  # get, post, put, delete, head
  method = request.http_method.downcase
  
  # Net::HTTP adds this header for us when the body is
  # provided, but it messes up signing
  headers = { 'content-type' => '' }
  
  # headers must have string values (net http calls .strip on them)
  request.headers.each_pair do |key,value|
    headers[key] = value.to_s
  end
  
  opts[:headers] = headers
  
  begin
    http_response = self.class.send(method, url, opts)
  rescue Timeout::Error, Errno::ETIMEDOUT => e
    response.timeout = true
  else
    response.body = http_response.body
    response.status = http_response.code.to_i
    response.headers = http_response.to_hash
  end
end