"""Interface to the ows-account microservice.""" from flask import g from sentry_sdk import capture_message from owsrequest import request as owsrequest from artist import response from artist.constants import service VENDOR_ID_MISSING_MESSAGE = 'Missing "vendor_id" in response from ows-account' OWS_ACCOUNT = 'ows-account' def get_vendor_id_for_subaccount_id(subaccount_id): """Fetch the vendor id for the given subaccount. Args: subaccount_id (int): ID of the subaccount to look up. Returns: response.Response: wrapper containing the results from ows-account. """ resource = '/subaccount/{}'.format(subaccount_id) account_response = owsrequest.get( OWS_ACCOUNT, resource) if account_response.status_code == 404: return response.create_not_found_response( message='subaccount not found') elif account_response.status_code != 200: error_message = 'ows-account returned {} status'.format( account_response.status_code) if account_response.text: # The response body might be helpful for debugging error_message += ' with body {}'.format(account_response.text) g.ows.log.error(error_message) capture_message(error_message) return response.create_fatal_response(message=error_message) account_json = account_response.json() vendor_id = account_json.get('vendor_id') # Basic validation on the response body from ows-account if vendor_id is None: g.ows.log.error(VENDOR_ID_MISSING_MESSAGE) capture_message(VENDOR_ID_MISSING_MESSAGE) return response.create_fatal_response( message=VENDOR_ID_MISSING_MESSAGE) return response.Response(message=vendor_id) def get_subaccount(subaccount_id): """Get a subaccount. Args: subaccount_id (int): subaccount_id of subaccount to get. Returns: dict: The retrieved subaccount. """ subaccount_response = owsrequest.get( service.OWS_ACCOUNT, '/subaccount/{}'.format(subaccount_id)) if subaccount_response.status_code != 200: error_message = 'ows-account returned {} status'.format( subaccount_response.status_code) if subaccount_response.text: # The response body might be helpful for debugging error_message += ' with body {}'.format(subaccount_response.text) g.ows.log.error(error_message) capture_message(error_message) return response.create_fatal_response(message=error_message) content = subaccount_response.json() return response.Response(message={ 'country_id': content['country_id'], 'description': content['description'], 'subaccount_id': content['subaccount_id'], 'name': content['subaccount_name'], 'vendor_id': content['vendor_id'], })