diff --git a/CHANGELOG.md b/CHANGELOG.md index db005c1..46573ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.1.7 ( Unreleased ) +- 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. diff --git a/lib/paapi/client.rb b/lib/paapi/client.rb index 0b75687..27ccd3a 100644 --- a/lib/paapi/client.rb +++ b/lib/paapi/client.rb @@ -1,4 +1,4 @@ -require 'net/http' +require 'net/http/persistent' require 'aws-sigv4' module Paapi @@ -10,9 +10,9 @@ module Paapi def initialize(access_key: Paapi.access_key, secret_key: Paapi.secret_key, partner_tag: Paapi.partner_tag, - market: Paapi.market || DEFAULT_MARKET, + market: Paapi.market || DEFAULT_MARKET, condition: Paapi.condition || DEFAULT_CONDITION, - resources: Paapi.resources || DEFAULT_RESOURCES, + resources: Paapi.resources || DEFAULT_RESOURCES, partner_type: DEFAULT_PARTNER_TYPE ) raise ArgumentError unless MARKETPLACES.keys.include?(market.to_sym) @@ -25,13 +25,7 @@ module Paapi 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 + @http = Net::HTTP::Persistent.new name: 'paapi' end def market=(a_market) @@ -107,18 +101,29 @@ module Paapi headers['X-Amz-Content-Sha256']= signature.headers['x-amz-content-sha256'] headers['Authorization'] = signature.headers['authorization'] headers['Content-Type'] = 'application/json; charset=utf-8' + + Response.new( post(url: endpoint, body: payload, headers: headers)) + end + + def post(url:, body:, headers:) + puts "Not Deprecated" + uri = URI.parse(url) + + post_request = Net::HTTP::Post.new(uri.path) + post_request.content_type = 'application/json; charset=UTF-8' - 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 + headers.each { |k, v| post_request[k] = v } + post_request.body = body.to_json + + #Client.post(client: @http, url: url, body: body, headers: headers) + http.request uri, post_request end - def self.post(url:, body:, headers:) + def self.post(url:, body:, headers:, client: nil) + puts "Deprecated" uri = URI.parse(url) - request = Net::HTTP::Post.new(uri) + request = client || 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 diff --git a/lib/paapi/version.rb b/lib/paapi/version.rb index e9a5c72..56d8c97 100644 --- a/lib/paapi/version.rb +++ b/lib/paapi/version.rb @@ -1,3 +1,3 @@ module Paapi - VERSION = '0.1.6' + VERSION = '0.1.7' end diff --git a/paapi.gemspec b/paapi.gemspec index 3cd465a..0dacfc1 100644 --- a/paapi.gemspec +++ b/paapi.gemspec @@ -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. @@ -32,4 +32,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'awesome_print', '~> 1.8' spec.add_dependency 'aws-sigv4', '~> 1' + spec.add_dependency 'net-http-persistent', '~> 4.0', '>= 4.0.1' end