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 = {}) 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/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) diff --git a/lib/plenty_client/request.rb b/lib/plenty_client/request.rb index d6e4045..319fde4 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,19 @@ 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 > 1 + + 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 + + wait_until = (delay_time - Time.now) + STDOUT.write "Plenty client => delaying request: #{wait_until} seconds" + sleep(wait_until.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