From 06d438ad54e11ccf2d36d36e32207ba62f98ace3 Mon Sep 17 00:00:00 2001 From: Owen Date: Thu, 2 Jan 2020 10:11:47 -0400 Subject: [PATCH 1/5] feat: Add delay to plenty requests when plenty's Short-Period-Decay is low Avoid "short period write limit reached" request errors --- lib/plenty_client/config.rb | 1 + lib/plenty_client/request.rb | 15 ++++++++++++++- spec/classes/request_spec.rb | 25 ++++++++++++++++--------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/lib/plenty_client/config.rb b/lib/plenty_client/config.rb index 230aca4..cb37cf0 100644 --- a/lib/plenty_client/config.rb +++ b/lib/plenty_client/config.rb @@ -11,6 +11,7 @@ class InvalidCredentials < StandardError; end class << self attr_accessor :site_url, :api_user, :api_password, :access_token, :refresh_token, :log, :expiry_date, :plenty_id + attr_accessor :request_wait_until attr_writer :attempt_count def validate_credentials diff --git a/lib/plenty_client/request.rb b/lib/plenty_client/request.rb index d6e4045..4112192 100644 --- a/lib/plenty_client/request.rb +++ b/lib/plenty_client/request.rb @@ -14,6 +14,8 @@ def request(http_method, path, params = {}) params = stringify_symbol_keys(params) if params.is_a?(Hash) + throttle_delay_request + perform(http_method, path, params) end @@ -84,6 +86,7 @@ def perform(http_method, path, params = {}) verb = http_method.to_s.downcase params = params.to_json unless %w[get delete].include?(verb) response = conn.send(verb, base_url(path), params) + throttle_check_short_period(response) assert_success_status_code(response) parse_body(response) end @@ -111,7 +114,17 @@ def throttle_check_short_period(response_header) short_calls_left = response_header['X-Plenty-Global-Short-Period-Calls-Left'] short_seconds_left = response_header['X-Plenty-Global-Short-Period-Decay'] return if short_calls_left&.empty? || short_seconds_left&.empty? - sleep(short_seconds_left.to_i + 1) if short_calls_left.to_i <= 10 && short_seconds_left.to_i < 3 + return if short_calls_left.to_i > 10 && short_seconds_left.to_i > 3 + + PlentyClient::Config.request_wait_until = Time.now + short_seconds_left.to_i + end + + def throttle_delay_request + delay_time = PlentyClient::Config.request_wait_until + return unless delay_time + return if Time.now > delay_time + + sleep((delay_time - Time.now).round) end def parse_body(response) diff --git a/spec/classes/request_spec.rb b/spec/classes/request_spec.rb index 161829d..3e66a99 100644 --- a/spec/classes/request_spec.rb +++ b/spec/classes/request_spec.rb @@ -198,30 +198,37 @@ def response_headers(mimetype = 'application/json') end end - xdescribe 'throttle check' do + describe 'throttle check' do context 'short period' do before do stub_api_tokens end it 'enough calls left' do - valid_request = stub_request(:post, /foobar/).to_return( + stub_request(:post, /foobar/).to_return( body: {}.to_json, - headers: { 'X-Plenty-Global-Short-Period-Calls-Left' => 50, 'X-Plenty-Global-Short-Period-Decay' => 5 } + headers: { + 'X-Plenty-Global-Short-Period-Calls-Left' => 50, + 'X-Plenty-Global-Short-Period-Decay' => 5 + }.merge(response_headers) ) + _success_request = request_client.request(:post, '/foobar') expect(Object).not_to receive(:sleep) request_client.request(:post, '/foobar') - expect(valid_request).to have_been_made.once end it 'limit reached' do - limited_request = stub_request(:post, /foobar/).to_return( + seconds_left = 2 + stub_request(:post, /foobar/).to_return( body: {}.to_json, - headers: { 'X-Plenty-Global-Short-Period-Calls-Left' => 5, 'X-Plenty-Global-Short-Period-Decay' => 2 } + headers: { + 'X-Plenty-Global-Short-Period-Calls-Left' => 5, + 'X-Plenty-Global-Short-Period-Decay' => seconds_left + }.merge(response_headers) ) - expect(Object).to receive(:sleep).with(3) - request_client.request(:post, '/foobar') - expect(limited_request).to have_been_made.once + _success_request = request_client.request(:post, '/foobar') + expect(Object).to receive(:sleep).with(seconds_left) + _delayed_request = request_client.request(:post, '/foobar') end end end From 10e17d2b7c17bb70cd674e9501346c7f0592e003 Mon Sep 17 00:00:00 2001 From: Owen Peredo Diaz Date: Fri, 3 Jan 2020 05:25:41 -0400 Subject: [PATCH 2/5] style: print log message when delaying Co-Authored-By: Carlos I. Garcia --- lib/plenty_client/request.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/plenty_client/request.rb b/lib/plenty_client/request.rb index 4112192..7c54381 100644 --- a/lib/plenty_client/request.rb +++ b/lib/plenty_client/request.rb @@ -124,7 +124,9 @@ def throttle_delay_request return unless delay_time return if Time.now > delay_time - sleep((delay_time - Time.now).round) + wait_until = (delay_time - Time.now) + STDOUT.write "Plenty client => delaying request: #{wait_until} seconds" + sleep(wait_until.round) end def parse_body(response) From 2fb777ad1123cdaf1717d2bfc2337010795348f8 Mon Sep 17 00:00:00 2001 From: Owen Peredo Date: Tue, 23 Jun 2020 05:11:52 -0400 Subject: [PATCH 3/5] fix: remove false delay --- lib/plenty_client/request.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plenty_client/request.rb b/lib/plenty_client/request.rb index 7c54381..319fde4 100644 --- a/lib/plenty_client/request.rb +++ b/lib/plenty_client/request.rb @@ -114,7 +114,7 @@ def throttle_check_short_period(response_header) short_calls_left = response_header['X-Plenty-Global-Short-Period-Calls-Left'] short_seconds_left = response_header['X-Plenty-Global-Short-Period-Decay'] return if short_calls_left&.empty? || short_seconds_left&.empty? - return if short_calls_left.to_i > 10 && short_seconds_left.to_i > 3 + return if short_calls_left.to_i > 1 PlentyClient::Config.request_wait_until = Time.now + short_seconds_left.to_i end From afb6e2d94c52dc99a57d36878c4f6ada46ec65f6 Mon Sep 17 00:00:00 2001 From: Owen Peredo Date: Tue, 4 Aug 2020 10:57:22 -0400 Subject: [PATCH 4/5] fix: fix typo when creating contact address --- lib/plenty_client/account/contact/address.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/plenty_client/account/contact/address.rb b/lib/plenty_client/account/contact/address.rb index 2369e59..5c03cee 100644 --- a/lib/plenty_client/account/contact/address.rb +++ b/lib/plenty_client/account/contact/address.rb @@ -23,8 +23,9 @@ def list(contact_id, address_type = '', headers = {}, &block) headers, &block) end - def create(body = {}) - post("#{CONTACT_ADDRESS_BASE_PATH}#{CREATE_A_CONTACT_ADDRESS}", body) + def create(contact_id, body = {}) + post(build_endpoint("#{CONTACT_ADDRESS_BASE_PATH}#{CREATE_A_CONTACT_ADDRESS}", + contact: contact_id), body) end def update(contact_id, address_id, body = {}) From 73df0d469378f6e188b17ccf25d4d4a29bcddc6d Mon Sep 17 00:00:00 2001 From: Matias Albarello Date: Fri, 9 Dec 2022 07:17:41 -0300 Subject: [PATCH 5/5] Fix typo --- lib/plenty_client/listing/market/info.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/plenty_client/listing/market/info.rb b/lib/plenty_client/listing/market/info.rb index 5833947..c879495 100644 --- a/lib/plenty_client/listing/market/info.rb +++ b/lib/plenty_client/listing/market/info.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +# PlentyClient::Listing::Market::Info.list module PlentyClient module Listing module Market @@ -7,7 +8,8 @@ class Info include PlentyClient::Endpoint include PlentyClient::Request - LIST_LISTINGS_MARKET_INFO = '/listings/markets/info' + # https://developers.plentymarkets.com/en-gb/plentymarkets-rest-api/index.html#/Listing/get_rest_listings_markets_infos + LIST_LISTINGS_MARKET_INFO = '/listings/markets/infos' class << self def list(headers = {}, &block)