"""Model for ows-product-digital.""" import logging from time import time import requests import config # noqa import const # noqa from models.api_exceptions import PersistentException logger = logging.getLogger() logger.setLevel(logging.INFO) def get_digital_product_info(product_id, session=requests): """Get digital product info. Args: product_id: The id of the product to get data for. session: Session object to make requests with. The default is the requests module because it creates a Session object internally and has the same interface. """ url = const.get_service_url( environment=config.ENVIRONMENT, service_name=const.OWS_PRODUCT_DIGITAL_SERVICE_NAME, path=const.OWS_PRODUCT_DIGITAL_GET_ENDPOINT.format(product_id)) response = session.get( url, headers={ 'Correlation-Id': 'lambda-artist-info-sync-{}'.format(time()) }, timeout=10 ) if response.status_code == 404: logger.error('Requested product not found.') raise PersistentException(response.text) if response.status_code != 200: logger.error('Failed to get information from ows-product_digital.') raise PersistentException(response.text) logging.info( 'get_digital_product_info success. Data: {}'.format(response.text)) return response.json() def update_digital_product_info(product_info, session=requests): """Update digital product info. Args: product_info: The updated attributes of the digital product. It must include a product_id. session: Session object to make requests with. The default is the requests module because it creates a Session object internally and has the same interface. """ url = const.get_service_url( environment=config.ENVIRONMENT, service_name=const.OWS_PRODUCT_DIGITAL_SERVICE_NAME, path=const.OWS_PRODUCT_DIGITAL_UPDATE_ENDPOINT.format(product_info['product_id'])) # noqa response = session.put( url, params={'is_non_destructive_update': 'true'}, json=product_info, timeout=10) if response.status_code != 200: logger.error('Failed to update ows-product-digital.') raise PersistentException(response.text) logging.info( 'update_digital_product_info success. Data: {}'.format(response.text)) return response.json()