"""Interface for the ows-account microservice.""" from functools import lru_cache from oto import response from owsrequest import request as requests from feature_fm.constants.error import ERROR_CODE_OWS_ACCOUNT_REQUEST from feature_fm.constants.ows_services import CONTENT_TYPE from feature_fm.constants.ows_services import DEFAULT_CURRENCY_CODE from feature_fm.constants.ows_services import OWS_ACCOUNT def get_enabled_features_for_vendor(vendor_id, correlation_id=None): """Get a list of enabled features for a vendor. Args: vendor_id (int): The vendor id. correlation_id (str): the correlation ID. Returns: response.Response: containing the list of features. """ path = '/vendor/{0}/features'.format(vendor_id) result = requests.get( OWS_ACCOUNT, path, headers={'Content-Type': CONTENT_TYPE}, correlation_id=correlation_id) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=ERROR_CODE_OWS_ACCOUNT_REQUEST, message=result.text) return response.Response(result.json()) def get_vendor_currency_code(vendor_id): """Get the currency code for a single vendor. Args: vendor_id (int): The vendor id. correlation_id (str): the correlation ID. Returns: response.Response: contains a dict that maps vendor ids to codes. """ vendor_currency_codes = get_vendor_currency_codes() if not vendor_currency_codes: get_vendor_currency_codes.cache_clear() vendor_currency_codes = get_vendor_currency_codes() if not vendor_currency_codes: return vendor_currency_codes currency_dict = vendor_currency_codes if not currency_dict.get(vendor_id): return DEFAULT_CURRENCY_CODE return currency_dict[vendor_id] @lru_cache(1000) def get_vendor_currency_codes(): """Get all currency codes for all vendors. Returns: response.Response: contains a dict that maps vendor ids to codes. """ path = '/vendor-currency-codes' result = requests.get( OWS_ACCOUNT, path, headers={'Content-Type': CONTENT_TYPE}) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=ERROR_CODE_OWS_ACCOUNT_REQUEST, message=result.text) vendor_currency_codes = {} for item in result.json()['items']: vendor_currency_codes[item['vendor_id']] = item['currency_code'] return vendor_currency_codes