"""API Client for ows-artist.""" import requests ARTIST_STRING = 'artist' class APIClient: """API Client for ows-artist.""" def __init__(self, base_url, session=None): """Initialize APIClient class.""" self.base_url = base_url self.session = session def get_artists(self, product_id): """Execute a GET against /products/artists.""" endpoint = '{}/products/{}/artists'.format( self.base_url, product_id) return requests.get( endpoint, headers={'Content-Type': 'application/json'}) def spotify_search(self, artist): """Execute a GET against spotify/search.""" params = {'q': artist} endpoint = '{}/{}/spotify/search'.format(self.base_url, ARTIST_STRING) return requests.get(endpoint, params=params, headers={'session': self.session}) def apple_search(self, artist): """Execute a GET against apple/search.""" params = {'q': artist} endpoint = '{}/{}/apple/search'.format(self.base_url, ARTIST_STRING) return requests.get(endpoint, params=params, headers={'session': self.session}) def artist_document(self, artist_id): """Execute a GET against artist/{artist_id}/document.""" endpoint = '{}/{}/{}/document'.format(self.base_url, ARTIST_STRING, artist_id) return requests.get(endpoint) def bulk_ensure_artists(self, source_artist_ids, destination_vendor_id, grass_vendor_id): """Execute a POST against /artist/bulk-ensure. Sends Grass-Account-Type=vendor + Grass-Account-Id= directly (the ows-grass proxy does not forward this route). source_artist_ids or destination_vendor_id may be None to exercise the validation paths. """ endpoint = '{}/{}/bulk-ensure'.format(self.base_url, ARTIST_STRING) body = {} if source_artist_ids is not None: body['source_artist_ids'] = source_artist_ids if destination_vendor_id is not None: body['destination_vendor_id'] = destination_vendor_id return requests.post( endpoint, json=body, headers={ 'Grass-Account-Type': 'vendor', 'Grass-Account-Id': str(grass_vendor_id), })