"""lookup-orchard-product.""" import json import boto3 from ddex_ingester_common.logging import utils as logging_utils import config from config import graphql_gateway from constants import queries logger = logging_utils.get_logger(config.app_logger) def handler(event, context): """Lambda entry point.""" bucket = event.get('bucket') key = event.get('key') item_index = event.get('item_index') logger.info(f'Received {bucket}/{key}.') s3_client = boto3.client('s3') file = s3_client.get_object(Bucket=bucket, Key=key) file_content = file.get('Body').read() data = json.loads(file_content) product = map_keys(data[item_index]) upc = product.get('upc') graphql_result = get_product_metadata(upc) if not graphql_result.get('productId'): raise ValueError(f'Product for {upc} not found.') product['product_id'] = graphql_result.get('productId') return product def get_product_metadata(upc): """Get product metadata via GraphQL. Args: upc (str): UPC Returns: result (dict): GraphQL response """ result = graphql_gateway.execute( queries.get_product_by_upc, {'upc': str(upc)} )['data']['productByUpc'] logger.info(f'Graphql result: {result}') return result def map_keys(data): """Map keys to proper case from input data. Args: data (dict): Input data Returns: dict """ return { 'status': data.get('Status'), 'original_release_date': data.get('ReleaseDate'), 'publish_status': data.get('PublishStatus'), 'main_artist': data.get('MainArtist'), 'release_name': data.get('ReleaseName'), 'digital_title_suppl': data.get('DigitalTitleSuppl'), 'product_number': data.get('ProdNo'), 'upc': data.get('UPC'), 'release_format': data.get('ReleaseFormat'), 'display_config': data.get('DisplayConfiguration'), 'media_format': data.get('MediaFormat'), 'local_genre': data.get('LocalGenre'), 'subgenre': data.get('SubGenre'), 'imprint': data.get('ImprintLabel'), 'artist_country': data.get('ArtistCountry'), 'sales_start_date': data.get('SalesStartDate'), 'preorder_date': data.get('PreOrderDate'), 'pitch_deadline': data.get('PitchDeadline'), 'internal_notes': data.get('InternalNotes'), 'marketing_blurb': data.get('MarketingBlurb'), 'global_mkt_highlights': data.get('GlobalMarketingHighlights'), 'territory_restrictions': data.get('TerritoryRestrictions'), 'delivery_history': data.get('DeliveryHistory'), 'territory_mkt_highlights': data.get('TerritoryMarketingHighlights'), 'spotify_artist_uri': data.get('SpotifyArtistURI'), 'apple_artist_id': data.get('AppleArtistId'), 'apple_id': data.get('AppleID'), 'spotify_store_link': data.get('SpotifyStorefrontLink'), 'deezer_store_link': data.get('DeezerStorefrontLink'), 'amz_asin_link': data.get('AmazonASINLink'), 'digital_asin': data.get('DigitalASIN'), 'created': data.get('Created'), }