26 Commits

Author SHA1 Message Date
Dan Milne
00e6b251cc Formatting - StandardRB updates 2023-07-14 17:45:26 +10:00
Dan Milne
53ff78f635 StandardRB 2023-06-08 12:08:54 +10:00
Dan Milne
b4ffb58592 Include PartnerTag and PartnerType 2023-06-05 12:48:32 +10:00
Dan Milne
2db1d64720 Remove old gem 2023-04-20 19:32:42 +10:00
Dan Milne
09e635fc6f Version bump. Make sure the console uses local paapi 2023-04-20 15:41:26 +10:00
Dan Milne
9297799555 Add net-http-persistent as a dependency. Remove code to determine which HTTP client to use. Cleanup spacing 2022-11-01 18:04:09 +11:00
Dan Milne
5d8fd3ffff Version bump with bug fix 2022-09-04 14:12:14 +10:00
Dan Milne
bba10e75fa Merge pull request #8 from crisdias/master
Missing comma in MARKETPLACES.
2022-09-04 14:11:32 +10:00
Cris Dias
72b413a40e Missing comma in MARKETPLACES.
Requests were failing for Brazil.
2022-09-03 11:26:53 -03:00
Dan Milne
ae3577a9fc Bump version 2022-08-10 12:09:06 +10:00
Dan Milne
11694b567e More safe operator 2022-08-10 12:07:38 +10:00
Dan Milne
a6a5cf45c3 Remove byebug 2022-08-10 12:04:31 +10:00
Dan Milne
761df379cf Fix depracation message 2022-08-10 12:02:45 +10:00
Dan Milne
6105ea621c Debugging 2022-08-10 12:00:10 +10:00
Dan Milne
d09be5f131 Migrate to just HTTP 2022-08-10 11:52:18 +10:00
Dan Milne
7aebd186a5 Bugfixing 2022-08-10 11:47:30 +10:00
Dan Milne
e04d258b07 Testing use of native http 2022-08-10 11:42:28 +10:00
Dan Milne
b66dda8d79 Use safe operator 2022-08-10 11:31:18 +10:00
Dan Milne
02057fb0d0 Typo 2020-08-26 14:22:32 +10:00
Dan Milne
8c4f4337a3 Merge branch 'add_condition' into master 2020-08-21 22:55:07 +10:00
Dan Milne
ba6a52ce9c Paapi will always use HTTPS 2020-08-21 18:06:00 +10:00
Dan Milne
747e76fe8f Require net/http and set the body to JSON 2020-08-21 17:58:56 +10:00
Dan Milne
6115f72119 Move the HTTP request code into a method and remove references to the HTTP gem 2020-08-21 17:45:47 +10:00
Dan Milne
e47add0064 Comment out unused Gem 2020-08-21 16:58:18 +10:00
Dan Milne
a86acb1ef9 Merge branch 'master' into net_http 2020-08-21 16:55:54 +10:00
Dan Milne
706a9fb377 Investigate a plain net::http client for less dependancies 2020-01-10 15:37:36 +11:00
8 changed files with 116 additions and 85 deletions

View File

@@ -1,3 +1,14 @@
## 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
- Remove the code which would use other http clients if available
## 0.1.3
- Dropped the HTTP gem and moved to Ruby's Net::HTTP
- Merged a branch which adds the Condtion parameter.
## 0.1.2
- Use Contributor RoleType, rather than Role. RoleType uses lowercase, rather than capitalised.

View File

@@ -4,7 +4,7 @@
[![Build Status](https://travis-ci.org/dkam/paapi.svg?branch=master)](https://travis-ci.org/dkam/paapi)
If this gem doesn't meet your needs, try the [Vacumm gem](https://github.com/hakanensari/vacuum).
If this gem doesn't meet your needs, try the [Vacuum gem](https://github.com/hakanensari/vacuum).
## Installation
@@ -87,6 +87,14 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
If you create a file `config.rb`, it will be loaded by `bin/console`, allowing you to configure keys and markets.
```ruby
Paapi.configure do |config|
config.access_key = 'access_key'
config.secret_key = 'secret_key'
config.partner_market = {au: 'au_tag', us: 'us_tag', uk: 'uk_tag', ca: 'ca_tag_'}
end
```
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing

View File

@@ -1,7 +1,8 @@
#!/usr/bin/env ruby
require 'bundler/setup'
require 'paapi'
#require 'paapi'
require './lib/paapi.rb'
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

View File

@@ -1,69 +1,70 @@
require 'paapi/version'
require "paapi/version"
require 'paapi/client'
require 'paapi/item'
require 'paapi/listing'
require 'paapi/response'
require "paapi/client"
require "paapi/item"
require "paapi/listing"
require "paapi/response"
module Paapi
class Error < StandardError; end
class NotImplemented < StandardError; end
SEARCH_PARAMS = %i[keywords actor, artist, author, brand title].freeze
DEFAULT_PARTNER_TYPE = 'Associates'
SEARCH_PARAMS = %i[Keywords Actor Artist Author Brand Title].freeze
DEFAULT_PARTNER_TYPE = "Associates"
DEFAULT_MARKET = :us
DEFAULT_CONDITION = 'Any'
DEFAULT_CONDITION = "Any"
DEFAULT_RESOURCES = [
'Images.Primary.Large',
'ItemInfo.ByLineInfo',
'ItemInfo.ContentInfo',
'ItemInfo.ExternalIds',
'ItemInfo.Features',
'ItemInfo.ManufactureInfo',
'ItemInfo.ProductInfo',
'ItemInfo.TechnicalInfo', # Includes format when Kindle
'ItemInfo.Title',
'ItemInfo.TradeInInfo',
'Offers.Listings.Availability.Message',
'Offers.Listings.Condition',
'Offers.Listings.Condition.SubCondition',
'Offers.Listings.DeliveryInfo.IsAmazonFulfilled',
'Offers.Listings.DeliveryInfo.IsFreeShippingEligible',
'Offers.Listings.DeliveryInfo.IsPrimeEligible',
'Offers.Listings.MerchantInfo',
'Offers.Listings.Price',
'Offers.Listings.SavingBasis'
"Images.Primary.Large",
"ItemInfo.ByLineInfo",
"ItemInfo.ContentInfo",
"ItemInfo.ExternalIds",
"ItemInfo.Features",
"ItemInfo.ManufactureInfo",
"ItemInfo.ProductInfo",
"ItemInfo.TechnicalInfo", # Includes format when Kindle
"ItemInfo.Title",
"ItemInfo.TradeInInfo",
"Offers.Listings.Availability.Message",
"Offers.Listings.Condition",
"Offers.Listings.Condition.SubCondition",
"Offers.Listings.DeliveryInfo.IsAmazonFulfilled",
"Offers.Listings.DeliveryInfo.IsFreeShippingEligible",
"Offers.Listings.DeliveryInfo.IsPrimeEligible",
"Offers.Listings.MerchantInfo",
"Offers.Listings.Price",
"Offers.Listings.SavingBasis"
].freeze
Locale = Struct.new(:key, :name, :host, :region) do
def site
host.sub('webservices', 'www')
host.sub("webservices", "www")
end
end
MARKETPLACES = {
au: Locale.new(:au, 'Australia', 'webservices.amazon.com.au', 'us-west-2'),
br: Locale.new(:br, 'Brazil', 'webservices.amazon.com.br' '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'),
de: Locale.new(:de, 'Germany', 'webservices.amazon.de', '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'),
jp: Locale.new(:jp, 'Japan', 'webservices.amazon.co.jp', 'us-west-2'),
mx: Locale.new(:mx, 'Mexico', 'webservices.amazon.com.mx', 'us-east-1'),
es: Locale.new(:es, 'Spain', 'webservices.amazon.es', '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'),
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'),
au: Locale.new(:au, "Australia", "webservices.amazon.com.au", "us-west-2"),
br: Locale.new(:br, "Brazil", "webservices.amazon.com.br", "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"),
de: Locale.new(:de, "Germany", "webservices.amazon.de", "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"),
jp: Locale.new(:jp, "Japan", "webservices.amazon.co.jp", "us-west-2"),
mx: Locale.new(:mx, "Mexico", "webservices.amazon.com.mx", "us-east-1"),
es: Locale.new(:es, "Spain", "webservices.amazon.es", "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"),
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")
}.freeze
Operation = Struct.new(:target_name, :endpoint_suffix, :http_method, :service)
OPERATIONS = {
get_browse_nodes: Operation.new( 'GetBrowseNodes', 'getbrowsenodes', 'POST', 'ProductAdvertisingAPI' ),
get_items: Operation.new( 'GetItems', 'getitems', 'POST', 'ProductAdvertisingAPI' ),
get_variations: Operation.new( 'GetVariations', 'getvariations', 'POST', 'ProductAdvertisingAPI' ),
search_items: Operation.new( 'SearchItems', 'searchitems', 'POST', 'ProductAdvertisingAPI' )
get_browse_nodes: Operation.new("GetBrowseNodes", "getbrowsenodes", "POST", "ProductAdvertisingAPI"),
get_items: Operation.new("GetItems", "getitems", "POST", "ProductAdvertisingAPI"),
get_variations: Operation.new("GetVariations", "getvariations", "POST", "ProductAdvertisingAPI"),
search_items: Operation.new("SearchItems", "searchitems", "POST", "ProductAdvertisingAPI")
}.freeze
class << self
@@ -85,7 +86,6 @@ module Paapi
end
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

View File

@@ -1,11 +1,10 @@
require 'http'
require 'net/http/persistent'
require 'aws-sigv4'
module Paapi
class Client
attr_accessor :partner_tag, :marketplace, :resources, :condition
attr_reader :partner_type, :access_key, :secret_key, :market
attr_reader :partner_type, :access_key, :secret_key, :market, :http
def initialize(access_key: Paapi.access_key,
secret_key: Paapi.secret_key,
@@ -24,18 +23,20 @@ module Paapi
@condition = condition
self.market = market
@partner_tag = partner_tag if !partner_tag.nil?
@http = Net::HTTP::Persistent.new name: 'paapi'
end
def market=(a_market)
@market = a_market
@marketplace = MARKETPLACES[market.to_sym]
if !Paapi.partner_market.nil?
@partner_tag = Paapi.partner_market.dig(a_market.to_sym) || @partner_tag
end
return if Paapi.partner_market.nil?
@partner_tag = Paapi.partner_market[a_market.to_sym] || @partner_tag
end
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)
end
@@ -44,13 +45,13 @@ module Paapi
request(op: :get_variations, payload: payload)
end
# TODO: Currently we assume Keywords, but we need one of the follow: [Keywords Actor Artist Author Brand Title ]
def search_items(keywords: nil, **options )
raise ArgumentError("Missing keywords") unless (options.keys | SEARCH_PARAMS).length.positive?
# TODO: Currently we assume Keywords, but we need one of the following: [Keywords Actor Artist Author Brand Title ]
def search_items(**options )
raise ArgumentError.new("Missing keywords") unless (options.keys & SEARCH_PARAMS).length.positive?
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)
end
@@ -100,9 +101,19 @@ module Paapi
headers['Authorization'] = signature.headers['authorization']
headers['Content-Type'] = 'application/json; charset=utf-8'
Response.new( HTTP.headers(headers).post(endpoint, json: payload ) )
end
Response.new( post(url: endpoint, body: payload, headers: headers))
end
def post(url:, body:, headers:)
uri = URI.parse(url)
post_request = Net::HTTP::Post.new(uri.path)
post_request.content_type = 'application/json; charset=UTF-8'
headers.each { |k, v| post_request[k] = v }
post_request.body = body.to_json
http.request uri, post_request
end
end
end

View File

@@ -128,22 +128,22 @@ module Paapi
def height
data = get(%w{ItemInfo ProductInfo ItemDimensions Height})
[data.dig('DisplayValue'), data.dig('Unit')].join(' ')
[data&.dig('DisplayValue'), data&.dig('Unit')].join(' ')
end
def length
data = get(%w{ItemInfo ProductInfo ItemDimensions Length})
[data.dig('DisplayValue'), data.dig('Unit')].join(' ')
[data&.dig('DisplayValue'), data&.dig('Unit')].join(' ')
end
def width
data = get(%w{ItemInfo ProductInfo ItemDimensions Width})
[data.dig('DisplayValue'), data.dig('Unit')].join(' ')
[data&.dig('DisplayValue'), data&.dig('Unit')].join(' ')
end
def weight
data = get(%w{ItemInfo ProductInfo ItemDimensions Weight})
[data.dig('DisplayValue'), data.dig('Unit')].join(' ')
[data&.dig('DisplayValue'), data&.dig('Unit')].join(' ')
end
def kindle?

View File

@@ -1,3 +1,3 @@
module Paapi
VERSION = '0.1.2'
VERSION = '0.1.9'
end

View File

@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
#spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
spec.metadata["changelog_uri"] = "https://github.com/dkam/paapi/blob/master/CHANGELOG.md"
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -29,8 +29,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake', '>= 12.3.r3'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'byebug', '~> 11'
spec.add_development_dependency 'awesome_print', '~> 1.8'
spec.add_development_dependency 'standard'
spec.add_dependency 'http', '~> 4'
spec.add_dependency 'aws-sigv4', '~> 1'
spec.add_dependency 'net-http-persistent', '~> 4.0', '>= 4.0.1'
end