"""Interface for the ows-artist microservice.""" from oto import response from owsrequest import request as requests from feature_fm.constants.error import ERROR_CODE_OWS_ARTIST_REQUEST from feature_fm.constants.ows_services import CONTENT_TYPE from feature_fm.constants.ows_services import DEFAULT_PAGE_LIMIT from feature_fm.constants.ows_services import DEFAULT_PAGE_OFFSET from feature_fm.constants.ows_services import OWS_ARTIST def get_artists( account_type, account_id, page_limit=None, page_offset=None, updated_since=None): """Get a paginated list of artists for a vendor/subaccount. Args: account_type (str): the account type (vendor or subaccount) account_id (int): The account id page_limit (str): The page limit of the request page_offset (str): The page offset of the request updated_since (int): The timestamp to restrict the query Returns: response.Response: containing the artists """ page_limit = int(page_limit or DEFAULT_PAGE_LIMIT) page_offset = int(page_offset or DEFAULT_PAGE_OFFSET) updated_since = int(updated_since or 0) path = ('/{0}/{1}/full-artists?page_limit={2}&page_offset={3}' '&updated_since={4}').format( account_type, account_id, page_limit, page_offset, updated_since) result = requests.get( OWS_ARTIST, path, headers={'Content-Type': CONTENT_TYPE}) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=ERROR_CODE_OWS_ARTIST_REQUEST, message=result.text) return response.Response(result.json()) def get_artists_bulk(account_type, account_id, artist_ids): """Get a list of artists for a specified list of artist ids. Args: account_type (str): the account type (vendor or subaccount) account_id (int): The account id artist_ids ([int]): The list of artist ids to fetch Returns: response.Response: containing the artists """ path = ('/{0}/{1}/full-artists/bulk').format(account_type, account_id) result = requests.post( OWS_ARTIST, path, headers={'Content-Type': CONTENT_TYPE}, json={'artist_ids': artist_ids}) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=ERROR_CODE_OWS_ARTIST_REQUEST, message=result.text) return response.Response(result.json()) def get_all_artist_ids( account_type, account_id, page_offset=0, artist_ids=None): """Fetch all artist ids for a vendor/subaccount. Args: account_type (str): the account type (vendor or subaccount) account_id (int): The account id page_offset (int): the page offset artist_ids ([int]): the artist ids Returns: response.Response: containing the artist ids """ path = '/{0}/{1}/artists?page_limit=1000&page_offset={2}'.format( account_type, account_id, page_offset) result = requests.get( OWS_ARTIST, path, headers={'Content-Type': CONTENT_TYPE}) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=ERROR_CODE_OWS_ARTIST_REQUEST, message=result.text) result = result.json() if not artist_ids: artist_ids = [] artist_ids += [item['id'] for item in result['items']] pagination = result['pagination'] if len(artist_ids) < pagination['total_records']: return get_all_artist_ids( account_type, account_id, page_offset=len(artist_ids), artist_ids=artist_ids) return response.Response({'items': artist_ids}) def get_artist_by_id(account_type, account_id, artist_id): """Get artist information by providing artist id. Args: account_type (str): the account type (vendor or subaccount) account_id (int): The account id artist_id (int): The artist id Returns: response.Response: containing the artist """ path = '/{0}/{1}/artist/{2}'.format( account_type, account_id, artist_id) result = requests.get( OWS_ARTIST, path, headers={'Content-Type': CONTENT_TYPE}) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=ERROR_CODE_OWS_ARTIST_REQUEST, message=result.text) json_result = result.json() if 'artist_id' not in json_result: json_result['artist_id'] = json_result.get('id') del json_result['id'] return response.Response(json_result)