"""Model for ows_assets.""" import json import requests import src.config as config import src.constants.const as const def get_image(product_id, cover_type): """Fetch a 2000x2000 cover art URL with product_id. Args: product_id (int): A product_id. cover_type (str): A type of cover. Return: str: A cover art URL. """ url = const.get_service_url( environment=config.ENVIRONMENT, service_name=const.OWS_ASSETS, path=const.OWS_ASSETS_GET_IMAGE_LOCATION.format( cover_type=cover_type, product_id=product_id ), ) response = requests.get(url) if response.status_code != 200: return "No image found for product {}".format(product_id) return response.text def fetch_covers(product_ids, cover_type): """Fetch a list of cover art URL with product_ids and UPCs. Args: product_ids (list[int]): A list of integer product IDs. cover_type (str): The cover type, ex: cover, large_cover, xlarge_cover. Return: list(dict): A list of dict with product IDs as keys and 2000x2000 cover art URL as values. """ url = const.get_service_url( environment=config.ENVIRONMENT, service_name=const.OWS_ASSETS, path=const.OWS_ASSETS_GET_IMAGE_LOCATIONS.format(cover_type=cover_type), ) response = requests.post( url, data=json.dumps({"entities": product_ids}), headers={"Content-Type": "application/json"}, ) if response.status_code != 200: raise Exception("No image found for product {}".format(product_ids)) return json.loads(response.text)