Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TME requests and make them more robust #254

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions kintree/search/tme_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,12 @@ def setup_environment(force=False) -> bool:

# Based on TME API snippets mentioned in API documentation: https://developers.tme.eu/documentation/download
# https://github.com/tme-dev/TME-API/blob/master/Python/call.py
def tme_api_request(endpoint, tme_api_settings, part_number, api_host='https://api.tme.eu', format='json', **kwargs):
def tme_api_request(endpoint, tme_api_settings, params, api_host='https://api.tme.eu', format='json', **kwargs):
TME_API_TOKEN = tme_api_settings.get('TME_API_TOKEN', None)
TME_API_SECRET = tme_api_settings.get('TME_API_SECRET', None)

params = collections.OrderedDict()
params['Country'] = tme_api_settings.get('TME_API_COUNTRY', 'US')
params['Language'] = tme_api_settings.get('TME_API_LANGUAGE', 'EN')
params['SymbolList[0]'] = part_number
if kwargs.get('currency', None):
params['Currency'] = kwargs.get('currency')
if not TME_API_TOKEN and not TME_API_SECRET:
TME_API_TOKEN = os.environ.get('TME_API_TOKEN', None)
TME_API_SECRET = os.environ.get('TME_API_SECRET', None)
Expand All @@ -72,6 +68,7 @@ def tme_api_request(endpoint, tme_api_settings, part_number, api_host='https://a
cprint('[INFO]\tWarning: Value not found for TME_API_TOKEN and/or TME_API_SECRET', silent=False)
return None
params['Token'] = TME_API_TOKEN
params = collections.OrderedDict(sorted(params.items()))

url = api_host + endpoint + '.' + format
encoded_params = urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
Expand Down Expand Up @@ -115,7 +112,8 @@ def search_product(response):
return found, index

tme_api_settings = config_interface.load_file(settings.CONFIG_TME_API)
response = tme_api_query(tme_api_request('/Products/GetProducts', tme_api_settings, part_number))
params = {'SymbolList[0]': part_number}
response = tme_api_query(tme_api_request('/Products/GetProducts', tme_api_settings, params))

if response is None or response['Status'] != 'OK':
return {}
Expand All @@ -138,7 +136,8 @@ def search_product(response):
part_info['subcategory'] = None

# query the parameters
response = tme_api_query(tme_api_request('/Products/GetParameters', tme_api_settings, part_number))
params = {'SymbolList[0]': part_number}
response = tme_api_query(tme_api_request('/Products/GetParameters', tme_api_settings, params))
# check if accidentally no data returned
if response is None or response['Status'] != 'OK':
return part_info
Expand All @@ -153,7 +152,8 @@ def search_product(response):
part_info['parameters'][param['ParameterName']] = param['ParameterValue']

# query the prices
response = tme_api_query(tme_api_request('/Products/GetPrices', tme_api_settings, part_number, currency='USD'))
params = {'SymbolList[0]': part_number, 'Curreny': 'USD'}
response = tme_api_query(tme_api_request('/Products/GetPrices', tme_api_settings, params))
# check if accidentally no data returned
if response is None or response['Status'] != 'OK':
return part_info
Expand All @@ -175,7 +175,8 @@ def search_product(response):
part_info['currency'] = response['Data'][currency_key]

# Query the files associated to the product
response = tme_api_query(tme_api_request('/Products/GetProductsFiles', tme_api_settings, part_number))
params = {'SymbolList[0]': part_number}
response = tme_api_query(tme_api_request('/Products/GetProductsFiles', tme_api_settings, params))
# check if accidentally no products returned
if response is None or response['Status'] != 'OK':
return part_info
Expand Down
Loading