"""Interface for the ows-product microservice.""" import json from flask import request from oto import response import requests from images import config from images.constants import header def get_vendor_and_subaccount_by_upc(upc): """Fetch the product with a given upc from ows-product. Args: upc (int): the UPC value to query by. Returns: response.Response: containing the vendor and subaccount for the upc. """ request_headers = { header.CORRELATION_ID: request.headers.get(header.CORRELATION_ID, '')} ows_product_response = requests.get( '{ows_product_url}/product/upc/{upc}'.format( ows_product_url=config.OWS_PRODUCT_URL, upc=upc), headers=request_headers) if ows_product_response.status_code != 200: return response.create_error_response( code=ows_product_response.reason, message=ows_product_response.text, status=ows_product_response.status_code) message = json.loads(ows_product_response.content.decode('utf8')) account_info = { 'vendor_id': message.get('vendor_id'), 'subaccount_id': message.get('subaccount_id')} return response.Response(message=account_info)