"""Interface to the ows-blacklist-manager microservice.""" from flask import g from oto import response from owsrequest import request from owsrequest.constants import headers as header_constants from product_digital.constants import services def validate_product(product_id): """Confirm the blocklist status of the product. Args: product_id (int): id of the product Returns: response.Response: status of validation call and dictionary of results """ headers = {k: str(v) for k, v in { header_constants.ORCHARD_USER_ID: getattr(g.request_context, 'orchard_user_id', None), header_constants.ORCHARD_PROFILE_TYPE: getattr(g.request_context, 'profile_type', None), header_constants.ORCHARD_PROFILE_ID: getattr(g.request_context, 'profile_id', None), header_constants.ORCHARD_IDENTITY_ID: getattr(g.request_context, 'identity_id', None), }.items() if v} blocklist_response = request.get( services.OWS_BLOCKLIST_MANAGER, '/validate/{product_id}'.format(product_id=product_id), headers=headers ) status = blocklist_response.status_code if status == 200: return response.Response(message={}) validation_body = blocklist_response.json() # if the product is invalid it will throw a 400 if status != 400: return response.create_error_response( status=status, code=validation_body.get('code'), message=validation_body.get('message')) return response.Response(message=validation_body) def validate_artists(payload): """Confirm the blocklist status of the product. Args: payload (int): inputs of product artists to validate Returns: response.Response: status of validation call and dictionary of results """ headers = {k: str(v) for k, v in { header_constants.ORCHARD_USER_ID: getattr(g.request_context, 'orchard_user_id', None), header_constants.ORCHARD_PROFILE_TYPE: getattr(g.request_context, 'profile_type', None), header_constants.ORCHARD_PROFILE_ID: getattr(g.request_context, 'profile_id', None), header_constants.ORCHARD_IDENTITY_ID: getattr(g.request_context, 'identity_id', None), }.items() if v} blocklist_response = request.post( services.OWS_BLOCKLIST_MANAGER, '/validate-artists', headers=headers, json=payload ) status = blocklist_response.status_code if status == 200: return response.Response(message={}) validation_body = blocklist_response.json() # if an artist is invalid it will throw a 400 if status != 400: return response.create_error_response( status=status, code=validation_body.get('code'), message=validation_body.get('message')) return response.Response(message=validation_body)