"""Product logic.""" from oto import response from owsrequest import request as requests from promo_player.constants import error from promo_player.constants import service_name def get_by_product_id(product_id): """Get a product by product_id from ows-digital-product. Args: product_id (int): the product ID. Returns: response.Response: containing the product dict. """ path = '/product/audio/{product_id}'.format(product_id=product_id) result = requests.get(service_name.OWS_PRODUCT_DIGITAL, path) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=error.ERROR_CODE_OWS_PRODUCT_REQUEST, message=result.json()) return response.Response(result.json()) def get_artist_name(product_details): """Extract the artist name from the product details. Args: product_details (dict): the product Returns: str: the artist name or N/A if not found. """ artists = product_details.get('product_artists', []) primary = [] featuring = [] for artist in artists: role = artist.get('role', None) name = artist.get('name', None) if name and role == 'primary_artist': primary.append(name) elif name and role == 'featuring': featuring.append(name) primary = ' & '.join(primary) featuring = ' & '.join(featuring) if primary and featuring: return '{0} ft. {1}'.format(primary, featuring) elif primary: return primary return 'N/A'