Class: Amazon::MWS::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/mws/connection.rb,
lib/amazon/mws/connection/management.rb,
lib/amazon/mws/connection/request_builder.rb

Defined Under Namespace

Modules: Management Classes: RequestBuilder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Connection

Returns a new instance of Connection.

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/amazon/mws/connection.rb', line 12

def initialize(params = {})
  # These values are essential to establishing a connection
  @server            = params['server'] || Amazon::MWS::DEFAULT_HOST
  @persistent        = params['persistent'] || false
  # These values are essential to signing requests
  @access_key        = params['access_key']
  @secret_access_key = params['secret_access_key']
  @merchant_id       = params['merchant_id']
  @marketplace_id    = params['marketplace_id']
  
  raise MissingConnectionOptions if [@access_key, @secret_access_key, @merchant_id, @marketplace_id].any? {|option| option.nil?}
  
  @http = connect
end

Class Method Details

.connect(options = {}) ⇒ Object



7
8
9
# File 'lib/amazon/mws/connection.rb', line 7

def connect(options = {})
  new(options)
end

Instance Method Details

#authenticate_query_string(verb, query_params = {}) ⇒ Object

Generates the authentication query string used by Amazon. Takes the http method and the query string of the request and returns the authenticated query string



74
75
76
77
78
79
80
81
82
83
# File 'lib/amazon/mws/connection.rb', line 74

def authenticate_query_string(verb, query_params = {})        
  Authentication::QueryString.new(
    :verb              => verb,
    :query_params      => query_params,
    :access_key        => @access_key,
    :secret_access_key => @secret_access_key,
    :merchant_id       => @merchant_id,
    :marketplace_id    => @marketplace_id
  )
end

#build_request(verb, path, body = nil) ⇒ Object

Builds up a Net::HTTP request object



86
87
88
89
90
91
92
93
# File 'lib/amazon/mws/connection.rb', line 86

def build_request(verb, path, body = nil)
  builder = RequestBuilder.new(verb, path, body)
  builder.add_user_agent
  builder.add_content_type
  builder.add_content_md5(body) unless body.nil?
  
  return builder.request
end

#connectObject

Create the Net::HTTP object to use for this connection



28
29
30
31
32
33
# File 'lib/amazon/mws/connection.rb', line 28

def connect
  http             = Net::HTTP.new(@server, 443)
  http.use_ssl     = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  return http
end

#prepare_path(verb, path, query_params) ⇒ Object

Create the signed authentication query string. Add this query string to the path WITHOUT prepending the server address.



67
68
69
70
# File 'lib/amazon/mws/connection.rb', line 67

def prepare_path(verb, path, query_params)
  query_string = authenticate_query_string(verb, query_params)
  return "#{path}?#{query_string}"
end

#request(verb, path, query_params, body = nil, attempts = 0, &block) ⇒ Object

Make the request, based on the apropriate request object Called from Amazon::MWS::Base



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/amazon/mws/connection.rb', line 37

def request(verb, path, query_params, body = nil, attempts = 0, &block)
  # presumably this is for files
  body.rewind if body.respond_to?(:rewind) unless attempts.zero?
  # Prepare the Proc to be called by Net::HTTP
  proc = requester(verb, path, query_params, body)
  
  if @persistent
    @http.start unless @http.started?
    proc.call
  else
    @http.start(&proc)
  end
rescue Errno::EPIPE, Timeout::Error, Errno::EINVAL, EOFError
  @http = connect
  attempts == 3 ? raise : (attempts += 1; retry)
end

#requester(verb, path, query_params, body) ⇒ Object

A Proc used by the request method



55
56
57
58
59
60
61
62
63
# File 'lib/amazon/mws/connection.rb', line 55

def requester(verb, path, query_params, body)
  Proc.new do |http|
    path    = prepare_path(verb, path, query_params)
    puts "#{path}\n\n" if AWS::MWS::Base.debug
    request = build_request(verb, path, body)
    
    @http.request(request)
  end
end