"""Interface for the ows-product microservice.""" import json from oto import response from owsrequest import request as requests from pricing.constants import error from pricing.constants import service_name def is_owner(product_id, account_type, account_id): """Get ownership of a product for account type and account id. Args: product_id (int): the product identifier. account_type (str): the account type (vendor or subaccount). account_id (str): the account id. Returns: response.Response: Indicates whether the account owns the product. """ path = '/{account_type}/{account_id}/product/{product_id}'.format( account_type=account_type, account_id=account_id, product_id=product_id) result = requests.head(service_name.OWS_PRODUCT, path) return response.Response(status=result.status_code) def get_product_ids_from_upcs(upcs): """Get a list of products regarding upcs. Args: upcs (dict): the upcs to get products from. Returns: response.Response: a list of products. """ path = '/bulk-upc' try: products_response = make_request( service_name.OWS_PRODUCT, path, {'upcs': upcs}) if products_response['status_code'] == 200: return response.Response( message=products_response['content']) else: return response.create_error_response( products_response['status_code'], products_response['content']) except Exception as err: return response.create_error_response(500, err) def get_product_details(product_id): """Get a product by its product_id. Args: product_id (int): the product_id (release_id) of the product Returns: response.Response: Product information related to the given product_id. """ path = '/product/{product_id}'.format(product_id=product_id) try: product_details_response = requests.get( service_name.OWS_PRODUCT, path) if product_details_response.status_code != 200: return response.create_error_response( status=500, code=error.INTERNAL_ERROR, message='error fetching product') return response.Response(message=product_details_response.json()) except Exception: return response.create_error_response( status=500, code=error.INTERNAL_ERROR, message='error fetching product') def make_request(service, path, data): """Make ows-request. Args: service (str): the service name to get request from. path (str): the url name to get request from. data (json): the json data to post. Returns: dict: the request content and its status. """ products_response = requests.post(service_name.OWS_PRODUCT, path, json=data) response_content = json.loads( products_response.content.decode('utf8')) return { 'status_code': products_response.status_code, 'content': response_content }