"""Interface for the ows-product-* microservices.""" from functools import partial from flask import current_app, request as flask_request from flask_executor_pde.executor import Executor from oto import response from owsrequest import request from project_manager.constant import field_const from project_manager.constant import services from project_manager.util.response_handler_util import safe_decode_json def update_product( product_id, context, params, headers): """Update product with specified params. Arguments: product_id (str): the product id context (str): context_type from distribution_format table params (dict): dictionary of params to update headers (dict): request headers Returns: response.Response: 200 if success other if not """ if context == field_const.PRODUCT_TYPE_DIGITAL: service_name = services.OWS_PRODUCT_DIGITAL uri_template = '/product/audio/{}' else: service_name = services.OWS_PRODUCT_PHYSICAL uri_template = '/product/{}' product_put_response = request.put( service_name, uri_template.format(product_id), json=params, headers=headers, ) content = safe_decode_json(product_put_response, service_name) if product_put_response.status_code != 200: return response.create_error_response( status=product_put_response.status_code, code=content.get('code'), message=content.get('message')) return response.Response( status=product_put_response.status_code, message=content) def _update_products_func(product, params, headers): """Update products for given project with specified params. Arguments: product (dict): dictionary of the product data params (dict): dictionary of params to update headers (dict): request headers Returns: response.Response: 200 if success other if not """ return update_product( product['product_id'], product['context_type'], params, headers) def update_products_by_pool(products, params): """Update products with specified params. Arguments: products (list): List of products for update params (dict): dictionary of params to update Returns: response.Response: 200 if success other if not """ with Executor(current_app) as executor: responses = list(executor.map( partial( _update_products_func, params=params, headers=dict(flask_request.headers) ), products )) for product_response in responses: if not product_response: return product_response return responses[-1]