Skip to content

Commit

Permalink
[Element14][Fixed] Too fast queries
Browse files Browse the repository at this point in the history
Throttle Farnell queries to avoid Server Error Forbidden 403
(403 Developer Over QPS (Proxy))

Fixes #569
  • Loading branch information
set-soft committed Jul 16, 2024
1 parent 07958b3 commit b1aaf71
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions kicost/distributors/api_element14.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import pprint
import requests
import difflib
import datetime
import time

# KiCost definitions.
from .. import KiCostError, DistData, W_NOINFO, ERR_SCRAPE, W_APIFAIL, W_AMBIPN
Expand Down Expand Up @@ -148,6 +150,7 @@ def __init__(self, dist, country, key, cache):
raise Element14Error('Unsupported country `{}` for `{}`'.format(country, dist))
self.key = key
self.cache = cache
self.last_query = None

def _extract_data(self, data, kind, term):
res = REPLY[kind]
Expand Down Expand Up @@ -189,6 +192,12 @@ def search(self, kind, term, part={}):
params.update(part)
key_hide = hide_secrets(self.key)
debug_obsessive('Query params: '+pprint.pformat(params).replace(self.key, key_hide))
# Throttle Farnell queries to avoid Server Error Forbidden 403 (403 Developer Over QPS (Proxy)) #569
if self.last_query is not None:
while (datetime.datetime.now() - self.last_query).total_seconds() < 0.5:
time.sleep(0.2)
debug_obsessive('Sleeping to avoid 2 queries in less than 1 second ...')
self.last_query = datetime.datetime.now()
r = requests.get(BASE_URL, params=params)
if r.status_code != 200:
# debug_obsessive(pprint.pformat(r.__dict__))
Expand Down

0 comments on commit b1aaf71

Please sign in to comment.