10 Commits

Author SHA1 Message Date
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
316e71ee46 Merge branch 'master' into add_condition 2020-08-21 22:38:57 +10:00
side Feng
8bd8ba1e84 Add Condition Parameter 2020-08-11 14:48:33 +08:00
6 changed files with 35 additions and 12 deletions

View File

@@ -1,3 +1,7 @@
## 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

View File

@@ -11,6 +11,7 @@ module Paapi
SEARCH_PARAMS = %i[keywords actor, artist, author, brand title].freeze
DEFAULT_PARTNER_TYPE = 'Associates'
DEFAULT_MARKET = :us
DEFAULT_CONDITION = 'Any'
DEFAULT_RESOURCES = [
'Images.Primary.Large',
'ItemInfo.ByLineInfo',
@@ -72,6 +73,7 @@ module Paapi
:partner_type,
:market,
:partner_market,
:condition,
:resources,
:test_mode

View File

@@ -1,16 +1,18 @@
require 'net/http'
require 'aws-sigv4'
require 'byebug'
module Paapi
class Client
attr_accessor :partner_tag, :marketplace, :resources
attr_reader :partner_type, :access_key, :secret_key, :market
attr_accessor :partner_tag, :marketplace, :resources, :condition
attr_reader :partner_type, :access_key, :secret_key, :market, :http
def initialize(access_key: Paapi.access_key,
secret_key: Paapi.secret_key,
partner_tag: Paapi.partner_tag,
market: Paapi.market || DEFAULT_MARKET,
condition: Paapi.condition || DEFAULT_CONDITION,
resources: Paapi.resources || DEFAULT_RESOURCES,
partner_type: DEFAULT_PARTNER_TYPE
)
@@ -20,9 +22,17 @@ module Paapi
@secret_key = secret_key
@partner_type = partner_type
@resources = resources unless resources.nil?
@condition = condition
self.market = market
@partner_tag = partner_tag if !partner_tag.nil?
if defined?(HTTPX)
@http = HTTPX.plugin(:persistent)
elsif defined?(HTTP)
@http = HTTP::Client.new
else
@http = nil
end
end
def market=(a_market)
@@ -43,7 +53,7 @@ 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 ]
# TODO: Currently we assume Keywords, but we need one of the following: [Keywords Actor Artist Author Brand Title ]
def search_items(keywords: nil, **options )
raise ArgumentError("Missing keywords") unless (options.keys | SEARCH_PARAMS).length.positive?
@@ -62,6 +72,7 @@ module Paapi
private
def request(op:, payload:)
byebug
raise ArguemntError unless Paapi::OPERATIONS.keys.include?(op)
operation = OPERATIONS[op]
@@ -72,6 +83,7 @@ module Paapi
}
default_payload = {
'Condition' => condition,
'PartnerTag' => partner_tag,
'PartnerType' => partner_type,
'Marketplace' => marketplace.site
@@ -98,7 +110,12 @@ module Paapi
headers['Authorization'] = signature.headers['authorization']
headers['Content-Type'] = 'application/json; charset=utf-8'
Response.new( Client.post(url: endpoint, body: payload, headers: headers))
unless @http.nil?
Response.new( @http.with_headers(headers).post(endpoint, json: payload ) )
else
Response.new( Client.post(url: endpoint, body: payload, headers: headers))
end
end
def self.post(url:, body:, headers:)

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.4'
end