"""Model for ows-artist.""" import logging from urllib.parse import quote_plus import requests import config # noqa import const # noqa from models.api_exceptions import PersistentException logger = logging.getLogger() logger.setLevel(logging.INFO) def filter_artists_info(artist_name, vendor_id, session=requests): """Get filter artists info. Args: artist_name: Name of artist to search for. vendor_id: Id of label the artist belongs to. session: Session object to make requests with. The default is the requests module because it creates a Session object internally and has the same interface. """ url = const.get_service_url( environment=config.ENVIRONMENT, service_name=const.OWS_ARTIST_SERVICE_NAME, path=const.OWS_ARTIST_FILTER_ARTISTS_ENDPOINT.format( quote_plus(artist_name, encoding='utf=8'), vendor_id)) response = session.get(url, timeout=10) if response.status_code != 200: logger.error('Failed to get information from ows-artist.') raise PersistentException(response.text) logging.info('filter_artists_info success. Data: {}'.format( response.text)) return response.json() def create_artist_info(artist_name, vendor_id, session=requests): """Create artist info function. Args: artist_name: Name of artist to create. vendor_id: Id of label the artist belongs to. session: Session object to make requests with. The default is the requests module because it creates a Session object internally and has the same interface. """ url = const.get_service_url( environment=config.ENVIRONMENT, service_name=const.OWS_ARTIST_SERVICE_NAME, path=const.OWS_ARTIST_CREATE_ARTIST_ENDPOINT) response = session.post( url, json={'name': artist_name, 'vendor_id': vendor_id}, timeout=10 ) if response.status_code != 201: logger.error('Failed to create ows-artist.') raise PersistentException(response.text) logging.info('create_artists_info success. Data: {}'.format(response.text)) return response.json()