Skip to content

Commit

Permalink
API is taking shape
Browse files Browse the repository at this point in the history
  • Loading branch information
David Michael committed Oct 14, 2009
1 parent dd07c92 commit 0dc187d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 27 deletions.
4 changes: 1 addition & 3 deletions lib/amazon/mws/authentication/query_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ def initialize(params = {})
}

# Add any params that are passed in via uri before calculating the signature
query_string = params[:query_string]
query_params = query_params.merge(Hash.from_query_string(query)) unless query_string.empty?

query_params = query_params.merge(params[:query_params] || {})
# Calculate the signature
query_params['Signature'] = Signature.new(query_params, params)

Expand Down
8 changes: 4 additions & 4 deletions lib/amazon/mws/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Base
#
# It is unlikely that you would call this method directly. Subclasses of Base have convenience methods for each http request verb
# that wrap calls to request.
def self.request(verb, path, body = nil, attempts = 0, &block)
def self.request(verb, path, query_params = {}, body = nil, attempts = 0, &block)
# Find the connection method in connection/management.rb which is evaled into Amazon::MWS::Base
response = connection.request(verb, path, body, attempts, &block)
response = connection.request(verb, path, query_params, body, attempts, &block)

rescue InternalError, RequestTimeout
if attempts == 3
Expand All @@ -25,8 +25,8 @@ def self.request(verb, path, body = nil, attempts = 0, &block)
# Make some convenience methods
[:get, :post, :put, :delete, :head].each do |verb|
class_eval(<<-EVAL, __FILE__, __LINE__)
def self.#{verb}(path, body = nil, &block)
request(:#{verb}, path, body, &block)
def self.#{verb}(path, query_params = {}, body = nil, &block)
request(:#{verb}, path, query_params, body, &block)
end
EVAL
end
Expand Down
19 changes: 9 additions & 10 deletions lib/amazon/mws/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def connect

# Make the request, based on the apropriate request object
# Called from Amazon::MWS::Base
def request(verb, path, body = nil, attempts = 0, &block)
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, body)
proc = requester(verb, path, query_params, body)

if @persistent
@http.start unless @http.started?
Expand All @@ -50,28 +50,27 @@ def request(verb, path, body = nil, attempts = 0, &block)
end

# A Proc used by the request method
def requester(verb, path, body)
def requester(verb, path, query_params, body)
Proc.new do |http|
path = prepare_path(verb, path)
path = prepare_path(verb, path, query_params)
request = build_request(verb, path, body)
@http.request(request)
end
end

# Create the signed authentication query string.
# Add this query string to the path WITHOUT prepending the server address.
def prepare_path(verb, path)
uri = URI.parse(path)
query_string = authenticate_query_string(verb, uri.query)
return "#{uri.path}?#{query_string}"
def prepare_path(verb, path, query_params)
query_string = authenticate_query_string(verb, query_params)
return "#{path}?#{query_string}"
end

# 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
def authenticate_query_string(verb, query_string = "")
def authenticate_query_string(verb, query_params = {})
Authentication::QueryString.new(
:verb => verb,
:query_string => query_string,
:query_params => query_params,
:access_key => @access_key,
:secret_access_key => @secret_access_key,
:merchant_id => @merchant_id,
Expand Down
8 changes: 3 additions & 5 deletions lib/amazon/mws/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ class Feed
"Delete"
])

def initialize
def initialize(params = {})
@xml = Builder::XmlMarkup.new
end

def render(params = {})

@xml.instruct!
@xml.AmazonEnvelope("xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation"=>"amzn-envelope.xsd") do
render_envelope
# header
render_header
# message
render_message
end
end
end

def render_header(params = {})
Expand Down
35 changes: 31 additions & 4 deletions lib/amazon/mws/inventory.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
module Amazon
module MWS

# Base API Class
# is this needed??
class API
end

# Intended usage:
#
# AWS::MWS::Base.establish_connection!(config['production'])
#
#
# Amazon::MWS::Inventory.submit_feed(:product=>{})

class Inventory < API
def submit_feed
AWS::MWS::Base.post("/?Action=SubmitFeed&FeedType=", body = ni)
class << self # everything is a static/class method

def submit_feed()
body = Amazon::MWS::Feed.new(??)
response = Amazon::MWS::Base.post("/", {"Action"=>"SubmitFeed", "FeedType"=>"something"}, body)
end

def get_feed_submission_list
response =
Amazon::MWS::Base.post("/", {
"Action" => "GetFeedSubmissionList",
"FeedType" => "something"
})
end

end
end
# Inventory
end
end
end


2 changes: 1 addition & 1 deletion test/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup
end

def test_first
response = AWS::MWS::Base.get("/?Action=GetReportCount")
response = AWS::MWS::Base.get("/", {"Action"=>"GetReportCount"})
puts response
end

Expand Down

0 comments on commit 0dc187d

Please sign in to comment.