"""Interface to the ows-blacklist-manager microservice.""" from owsrequest import request from backend.constants import error from backend.constants import services from backend.exceptions import RequestError def validate_artists(payload): """Validate track artists against the blacklist manager. Calls the /validate-artists endpoint to check whether any artists on a track are on the Spotify watchlist or other blacklists. Args: payload (dict): payload containing track_artists list, e.g. {'track_artists': [{'tuid': , 'artists': }]} Returns: dict: validation response JSON from the service Raises: RequestError: if the service returns a non-200/400 response """ endpoint = '/validate-artists' blacklist_response = request.post( services.OWS_BLACKLIST_MANAGER, endpoint, json=payload ) response_status = blacklist_response.status_code if response_status == 200: return {} # if an artist is invalid it will throw a 400 if response_status != 400: raise RequestError( f'Failed to validate artists against blacklist manager. ' f'Error: {blacklist_response.text}', error_code=error.OWS_BLACKLIST_MANAGER_ERROR, http_status=response_status, ) validation_body = blacklist_response.json() return validation_body