"""Complete Product Digital.""" from owsclient import M2MTokenManager from owsclient import OwsClient from owsresponse import status from secrets_manager.lambda_ext import LambdaSecretsManager from src import config from src import exceptions ows_client = OwsClient( environment=config.ENVIRONMENT, service_name=config.APPLICATION_NAME, m2m_token_manager=M2MTokenManager( secrets_manager=LambdaSecretsManager( environment=config.ENVIRONMENT, service_name=config.APPLICATION_NAME, ), environment=config.ENVIRONMENT, service_name=config.APPLICATION_NAME, ), ) def get_ows_client() -> OwsClient: """Get OWS client.""" return ows_client def complete_product_digital(resolution, product_id, will_not_deliver): """Approve or reject product in ows-product-digital.""" assert resolution in {'approve', 'reject'} resolve_product(resolution, product_id, will_not_deliver) def resolve_product(resolution, product_id, will_not_deliver): """Call endpoint in ows-product-digital.""" if will_not_deliver: result = call_endpoint_will_not_deliver(product_id) else: result = call_endpoint(resolution, product_id) result_handler(result) def call_endpoint(resolution, product_id): """Approve or reject product in ows-product-digital.""" return get_ows_client().put( 'ows-product-digital', f'/product/audio/{product_id}/{resolution}', ) def call_endpoint_will_not_deliver(product_id): """Set product as not for distribution.""" return get_ows_client().put( 'ows-product-digital', f'/product/{product_id}/not_for_distribution', json={ 'not_for_distribution': 'ReviewedWontDeliver', 'release_status': 'label_processing', }, ) def result_handler(result): """Raise custom exception if bad request.""" if result.status_code == status.OK: return if ( result.status_code == status.BAD_REQUEST and result.json().get('message') == 'product not in submitted state' ): raise exceptions.ProductNotSubmittedException('Product not in submitted state') raise Exception(result.text)