"""Asset logic.""" from oto import response from owsrequest import request as requests from promo_player import config from promo_player.constants import error from promo_player.constants import service_name def get_product_assets( product_id, vendor_id, subaccount_id, correlation_id=None ): """Get the assets of a product from ows-assets. Args: product_id (int): the product ID. vendor_id (int): unique identifier for vendor. subaccount_id (int): unique identifier for subaccount. correlation_id (str): the correlation ID. Returns: response.Response: containing a dict with the list of assets. """ if not product_id: return response.Response({'assets': []}) path = '/v2/asset/product/{product_id}' result = requests.get( service_name.OWS_ASSETS, path.format(product_id=product_id), correlation_id=correlation_id ) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=error.ERROR_CODE_OWS_ASSET_REQUEST, message=result.json()) return response.Response(result.json()) def get_product_cover_url(product_id, correlation_id=None): """Get the product cover image URL from ows-assets. Args: product_id (int): the product ID. correlation_id (str): the correlation ID. Returns: response.Response: containing the cover image URL. """ if not product_id: return response.Response('') path = '/image/product/large_cover/{product_id}/location'.format( product_id=product_id) result = requests.get( service_name.OWS_ASSETS, path, correlation_id=correlation_id) if result.status_code not in [200, 404]: return response.create_error_response( status=result.status_code, code=error.ERROR_CODE_OWS_ASSET_REQUEST, message=result.json()) if result.status_code == 404: return response.Response('') return response.Response(result.text) def get_track_manifest_url(track_id, referrer=None): """Get the track HLS manifest URL from ows-assets. Args: track_id (int): the track ID. referrer (str): the request referrer Returns: response.Response: containing the HLS manifest URL. """ path = '/stream/track/{track_id}/hls'.format(track_id=track_id) headers = {'Orchard-User-Id': config.SYSTEM_ORCHARD_USER_ID} if referrer: headers['Referer'] = referrer result = requests.get( service_name.OWS_ASSETS, path, headers=headers) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=error.ERROR_CODE_OWS_ASSET_REQUEST, message=result.json()) return response.Response(result.json())