14 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
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
6 changed files with 41 additions and 14 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 ## 0.1.2
- Use Contributor RoleType, rather than Role. RoleType uses lowercase, rather than capitalised. - 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) [![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 ## Installation

View File

@@ -1,11 +1,12 @@
require 'http' require 'net/http'
require 'aws-sigv4' require 'aws-sigv4'
require 'byebug'
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 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,
@@ -24,6 +25,14 @@ module Paapi
@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?
if defined?(HTTPX)
@http = HTTPX.plugin(:persistent)
elsif defined?(HTTP)
@http = HTTP::Client.new
else
@http = nil
end
end end
def market=(a_market) def market=(a_market)
@@ -44,7 +53,7 @@ module Paapi
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 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 ) def search_items(keywords: nil, **options )
raise ArgumentError("Missing keywords") unless (options.keys | SEARCH_PARAMS).length.positive? raise ArgumentError("Missing keywords") unless (options.keys | SEARCH_PARAMS).length.positive?
@@ -63,6 +72,7 @@ module Paapi
private private
def request(op:, payload:) def request(op:, payload:)
byebug
raise ArguemntError unless Paapi::OPERATIONS.keys.include?(op) raise ArguemntError unless Paapi::OPERATIONS.keys.include?(op)
operation = OPERATIONS[op] operation = OPERATIONS[op]
@@ -100,9 +110,23 @@ module Paapi
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( HTTP.headers(headers).post(endpoint, json: payload ) ) 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:)
uri = URI.parse(url)
request = Net::HTTP::Post.new(uri)
request.content_type = 'application/json; charset=UTF-8'
headers.each { |k, v| request[k] = v }
request.body = body.to_json
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
end end
end end
end end

View File

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

View File

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

View File

@@ -31,6 +31,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'byebug', '~> 11' spec.add_development_dependency 'byebug', '~> 11'
spec.add_development_dependency 'awesome_print', '~> 1.8' spec.add_development_dependency 'awesome_print', '~> 1.8'
spec.add_dependency 'http', '~> 4'
spec.add_dependency 'aws-sigv4', '~> 1' spec.add_dependency 'aws-sigv4', '~> 1'
end end