"""Interface for the ows-search microservice.""" from oto import response from owsrequest import request as requests from feature_fm.constants.error import ERROR_CODE_OWS_SEARCH 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 OWS_SEARCH from feature_fm.constants.ows_services import OWS_SEARCH_RELEASES_CONTEXTS from feature_fm.constants.ows_services import OWS_SEARCH_RELEASES_TOKEN def get_artist_releases( account_type, account_id, artist_id, search_term=None, page_size=None): """Get a paginated list of releases for an artist. Args: account_type (str): The account type (vendor or subaccount) account_id (int): The account id artist_id: (int): The artist id search_term (str): The search term page_size (int): The page size Returns: response.Response: containing the releases """ search_term = str(search_term or '') page_size = int(page_size or DEFAULT_PAGE_LIMIT) path = ('/api/v1/suggest?context={0}&token={1}&term={2}&page_size={3}' '&filter.artist_id={4}&filter.{5}={6}' '&sort=sale_start_date&sort_order=desc').\ format( OWS_SEARCH_RELEASES_CONTEXTS[account_type], OWS_SEARCH_RELEASES_TOKEN, search_term, page_size, artist_id, '{0}_id'.format(account_type), account_id) result = requests.get( OWS_SEARCH, path, headers={'Content-Type': CONTENT_TYPE}) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=ERROR_CODE_OWS_SEARCH, message=result.text) return response.Response(result.json()) def get_label_releases( account_type, account_id, search_term=None, page_size=None): """Get a paginated list of releases for a label. Args: account_type (str): The account type (vendor or subaccount) account_id (int): The account id search_term (str): The search term page_size (int): The page size Returns: response.Response: containing the releases """ search_term = str(search_term or '') page_size = int(page_size or DEFAULT_PAGE_LIMIT) path = ('/api/v1/suggest?context={0}&token={1}&term={2}&page_size={3}' '&filter.{4}={5}' '&sort=sale_start_date&sort_order=desc').\ format( OWS_SEARCH_RELEASES_CONTEXTS[account_type], OWS_SEARCH_RELEASES_TOKEN, search_term, page_size, '{0}_id'.format(account_type), account_id) result = requests.get( OWS_SEARCH, path, headers={'Content-Type': CONTENT_TYPE}) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=ERROR_CODE_OWS_SEARCH, message=result.text) return response.Response(result.json())