mirror of
https://github.com/dkam/paapi.git
synced 2025-12-28 15:14:52 +00:00
Compare commits
4 Commits
persistent
...
v0.1.9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00e6b251cc | ||
|
|
53ff78f635 | ||
|
|
b4ffb58592 | ||
|
|
2db1d64720 |
@@ -1,4 +1,8 @@
|
|||||||
## 0.1.7 ( Unreleased )
|
## 0.1.9 (Unreleased)
|
||||||
|
- Fix bug allowing non-title case Request Parameters
|
||||||
|
- Add the required PartnerTag and PartnerType key values to the search body request
|
||||||
|
|
||||||
|
## 0.1.7
|
||||||
- Add gem 'net-http-persistent' for connection persistance
|
- Add gem 'net-http-persistent' for connection persistance
|
||||||
- Remove the code which would use other http clients if available
|
- Remove the code which would use other http clients if available
|
||||||
## 0.1.3
|
## 0.1.3
|
||||||
|
|||||||
114
lib/paapi.rb
114
lib/paapi.rb
@@ -1,81 +1,82 @@
|
|||||||
require 'paapi/version'
|
require "paapi/version"
|
||||||
|
|
||||||
require 'paapi/client'
|
require "paapi/client"
|
||||||
require 'paapi/item'
|
require "paapi/item"
|
||||||
require 'paapi/listing'
|
require "paapi/listing"
|
||||||
require 'paapi/response'
|
require "paapi/response"
|
||||||
|
|
||||||
module Paapi
|
module Paapi
|
||||||
class Error < StandardError; end
|
class Error < StandardError; end
|
||||||
|
|
||||||
class NotImplemented < StandardError; end
|
class NotImplemented < StandardError; end
|
||||||
SEARCH_PARAMS = %i[keywords actor, artist, author, brand title].freeze
|
SEARCH_PARAMS = %i[Keywords Actor Artist Author Brand Title].freeze
|
||||||
DEFAULT_PARTNER_TYPE = 'Associates'
|
DEFAULT_PARTNER_TYPE = "Associates"
|
||||||
DEFAULT_MARKET = :us
|
DEFAULT_MARKET = :us
|
||||||
DEFAULT_CONDITION = 'Any'
|
DEFAULT_CONDITION = "Any"
|
||||||
DEFAULT_RESOURCES = [
|
DEFAULT_RESOURCES = [
|
||||||
'Images.Primary.Large',
|
"Images.Primary.Large",
|
||||||
'ItemInfo.ByLineInfo',
|
"ItemInfo.ByLineInfo",
|
||||||
'ItemInfo.ContentInfo',
|
"ItemInfo.ContentInfo",
|
||||||
'ItemInfo.ExternalIds',
|
"ItemInfo.ExternalIds",
|
||||||
'ItemInfo.Features',
|
"ItemInfo.Features",
|
||||||
'ItemInfo.ManufactureInfo',
|
"ItemInfo.ManufactureInfo",
|
||||||
'ItemInfo.ProductInfo',
|
"ItemInfo.ProductInfo",
|
||||||
'ItemInfo.TechnicalInfo', # Includes format when Kindle
|
"ItemInfo.TechnicalInfo", # Includes format when Kindle
|
||||||
'ItemInfo.Title',
|
"ItemInfo.Title",
|
||||||
'ItemInfo.TradeInInfo',
|
"ItemInfo.TradeInInfo",
|
||||||
'Offers.Listings.Availability.Message',
|
"Offers.Listings.Availability.Message",
|
||||||
'Offers.Listings.Condition',
|
"Offers.Listings.Condition",
|
||||||
'Offers.Listings.Condition.SubCondition',
|
"Offers.Listings.Condition.SubCondition",
|
||||||
'Offers.Listings.DeliveryInfo.IsAmazonFulfilled',
|
"Offers.Listings.DeliveryInfo.IsAmazonFulfilled",
|
||||||
'Offers.Listings.DeliveryInfo.IsFreeShippingEligible',
|
"Offers.Listings.DeliveryInfo.IsFreeShippingEligible",
|
||||||
'Offers.Listings.DeliveryInfo.IsPrimeEligible',
|
"Offers.Listings.DeliveryInfo.IsPrimeEligible",
|
||||||
'Offers.Listings.MerchantInfo',
|
"Offers.Listings.MerchantInfo",
|
||||||
'Offers.Listings.Price',
|
"Offers.Listings.Price",
|
||||||
'Offers.Listings.SavingBasis'
|
"Offers.Listings.SavingBasis"
|
||||||
].freeze
|
].freeze
|
||||||
|
|
||||||
Locale = Struct.new(:key, :name, :host, :region) do
|
Locale = Struct.new(:key, :name, :host, :region) do
|
||||||
def site
|
def site
|
||||||
host.sub('webservices', 'www')
|
host.sub("webservices", "www")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
MARKETPLACES = {
|
MARKETPLACES = {
|
||||||
au: Locale.new(:au, 'Australia', 'webservices.amazon.com.au', 'us-west-2'),
|
au: Locale.new(:au, "Australia", "webservices.amazon.com.au", "us-west-2"),
|
||||||
br: Locale.new(:br, 'Brazil', 'webservices.amazon.com.br', 'us-east-1'),
|
br: Locale.new(:br, "Brazil", "webservices.amazon.com.br", "us-east-1"),
|
||||||
ca: Locale.new(:ca, 'Canada', 'webservices.amazon.ca', 'us-east-1'),
|
ca: Locale.new(:ca, "Canada", "webservices.amazon.ca", "us-east-1"),
|
||||||
fr: Locale.new(:fr, 'France', 'webservices.amazon.fr', 'eu-west-1'),
|
fr: Locale.new(:fr, "France", "webservices.amazon.fr", "eu-west-1"),
|
||||||
de: Locale.new(:de, 'Germany', 'webservices.amazon.de', 'eu-west-1'),
|
de: Locale.new(:de, "Germany", "webservices.amazon.de", "eu-west-1"),
|
||||||
in: Locale.new(:in, 'India', 'webservices.amazon.in', 'eu-west-1'),
|
in: Locale.new(:in, "India", "webservices.amazon.in", "eu-west-1"),
|
||||||
it: Locale.new(:it, 'Italy', 'webservices.amazon.it', 'eu-west-1'),
|
it: Locale.new(:it, "Italy", "webservices.amazon.it", "eu-west-1"),
|
||||||
jp: Locale.new(:jp, 'Japan', 'webservices.amazon.co.jp', 'us-west-2'),
|
jp: Locale.new(:jp, "Japan", "webservices.amazon.co.jp", "us-west-2"),
|
||||||
mx: Locale.new(:mx, 'Mexico', 'webservices.amazon.com.mx', 'us-east-1'),
|
mx: Locale.new(:mx, "Mexico", "webservices.amazon.com.mx", "us-east-1"),
|
||||||
es: Locale.new(:es, 'Spain', 'webservices.amazon.es', 'eu-west-1'),
|
es: Locale.new(:es, "Spain", "webservices.amazon.es", "eu-west-1"),
|
||||||
tr: Locale.new(:tk, 'Turkey', 'webservices.amazon.com.tr', 'eu-west-1'),
|
tr: Locale.new(:tk, "Turkey", "webservices.amazon.com.tr", "eu-west-1"),
|
||||||
ae: Locale.new(:ae, 'United Arab Emirates', 'webservices.amazon.ae', 'eu-west-1'),
|
ae: Locale.new(:ae, "United Arab Emirates", "webservices.amazon.ae", "eu-west-1"),
|
||||||
uk: Locale.new(:uk, 'United Kingdom', 'webservices.amazon.co.uk', 'eu-west-1'),
|
uk: Locale.new(:uk, "United Kingdom", "webservices.amazon.co.uk", "eu-west-1"),
|
||||||
us: Locale.new(:us, 'United States', 'webservices.amazon.com', 'us-east-1'),
|
us: Locale.new(:us, "United States", "webservices.amazon.com", "us-east-1")
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
Operation = Struct.new(:target_name, :endpoint_suffix, :http_method, :service )
|
Operation = Struct.new(:target_name, :endpoint_suffix, :http_method, :service)
|
||||||
|
|
||||||
OPERATIONS = {
|
OPERATIONS = {
|
||||||
get_browse_nodes: Operation.new( 'GetBrowseNodes', 'getbrowsenodes', 'POST', 'ProductAdvertisingAPI' ),
|
get_browse_nodes: Operation.new("GetBrowseNodes", "getbrowsenodes", "POST", "ProductAdvertisingAPI"),
|
||||||
get_items: Operation.new( 'GetItems', 'getitems', 'POST', 'ProductAdvertisingAPI' ),
|
get_items: Operation.new("GetItems", "getitems", "POST", "ProductAdvertisingAPI"),
|
||||||
get_variations: Operation.new( 'GetVariations', 'getvariations', 'POST', 'ProductAdvertisingAPI' ),
|
get_variations: Operation.new("GetVariations", "getvariations", "POST", "ProductAdvertisingAPI"),
|
||||||
search_items: Operation.new( 'SearchItems', 'searchitems', 'POST', 'ProductAdvertisingAPI' )
|
search_items: Operation.new("SearchItems", "searchitems", "POST", "ProductAdvertisingAPI")
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :access_key,
|
attr_accessor :access_key,
|
||||||
:secret_key,
|
:secret_key,
|
||||||
:partner_tag,
|
:partner_tag,
|
||||||
:partner_type,
|
:partner_type,
|
||||||
:market,
|
:market,
|
||||||
:partner_market,
|
:partner_market,
|
||||||
:condition,
|
:condition,
|
||||||
:resources,
|
:resources,
|
||||||
:test_mode
|
:test_mode
|
||||||
|
|
||||||
def configure
|
def configure
|
||||||
yield self
|
yield self
|
||||||
@@ -85,7 +86,6 @@ module Paapi
|
|||||||
end
|
end
|
||||||
|
|
||||||
def symbolize_keys(hash)
|
def symbolize_keys(hash)
|
||||||
Hash[hash.map{|k,v| v.is_a?(Hash) ? [k.to_sym, symbolize_keys(v)] : [k.to_sym, v] }]
|
hash.map { |k, v| v.is_a?(Hash) ? [k.to_sym, symbolize_keys(v)] : [k.to_sym, v] }.to_h
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ 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
|
||||||
|
|
||||||
@@ -21,7 +20,7 @@ module Paapi
|
|||||||
@secret_key = secret_key
|
@secret_key = secret_key
|
||||||
@partner_type = partner_type
|
@partner_type = partner_type
|
||||||
@resources = resources unless resources.nil?
|
@resources = resources unless resources.nil?
|
||||||
@condition = condition
|
@condition = condition
|
||||||
self.market = market
|
self.market = market
|
||||||
@partner_tag = partner_tag if !partner_tag.nil?
|
@partner_tag = partner_tag if !partner_tag.nil?
|
||||||
|
|
||||||
@@ -31,13 +30,13 @@ module Paapi
|
|||||||
def market=(a_market)
|
def market=(a_market)
|
||||||
@market = a_market
|
@market = a_market
|
||||||
@marketplace = MARKETPLACES[market.to_sym]
|
@marketplace = MARKETPLACES[market.to_sym]
|
||||||
if !Paapi.partner_market.nil?
|
return if Paapi.partner_market.nil?
|
||||||
@partner_tag = Paapi.partner_market.dig(a_market.to_sym) || @partner_tag
|
|
||||||
end
|
@partner_tag = Paapi.partner_market[a_market.to_sym] || @partner_tag
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_items(item_ids:, **options)
|
def get_items(item_ids:, **options)
|
||||||
payload = { 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
|
||||||
|
|
||||||
@@ -47,12 +46,12 @@ module Paapi
|
|||||||
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(keywords: nil, **options )
|
def search_items(**options )
|
||||||
raise ArgumentError("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 = { Keywords: keywords, 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
|
||||||
@@ -110,12 +109,11 @@ module Paapi
|
|||||||
|
|
||||||
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
|
||||||
|
|
||||||
http.request uri, post_request
|
http.request uri, post_request
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module Paapi
|
module Paapi
|
||||||
VERSION = '0.1.8'
|
VERSION = '0.1.9'
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
|
|||||||
spec.add_development_dependency 'rake', '>= 12.3.r3'
|
spec.add_development_dependency 'rake', '>= 12.3.r3'
|
||||||
spec.add_development_dependency 'minitest', '~> 5.0'
|
spec.add_development_dependency 'minitest', '~> 5.0'
|
||||||
spec.add_development_dependency 'byebug', '~> 11'
|
spec.add_development_dependency 'byebug', '~> 11'
|
||||||
spec.add_development_dependency 'awesome_print', '~> 1.8'
|
spec.add_development_dependency 'standard'
|
||||||
|
|
||||||
spec.add_dependency 'aws-sigv4', '~> 1'
|
spec.add_dependency 'aws-sigv4', '~> 1'
|
||||||
spec.add_dependency 'net-http-persistent', '~> 4.0', '>= 4.0.1'
|
spec.add_dependency 'net-http-persistent', '~> 4.0', '>= 4.0.1'
|
||||||
|
|||||||
Reference in New Issue
Block a user