|
|
|
@@ -1,12 +1,13 @@
|
|
|
|
require 'net/http/persistent'
|
|
|
|
require "net/http/persistent"
|
|
|
|
require 'aws-sigv4'
|
|
|
|
require "aws-sigv4"
|
|
|
|
|
|
|
|
|
|
|
|
module Paapi
|
|
|
|
module Paapi
|
|
|
|
class Client
|
|
|
|
class Client
|
|
|
|
attr_accessor :partner_tag, :marketplace, :resources, :condition
|
|
|
|
attr_accessor :partner_tag, :marketplace, :resources, :condition
|
|
|
|
attr_reader :partner_type, :access_key, :secret_key, :market, :http
|
|
|
|
attr_reader :partner_type, :access_key, :secret_key, :market, :http
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(access_key: Paapi.access_key,
|
|
|
|
def initialize(
|
|
|
|
|
|
|
|
access_key: Paapi.access_key,
|
|
|
|
secret_key: Paapi.secret_key,
|
|
|
|
secret_key: Paapi.secret_key,
|
|
|
|
partner_tag: Paapi.partner_tag,
|
|
|
|
partner_tag: Paapi.partner_tag,
|
|
|
|
market: Paapi.market || DEFAULT_MARKET,
|
|
|
|
market: Paapi.market || DEFAULT_MARKET,
|
|
|
|
@@ -14,7 +15,7 @@ module Paapi
|
|
|
|
resources: Paapi.resources || DEFAULT_RESOURCES,
|
|
|
|
resources: Paapi.resources || DEFAULT_RESOURCES,
|
|
|
|
partner_type: DEFAULT_PARTNER_TYPE
|
|
|
|
partner_type: DEFAULT_PARTNER_TYPE
|
|
|
|
)
|
|
|
|
)
|
|
|
|
raise ArgumentError unless MARKETPLACES.keys.include?(market.to_sym)
|
|
|
|
raise ArgumentError unless MARKETPLACES.key?(market.to_sym)
|
|
|
|
|
|
|
|
|
|
|
|
@access_key = access_key
|
|
|
|
@access_key = access_key
|
|
|
|
@secret_key = secret_key
|
|
|
|
@secret_key = secret_key
|
|
|
|
@@ -24,7 +25,11 @@ module Paapi
|
|
|
|
self.market = market
|
|
|
|
self.market = market
|
|
|
|
@partner_tag = partner_tag if !partner_tag.nil?
|
|
|
|
@partner_tag = partner_tag if !partner_tag.nil?
|
|
|
|
|
|
|
|
|
|
|
|
@http = Net::HTTP::Persistent.new name: 'paapi'
|
|
|
|
@http = Net::HTTP::Persistent.new(name: "paapi").tap do |c|
|
|
|
|
|
|
|
|
c.open_timeout = 2
|
|
|
|
|
|
|
|
c.read_timeout = 5
|
|
|
|
|
|
|
|
c.write_timeout = 5
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def market=(a_market)
|
|
|
|
def market=(a_market)
|
|
|
|
@@ -36,48 +41,49 @@ module Paapi
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def get_items(item_ids:, **options)
|
|
|
|
def get_items(item_ids:, **options)
|
|
|
|
payload = { PartnerTag: partner_tag, PartnerType: 'Associates', ItemIds: Array(item_ids), Resources: @resources }.merge(options)
|
|
|
|
payload = {PartnerTag: partner_tag, PartnerType: "Associates", ItemIds: Array(item_ids), Resources: @resources}.merge(options)
|
|
|
|
request(op: :get_items, payload: payload)
|
|
|
|
request(op: :get_items, payload: payload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def get_variations(asin:, **options )
|
|
|
|
def get_variations(asin:, **options)
|
|
|
|
payload = { ASIN: asin, Resources: @resources }.merge(options)
|
|
|
|
payload = {ASIN: asin, Resources: @resources}.merge(options)
|
|
|
|
request(op: :get_variations, payload: payload)
|
|
|
|
request(op: :get_variations, payload: payload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: Currently we assume Keywords, but we need one of the following: [Keywords Actor Artist Author Brand Title ]
|
|
|
|
# TODO: Currently we assume Keywords, but we need one of the following: [Keywords Actor Artist Author Brand Title ]
|
|
|
|
def search_items(**options )
|
|
|
|
def search_items(**options)
|
|
|
|
|
|
|
|
options.transform_keys!(&:to_s)
|
|
|
|
raise ArgumentError.new("Missing keywords") unless (options.keys & SEARCH_PARAMS).length.positive?
|
|
|
|
raise ArgumentError.new("Missing keywords") unless (options.keys & SEARCH_PARAMS).length.positive?
|
|
|
|
|
|
|
|
|
|
|
|
search_index = options.dig(:SearchIndex) ||'All'
|
|
|
|
search_index = options.dig(:SearchIndex) || "All"
|
|
|
|
|
|
|
|
|
|
|
|
payload = { PartnerTag: partner_tag, PartnerType: 'Associates', Resources: @resources, ItemCount: 10, ItemPage: 1, SearchIndex: search_index }.merge(options)
|
|
|
|
payload = {"PartnerTag" => partner_tag, "PartnerType" => "Associates", "Resources" => @resources, "ItemCount" => 10, "ItemPage" => 1, "SearchIndex" => search_index}.merge(options)
|
|
|
|
|
|
|
|
|
|
|
|
request(op: :search_items, payload: payload)
|
|
|
|
request(op: :search_items, payload: payload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def get_browse_nodes(browse_node_ids:, **options)
|
|
|
|
def get_browse_nodes(browse_node_ids:, **options)
|
|
|
|
payload = { BrowseNodeIds: Array(browse_node_ids), Resources: @resources }.merge(options)
|
|
|
|
payload = {BrowseNodeIds: Array(browse_node_ids), Resources: @resources}.merge(options)
|
|
|
|
request(op: :get_browse_nodes, payload: payload)
|
|
|
|
request(op: :get_browse_nodes, payload: payload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
def request(op:, payload:)
|
|
|
|
def request(op:, payload:)
|
|
|
|
raise ArguemntError unless Paapi::OPERATIONS.keys.include?(op)
|
|
|
|
raise ArguemntError unless Paapi::OPERATIONS.key?(op)
|
|
|
|
|
|
|
|
|
|
|
|
operation = OPERATIONS[op]
|
|
|
|
operation = OPERATIONS[op]
|
|
|
|
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
headers = {
|
|
|
|
'X-Amz-Target' => "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.#{operation.target_name}",
|
|
|
|
"X-Amz-Target" => "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.#{operation.target_name}",
|
|
|
|
'Content-Encoding' => 'amz-1.0',
|
|
|
|
"Content-Encoding" => "amz-1.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
default_payload = {
|
|
|
|
default_payload = {
|
|
|
|
'Condition' => condition,
|
|
|
|
"Condition" => condition,
|
|
|
|
'PartnerTag' => partner_tag,
|
|
|
|
"PartnerTag" => partner_tag,
|
|
|
|
'PartnerType' => partner_type,
|
|
|
|
"PartnerType" => partner_type,
|
|
|
|
'Marketplace' => marketplace.site
|
|
|
|
"Marketplace" => marketplace.site
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
payload = default_payload.merge(payload)
|
|
|
|
payload = default_payload.merge(payload)
|
|
|
|
@@ -95,20 +101,20 @@ module Paapi
|
|
|
|
|
|
|
|
|
|
|
|
signature = signer.sign_request(http_method: operation.http_method, url: endpoint, headers: headers, body: payload.to_json)
|
|
|
|
signature = signer.sign_request(http_method: operation.http_method, url: endpoint, headers: headers, body: payload.to_json)
|
|
|
|
|
|
|
|
|
|
|
|
headers['Host'] = marketplace.host
|
|
|
|
headers["Host"] = marketplace.host
|
|
|
|
headers['X-Amz-Date'] = signature.headers['x-amz-date']
|
|
|
|
headers["X-Amz-Date"] = signature.headers["x-amz-date"]
|
|
|
|
headers['X-Amz-Content-Sha256']= signature.headers['x-amz-content-sha256']
|
|
|
|
headers["X-Amz-Content-Sha256"] = signature.headers["x-amz-content-sha256"]
|
|
|
|
headers['Authorization'] = signature.headers['authorization']
|
|
|
|
headers["Authorization"] = signature.headers["authorization"]
|
|
|
|
headers['Content-Type'] = 'application/json; charset=utf-8'
|
|
|
|
headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
|
|
|
|
|
|
|
|
Response.new( post(url: endpoint, body: payload, headers: headers))
|
|
|
|
Response.new(post(url: endpoint, body: payload, headers: headers))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def post(url:, body:, headers:)
|
|
|
|
def post(url:, body:, headers:)
|
|
|
|
uri = URI.parse(url)
|
|
|
|
uri = URI.parse(url)
|
|
|
|
|
|
|
|
|
|
|
|
post_request = Net::HTTP::Post.new(uri.path)
|
|
|
|
post_request = Net::HTTP::Post.new(uri.path)
|
|
|
|
post_request.content_type = 'application/json; charset=UTF-8'
|
|
|
|
post_request.content_type = "application/json; charset=UTF-8"
|
|
|
|
|
|
|
|
|
|
|
|
headers.each { |k, v| post_request[k] = v }
|
|
|
|
headers.each { |k, v| post_request[k] = v }
|
|
|
|
post_request.body = body.to_json
|
|
|
|
post_request.body = body.to_json
|
|
|
|
|