From 8ba56663ae86071e2b2a5817377797b3b85dd11b Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Mon, 9 Sep 2019 11:52:22 +1000 Subject: [PATCH] Merge request into client --- Gemfile.lock | 4 +-- lib/paapi.rb | 1 - lib/paapi/client.rb | 45 ++++++++++++++++++++++++++++---- lib/paapi/request.rb | 61 -------------------------------------------- 4 files changed, 42 insertions(+), 69 deletions(-) delete mode 100644 lib/paapi/request.rb diff --git a/Gemfile.lock b/Gemfile.lock index 6606c3d..5ab7f82 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,9 +37,9 @@ PLATFORMS ruby DEPENDENCIES - awesome_print + awesome_print (~> 1.8) bundler (~> 2.0) - byebug + byebug (~> 11) minitest (~> 5.0) paapi! rake (~> 10.0) diff --git a/lib/paapi.rb b/lib/paapi.rb index 5384deb..aa4a89a 100644 --- a/lib/paapi.rb +++ b/lib/paapi.rb @@ -3,7 +3,6 @@ require "paapi/version" require 'paapi/locales' require 'paapi/aws_request' require 'paapi/client' -require 'paapi/request' require 'paapi/response' module Paapi diff --git a/lib/paapi/client.rb b/lib/paapi/client.rb index e533a7d..f2556ae 100644 --- a/lib/paapi/client.rb +++ b/lib/paapi/client.rb @@ -2,32 +2,67 @@ require 'http' module Paapi class Client + include AwsRequest 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') + def initialize(access_key:, secret_key:, marketplace:, partner_tag: nil, resources: 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 + @resources = resources || [ + "Images.Primary.Large", + "ItemInfo.ContentInfo", + "ItemInfo.ProductInfo", + "ItemInfo.Title", + "ItemInfo.ExternalIds", + "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" + ] end def get_items(item_ids:, **options) - Response.new(Request.new(client: self).get_items(item_ids: item_ids, **options)) + item_ids = Array(item_ids) + + payload = { ItemIds: item_ids, Resources: @resources } + + res = do_request(op: :get_items, payload: payload) + + Response.new(res) end def get_variations(asin:, **options ) - Response.new(Request.new(client: self).get_variations(asin: asin, **options)) + payload = { ASIN: asin, Resources: @resources } + + res = do_request(op: :get_variations, payload: payload) + Response.new(res) end def search_items(keywords:, **options ) - Response.new(Request.new(client: self).search_items(keywords: keywords, **options)) + search_index = 'All' + + # %i[Keywords Actor Artist Author Brand Title ] + + payload = { Keywords: keywords, Resources: @resources, ItemCount: 10, ItemPage: 1, SearchIndex: search_index }.merge(options) + + res = do_request(op: :search_items, payload: payload) + + Response.new(res) end def get_browse_nodes(keywords:, **options) - Response.new(Request.new(client: self).get_browse_nodes(keywords: keywords, **options)) + raise NotImplemented + #Response.new(Request.new(client: self).get_browse_nodes(keywords: keywords, **options)) end end end diff --git a/lib/paapi/request.rb b/lib/paapi/request.rb deleted file mode 100644 index 7cd2763..0000000 --- a/lib/paapi/request.rb +++ /dev/null @@ -1,61 +0,0 @@ -require 'byebug' - -module Paapi - class Request - include AwsRequest - attr_accessor :client, :marketplace, :resources, :payload, :service, :partner_type - attr_reader :partner_tag - - def initialize(client:, resources: nil) - @client = client - @marketplace = client.marketplace - @partner_tag = client.partner_tag - @partner_type = 'Associates' - - @resources = resources || [ - "Images.Primary.Large", - "ItemInfo.ContentInfo", - "ItemInfo.ProductInfo", - "ItemInfo.Title", - "ItemInfo.ExternalIds", - "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" - ] - 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:, **options ) - search_index = 'All' - - # %i[Keywords Actor Artist Author Brand Title ] - - payload = { Keywords: keywords, Resources: @resources, ItemCount: 10, ItemPage: 1, SearchIndex: search_index }.merge(options) - - do_request(op: :search_items, payload: payload) - end - - def get_browse_nodes - raise NotImplemented - end - end -end \ No newline at end of file