"""Lambda create_vendor_sme_analytics function module.""" import config from config import app_logger as logger import constants.general as constants from owsrequest import request class MultipleVendorsExist(Exception): """Bad Request cannot retry Exception.""" pass class BadRequest(Exception): """Bad Request cannot retry Exception.""" pass def check_vendor_already_exist(input_data): """Check that vendor is already exist.""" external_identifier_1 = input_data.get('parent_rep_owner_key') logger.info( f'Check if vendor with external_identifier_1 ' f'{external_identifier_1} is already exist.') params = { 'owner': constants.OWNER } get_vendor_result = request.process( config.LAMBDA_NAME, config.ENVIRONMENT, config.GET_VENDOR_ENDPOINT_METHOD, config.OWS_SERVICE_NAME, config.GET_VENDOR_ENDPOINT.format( external_identifier_1=external_identifier_1), headers=config.OWS_REQUEST_HEADERS, params=params) logger.info(get_vendor_result) if get_vendor_result.status_code == 200: if len(get_vendor_result.json()['items']) == 1: logger.info( f'Vendor with external_identifier_1 {external_identifier_1} ' f'is already exist.') return get_vendor_result.json()['items'][0] elif len(get_vendor_result.json()['items']) > 1: raise MultipleVendorsExist( f'There are several vendor for external_identifier_1 ' f'{external_identifier_1}.') def handler(event, context): """Lambda entry point.""" input_data = event or {} existing_vendor = check_vendor_already_exist(input_data) if existing_vendor: # skip vendor creation if vendor is already exist return existing_vendor else: return {'vendor_id': None}