From b30f5bc697d9f51eb511fb959610a8b0e95a4b8b Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Sat, 7 Sep 2019 17:38:18 +1000 Subject: [PATCH] Move to using Amazon Signer, rather than reimplementing. --- Gemfile.lock | 30 ++++++++++++------- aws_paa.gemspec | 7 +++-- lib/aws_paa.rb | 3 +- lib/aws_paa/client.rb | 31 +++++++++++++++++-- lib/aws_paa/request.rb | 67 +++++++++++++++++++++++------------------- test/test_helper.rb | 2 +- 6 files changed, 91 insertions(+), 49 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index eb92d80..07dc881 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,27 +2,33 @@ PATH remote: . specs: aws_paa (0.1.0) - rest-client (~> 2.0) + aws-sigv4 (~> 1) + http (~> 0.1) GEM remote: https://rubygems.org/ specs: + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) + awesome_print (1.8.0) + aws-eventstream (1.0.3) + aws-sigv4 (1.1.0) + aws-eventstream (~> 1.0, >= 1.0.2) + byebug (11.0.1) domain_name (0.5.20190701) unf (>= 0.0.5, < 1.0.0) - http-accept (1.7.0) + http (0.9.9) + addressable (~> 2.3) + http-cookie (~> 1.0) + http-form_data (~> 1.0.1) + http_parser.rb (~> 0.6.0) http-cookie (1.0.3) domain_name (~> 0.5) - mime-types (3.2.2) - mime-types-data (~> 3.2015) - mime-types-data (3.2019.0331) + http-form_data (1.0.3) + http_parser.rb (0.6.0) minitest (5.11.3) - netrc (0.11.0) + public_suffix (4.0.1) rake (10.5.0) - rest-client (2.1.0) - http-accept (>= 1.7.0, < 2.0) - http-cookie (>= 1.0.2, < 2.0) - mime-types (>= 1.16, < 4.0) - netrc (~> 0.8) unf (0.1.4) unf_ext unf_ext (0.0.7.6) @@ -31,8 +37,10 @@ PLATFORMS ruby DEPENDENCIES + awesome_print aws_paa! bundler (~> 2.0) + byebug minitest (~> 5.0) rake (~> 10.0) diff --git a/aws_paa.gemspec b/aws_paa.gemspec index 6246390..b7dc29f 100644 --- a/aws_paa.gemspec +++ b/aws_paa.gemspec @@ -8,7 +8,7 @@ Gem::Specification.new do |spec| spec.authors = ["Dan Milne"] spec.email = ["d@nmilne.com"] - spec.summary = %q{Ruby API for Amazon's Product Advertising API v5} + spec.summary = %q{Client library for Amazon's Product Advertising API v5} spec.homepage = "https://github.com/dkam/aws_paa" spec.license = "MIT" @@ -30,6 +30,9 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 2.0" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "minitest", "~> 5.0" + spec.add_development_dependency "byebug" + spec.add_development_dependency "awesome_print" - spec.add_dependency 'httprb', '~> 0.1' + spec.add_dependency 'http', '~> 0.1' + spec.add_dependency 'aws-sigv4', '~> 1' end diff --git a/lib/aws_paa.rb b/lib/aws_paa.rb index ac76c74..954598c 100644 --- a/lib/aws_paa.rb +++ b/lib/aws_paa.rb @@ -1,5 +1,7 @@ require "aws_paa/version" +require 'aws_paa/locales' +require 'aws_paa/aws_request' require 'aws_paa/client' require 'aws_paa/request' require 'aws_paa/response' @@ -7,5 +9,4 @@ require 'aws_paa/response' module AwsPaa class Error < StandardError; end class NotImplemented < StandardError; end - # Your code goes here... end diff --git a/lib/aws_paa/client.rb b/lib/aws_paa/client.rb index 11cd24f..44e4527 100644 --- a/lib/aws_paa/client.rb +++ b/lib/aws_paa/client.rb @@ -1,8 +1,33 @@ -require 'rest-client' +require 'http' module AwsPaa class Client - def initialize(:access_key, :secret_key, :marketplace, partner_tag: nil, partner_type: 'Associates') + attr_accessor :marketplace, :partner_tag + attr_reader :partner_type, :access_key, :secret_key + + def initialize(access_key:, secret_key:, marketplace:, partner_tag: nil, partner_type: 'Associates') + raise ArgumentError unless MARKETPLACES.keys.include?(marketplace.to_sym) + @access_key = access_key + @secret_key = secret_key + @marketplace = MARKETPLACES[marketplace.to_sym] + @partner_tag = partner_tag + @partner_type = partner_type + end + + def get_items(item_ids: )[] + Request.new(client: self).get_items(item_ids: item_ids) + end + + def get_variations(asin: ) + Request.new(client: self).get_variations(asin: asin) + end + + def search_items(keywords: ) + Request.new(client: self).search_items(keywords: keywords) + end + + def get_browse_nodes + Request.new(client: self).get_browse_nodes(keywords: keywords) end end -end \ No newline at end of file +end diff --git a/lib/aws_paa/request.rb b/lib/aws_paa/request.rb index ef23cd5..dff3716 100644 --- a/lib/aws_paa/request.rb +++ b/lib/aws_paa/request.rb @@ -1,43 +1,48 @@ - +require 'byebug' module AwsPaa class Request - def initialize() - @request_example = { - "Keywords": "Harry", - "Marketplace": "www.amazon.com", - "PartnerTag": "xyz-20", - "PartnerType": "Associates", - "Resources": ["Images.Primary.Small","ItemInfo.Title","Offers.Listings.Price"], - "SearchIndex": "All" - } + include AwsRequest + attr_accessor :client, :marketplace, :timestamp, :datestamp, :amzstamp, :resources, :service + attr_accessor :headers, :payload - @headers = { - 'host' => 'webservices.amazon.com', - 'content-type' => 'application/json; charset=utf-8', - 'content-encoding' => 'amz-1.0', - 'x-amz-date' => '20160925T120000Z', - 'x-amz-target' => 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems', - 'Authorization' => 'AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE, SignedHeaders=content-type;host;x-amz-date;x-amz-target, Signature=&5d672d79c15b13162d9279b0855cfba6789a8edb4c82c400e06b5924a6f2b5d7;' - } + def initialize(client:) + @client = client + @marketplace = client.marketplace + @partner_tag = client.partner_tag + @partner_type = 'Associates' - @marketplaces = [www.amazon.com] + @resources = [ + "Images.Primary.Large", + "ItemInfo.ContentInfo", + "ItemInfo.ProductInfo", + "ItemInfo.Title", + "ItemInfo.ExternalIds", + ] + end + + def get_items(item_ids:, **options) + item_ids = Array(item_ids) + + payload = { ItemIds: item_ids, Resources: @resources } + + do_request(op: :get_items, payload: payload) + end + + def get_variations(asin:) + payload = { ASIN: asin, Resources: @resources } + + do_request(op: :get_variations, payload: payload) + end + + def search_items(keywords: ) + payload = { Keywords: keywords, Resources: @resources } + + do_request(op: :search_items, payload: payload) end def get_browse_nodes raise NotImplemented end - - def get_items - raise NotImplemented - end - - def get_variations - raise NotImplemented - end - - def search_items - raise NotImplemented - end end end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index f286135..4d955b4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,4 @@ $LOAD_PATH.unshift File.expand_path("../lib", __dir__) require "aws_paa" - +require 'byebug' require "minitest/autorun"