def entity_resolution_force(event, context): try: keyword = extract_keyword(event) result = search_tr(keyword=keyword) if not result: return construct_response(requests.codes.not_found, failure_response('resolve', keyword)) return construct_response(requests.codes.ok, result) except: message = 'ENTITY-RESOLUTION-FORCE: Error while processing request, dropping into SNS for some hand holding' return error_response(event, message, log)
def entity_lookup(event, context): try: keyword = extract_keyword(event) log.info(f'Received lookup query for {keyword}') result = dynamo_lookup_entity(keyword) if not result: return construct_response(requests.codes.not_found, failure_response('lookup', keyword)) return construct_response(requests.codes.ok, result) except: message = 'ENTITY-LOOKUP: Error while processing request, dropping into SNS for some hand holding' return error_response(event, message, log)
def add_override(event, context): try: override_request = json.loads(event['body']) keyword = override_request['keyword'] override_request['keyword'] = keyword manual_override = override_request[ ResolutionAlgo.MANUAL_OVERRIDE.value] manual_override['update_timestamp'] = get_timestamp() manual_override['keyword'] = keyword log.info( f'Received manual override request for keyword {keyword}, and payload {override_request}' ) if override_request is not None: dynamo_result = dynamo_lookup_entity(keyword) if not dynamo_result: dynamo_save_override_entity(override_request) else: dynamo_result[ ResolutionAlgo.MANUAL_OVERRIDE.value] = manual_override dynamo_save_override_entity(dynamo_result) return construct_response( requests.codes.ok, f'Successfully added manual override for {keyword}') except: message = 'MANUAL-OVERRIDE: Error while processing a manual override request, ' \ 'dropping into SNS for some hand holding' return error_response(event, message, log)
def demo_kinesis_event(event, context): try: log.info(f'Event triggered from kinesis') data = base64.b64decode( event['Records'][0]['kinesis']['data']).decode("utf-8") if 'email' in data: payload = { 'name': 'ALERT', 'email': '*****@*****.**', 'content': data } log.info(f'Sending email with {payload}') headers = {'Content-Type': 'application/json'} requests.post( url='https://st-dev.cb-learning.com/api/v1/email/send', data=json.dumps(payload), headers=headers) else: log.info(data) return construct_response(requests.codes.ok, f'Triggered kinesis event') except: log.info(f'Something bad happened for {event}') message = 'ERROR: Goodbye world!' return error_response(event, message, log)
def demo_dynamo_find(event, context): try: keyword = __extract_parameter(event, 'keyword') log.info(f'Lets look for this {keyword} in dynamo') item = dynamo_lookup_item(keyword) if not item: log.info(f'No items found for keyword {keyword} in dynamo') return construct_response( requests.codes.ok, f'No items found for keyword {keyword} in dynamo') return construct_response(requests.codes.ok, f'{item}') except: message = 'ERROR: Could not execute find!' return error_response(event, message, log)
def hello_world(event, context): try: log.info(f'Hello World!') return construct_response(requests.codes.ok, 'Hello World') except: message = 'ERROR: Goodbye world!' return error_response(event, message, log)
def demo_dynamo_event(event, context): try: log.info(f'Event triggered from dynamo') for item in event['Records']: event_type = item['eventName'] if event_type == 'REMOVE': image_name = 'OldImage' else: image_name = 'NewImage' record = item['dynamodb'][image_name] log.info(f'{event_type} {record} from dynamo') return construct_response(requests.codes.ok, f'Triggered event') except: log.info(f'Something bad happened for {event}') message = 'ERROR: Goodbye world!' return error_response(event, message, log)
def demo_dynamo_save(event, context): try: keyword = __extract_parameter(event, 'keyword') data = __extract_parameter(event, 'data') expire_at = int(time.time() + 30) log.info(f'Lets persist this {keyword}:{data}:{expire_at} into dynamo') some_random_object = { 'keyword': keyword, 'expireAt': expire_at, 'value': data } __save_time(some_random_object) return construct_response(requests.codes.ok, f'{keyword} persisted in dynamo') except: message = 'ERROR: Could not execute save!' return error_response(event, message, log)