Skip to content
This repository has been archived by the owner on Jun 22, 2021. It is now read-only.

re-introduce throttle checking #13

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
5 changes: 3 additions & 2 deletions lib/plenty_client/account/contact/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down
1 change: 1 addition & 0 deletions lib/plenty_client/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/plenty_client/listing/market/info.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true

# PlentyClient::Listing::Market::Info.list
module PlentyClient
module Listing
module Market
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)
Expand Down
17 changes: 16 additions & 1 deletion lib/plenty_client/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 16 additions & 9 deletions spec/classes/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down