"""Interface to the ows-account microservice.""" from functools import wraps from ddtrace import tracer from oto import response as oto_response from owsrequest.constants import headers as owsrequest_headers from sound_recordings.constants import access as access_constants from sound_recordings.constants import account as account_constants from sound_recordings.constants import service as service_constants from sound_recordings.services import request @tracer.wrap(name="get_vendor") def get_vendor(vendor_id): """Get basic information about a vendor. Args: vendor_id (str): The Vendor ID Return: response.Response: Response object containing dict of vendor info. """ resource = "/vendor/{}".format(vendor_id) ows_account_response = request.get(service_constants.OWS_ACCOUNT, resource) response_body = ows_account_response.json() if ows_account_response.status_code != 200: return oto_response.create_error_response( status=ows_account_response.status_code, code=response_body.get("code"), message=response_body.get("message"), ) return oto_response.Response(message=response_body) @tracer.wrap(name="get_vendor_id") def get_vendor_id(subaccount_id): """Get label_id by given subaccount_id. Args: subaccount_id (str): Subaccount unique identifier Return response.Response: Response object containing label id """ resource = "/subaccount/{}".format(subaccount_id) ows_account_response = request.get(service_constants.OWS_ACCOUNT, resource) response_body = ows_account_response.json() if ows_account_response.status_code != 200: return oto_response.create_error_response( status=ows_account_response.status_code, code=response_body.get("code"), message=response_body.get("message"), ) return oto_response.Response(message=response_body.get("vendor_id")) @tracer.wrap(name="vendor_has_access") def vendor_has_access(vendor_id): """Check if vendor has an access to the mobile app. Args: vendor_id (str): Vendor unique identifier Return bool: True if vendor has an access to Analytics feature, False otherwise """ resource = service_constants.OWS_ACCOUNT_FEATURES_RESOURCE.format( vendor_id=vendor_id ) ows_account_response = request.get(service_constants.OWS_ACCOUNT, resource) if ows_account_response.status_code != 200: raise Exception( "ows_account response error for resource {resource}: " "status code - {status_code}, reason - {reason}, Correlation-Id - " "{correlation_id}".format( resource=resource, status_code=ows_account_response.status_code, reason=ows_account_response.reason, correlation_id=ows_account_response.headers.get("correlation-id"), ) ) items = ows_account_response.json()["items"] return oto_response.Response( message=( access_constants.FEATURE_ANALYTICS in [feature["feature_name"] for feature in items] ) or False ) @tracer.wrap(name="get_label_subaccount_headers") def get_label_subaccount_headers(account_type, account_id): """Return labelid and subaccountid from account_type and account_id. Args: account_type (str): Vendor or subaccount account_id (str): Numerical account identifer Returns: oto.response.Response object containing labelid, subaccountid and headers. """ if account_type == account_constants.SUBACCOUNT_TYPE: ows_account_response = get_vendor_id(account_id) if not ows_account_response: return ows_account_response labelid = ows_account_response.message subaccountid = account_id else: labelid = account_id subaccountid = None return oto_response.Response( { "labelid": labelid, "subaccountid": subaccountid, "headers": { owsrequest_headers.GRASS_ACCOUNT_TYPE: account_type, owsrequest_headers.GRASS_ACCOUNT_ID: account_id, }, } ) @tracer.wrap(name="add_labelid_subaccountid_headers_to_kwargs") def add_labelid_subaccountid_headers_to_kwargs(orig_func): """Add labelid, subaccountid and subaccountid to kwargs of a function. Returns: func: Decorated function. """ @wraps(orig_func) def wrapper(*args, **kwargs): result = get_label_subaccount_headers( kwargs["account_type"], kwargs["account_id"] ) if not result: return result kwargs["labelid"] = result.message["labelid"] kwargs["subaccountid"] = result.message["subaccountid"] kwargs["headers"] = result.message["headers"] return orig_func(*args, **kwargs) return wrapper