"""Logic for artist search through ows-search.""" from oto import response import requests from social_analytics.constants import error from social_analytics.constants import search def _construct_ows_search_url(params): """ Generate an ows-search url based on a search term. Args: params (dict): A dict containing the following params: { 'context', 'token', 'term' } Returns: url (str): an ows-search url for the given search term. """ url = ('{base_url}' '?context={context}' '&token={access_token}' '&term={search_term}').format( base_url=search.OWS_SEARCH_BASE_URL, context=params['context'], access_token=params['token'], search_term=params['term']) return url def search_artist(params, headers): """Search artists on ows-search. Args: params (dict): A dict containing the following params: { 'context', 'token', 'term' } headers (dict) : The Grass headers to be passed to ows-search Returns: response.Response: containing a list of artist objects of the following format: { 'artist_name': 'Test artist', 'artist_id': '2', 'highlight_fields': { 'artist_name': 'Test A request.' } """ url = _construct_ows_search_url(params) result = requests.get(url, headers=headers) result_json = result.json() if result_json['isError']: return response.create_fatal_response(message=error.INTERNAL_ERROR) artist_results = result_json['results'] if not artist_results: return response.create_not_found_response() artists = [value for key, value in artist_results.items()] return response.Response({'items': artists})