"""Interface for the ows-marketing microservice.""" from oto import response from owsrequest import request as requests from ows_product_physical.constant import marketing from ows_product_physical.constant import service_name from ows_product_physical.utils.response_handler_util import safe_decode_json def get_product_highlight_by_product_id(product_id): """Get a single highlight for a product. Args: product_id (int): primary key for a product. Returns: response.Response: the result of fetching a single highlight. """ params = { 'client': 'alw', 'limit': 1 } highlight_response = requests.get( service_name.OWS_MARKETING, '/highlights/release/{PRODUCT_ID}?mkt_program_id={PROGRAM_ID}'.format( PRODUCT_ID=product_id, PROGRAM_ID=marketing.PROGRAM_MARKETING_HIGHLIGHTS), params=params) content = safe_decode_json(highlight_response, service_name.OWS_MARKETING) if highlight_response.status_code != 200: return response.create_error_response( status=highlight_response.status_code, code=content.get('code'), message=content.get('message')) return response.Response( status=highlight_response.status_code, message=content.get('items')[0]) def create_product_highlight(product_id, highlight_description): """Create a single highlight for a product. POST request. Args: product_id (int): primary key for a product. highlight_description (str): The copy for the highlight. Returns: response.Response: the result of creating a highlight. """ params = { 'entity': 'release', 'entity_id': product_id, 'subject': 'Product Highlight', 'description': highlight_description, 'client': 'alw', 'mkt_program_id': marketing.PROGRAM_MARKETING_HIGHLIGHTS, 'attachment': 'N', 'scope': 'public'} highlight_response = requests.post( service_name.OWS_MARKETING, '/highlights', json=params) content = safe_decode_json(highlight_response, service_name.OWS_MARKETING) if highlight_response.status_code != 201: return response.create_error_response( status=highlight_response.status_code, code=content.get('code'), message=content.get('message')) return response.Response( status=highlight_response.status_code, message=content) def update_product_highlight(highlight_id, highlight_description): """Update a product highlight. PUT request. Args: highlight_id (int): primary key for the highlight to update. highlight_description (str): The copy for the highlight. Returns: response.Response: the result of updating a highlight. """ params = {'description': highlight_description} highlight_response = requests.put( service_name.OWS_MARKETING, '/highlights/{HIGHLIGHT_ID}'.format( HIGHLIGHT_ID=highlight_id), json=params) content = safe_decode_json(highlight_response, service_name.OWS_MARKETING) if highlight_response.status_code != 200: return response.create_error_response( status=highlight_response.status_code, code=content.get('code'), message=content.get('message')) return response.Response( status=highlight_response.status_code, message=content)