Skip to content

Commit

Permalink
Merge 292b5b1 into c2d13da
Browse files Browse the repository at this point in the history
  • Loading branch information
tdtds committed Nov 8, 2019
2 parents c2d13da + 292b5b1 commit aefee7e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
62 changes: 62 additions & 0 deletions lib/aws/pa_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'aws/sig_v4'
require 'json'
require 'net/http'

module AWS
class PAAPI
Market = Struct.new(:host, :region)
MARKETS = {
au: Market.new('webservices.amazon.com.au', 'us-west-2'),
br: Market.new('webservices.amazon.com.br' 'us-east-1'),
ca: Market.new('webservices.amazon.ca', 'us-east-1'),
fr: Market.new('webservices.amazon.fr', 'eu-west-1'),
de: Market.new('webservices.amazon.de', 'eu-west-1'),
in: Market.new('webservices.amazon.in', 'eu-west-1'),
it: Market.new('webservices.amazon.it', 'eu-west-1'),
jp: Market.new('webservices.amazon.co.jp', 'us-west-2'),
mx: Market.new('webservices.amazon.com.mx', 'us-east-1'),
es: Market.new('webservices.amazon.es', 'eu-west-1'),
tr: Market.new('webservices.amazon.com.tr', 'eu-west-1'),
ae: Market.new('webservices.amazon.ae', 'eu-west-1'),
uk: Market.new('webservices.amazon.co.uk', 'eu-west-1'),
us: Market.new('webservices.amazon.com', 'us-east-1'),
}.freeze

def initialize(access_key, secret_key, partner_tag)
@access_key, @secret_key, @partner_tag = access_key, secret_key, partner_tag
end

def get_items(asin, locale)
payload = {
"PartnerTag" => @partner_tag,
"PartnerType" => "Associates",
"Marketplace" => MARKETS[locale].host.sub('webservices', 'www'),
"ItemIds" => [asin],
"Resources" => [
"Images.Primary.Small",
"Images.Primary.Medium",
"Images.Primary.Large",
"ItemInfo.Title",
"Offers.Listings.Price"
]
}.to_json
time_stamp = Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
signature = AWS::SigV4.signature(@secret_key, time_stamp, MARKETS[locale].region, MARKETS[locale].host, payload)
authorization = "AWS4-HMAC-SHA256 Credential=#{@access_key}/#{time_stamp[0,8]}/#{MARKETS[locale].region}/ProductAdvertisingAPI/aws4_request, SignedHeaders=content-encoding;host;x-amz-content-sha256;x-amz-date;x-amz-target, Signature=#{signature}"

headers = {
"X-Amz-Target" => "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems",
"Content-Encoding" => "amz-1.0",
"Host" => MARKETS[locale].host,
"X-Amz-Date" => time_stamp,
"X-Amz-Content-Sha256" => OpenSSL::Digest::SHA256.hexdigest(payload),
"Authorization" => authorization,
"Content-Type" => "application/json; charset=utf-8"
}
uri = URI("https://#{MARKETS[locale].host}/paapi5/getitems")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.post(uri.path, payload, headers)
end
end
end
45 changes: 45 additions & 0 deletions lib/aws/sig_v4.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# A Simple impl of AWS Signature V4 for PA-API GetItems
#
require 'openssl'

module AWS
module SigV4
def self.canonical_request(host, payload, time_stamp)
headers = {
'Content-Encoding' => 'amz-1.0',
'Host' => host,
'X-Amz-Content-Sha256' => OpenSSL::Digest::SHA256.hexdigest(payload),
'X-Amz-Date' => time_stamp,
'X-Amz-Target' => "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems",
}
[
"POST",
"/paapi5/getitems",
'',
headers.keys.sort.map{|k|"#{k.downcase}:#{headers[k]}"}.join("\n") + "\n",
headers.keys.sort.map{|k|k.downcase}.join(';'),
OpenSSL::Digest::SHA256.hexdigest(payload)
].join("\n")
end
def self.string2sign(host, payload, time_stamp)
[
'AWS4-HMAC-SHA256',
time_stamp,
"#{time_stamp[0,8]}/us-west-2/ProductAdvertisingAPI/aws4_request",
OpenSSL::Digest::SHA256.hexdigest(canonical_request(host, payload, time_stamp))
].join("\n")
end
def self.signature(secret, time_stamp, region, host, payload)
k_secret = secret
k_date = hmac("AWS4" + k_secret, time_stamp[0,8])
k_region = hmac(k_date, region)
k_service = hmac(k_region, 'ProductAdvertisingAPI')
k_credential = hmac(k_service, "aws4_request")
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), k_credential, string2sign(host, payload, time_stamp))
end
def self.hmac(key, value)
OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, value)
end
end
end

0 comments on commit aefee7e

Please sign in to comment.