|
| 1 | +# Copyright 2019 The Nuclio Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import requests |
| 16 | +import json |
| 17 | +import base64 |
| 18 | +import os |
| 19 | + |
| 20 | + |
| 21 | +def init_context(context): |
| 22 | + |
| 23 | + # env -> config |
| 24 | + setattr(context.user_data, 'config', { |
| 25 | + 'v3io_api': os.environ['V3IO_API'], |
| 26 | + 'v3io_username': os.environ['V3IO_USERNAME'], |
| 27 | + 'container_name': os.environ['CONTAINER_NAME'], |
| 28 | + 'table_name': os.environ['TABLE_NAME'], |
| 29 | + 'input_stream_search_key': os.environ['INPUT_STREAM_SEARCH_KEY'], |
| 30 | + 'output_stream_name': os.environ['OUTPUT_STREAM_NAME'], |
| 31 | + 'v3io_access_key': os.environ['V3IO_ACCESS_KEY'], |
| 32 | + }) |
| 33 | + |
| 34 | + |
| 35 | +def handler(context, event): |
| 36 | + config = context.user_data.config |
| 37 | + msg = json.loads(event.body) |
| 38 | + context.logger.info('Incoming message', msg=msg) |
| 39 | + enrichment_data = _search_kv(msg, config) |
| 40 | + context.logger.info('Enrichment data', enrichment_data=enrichment_data) |
| 41 | + msg['enrichment'] = enrichment_data |
| 42 | + _put_records([msg], config) |
| 43 | + context.logger.debug('Output message', msg=msg) |
| 44 | + |
| 45 | + |
| 46 | +def _get_url(v3io_api, container_name, collection_path): |
| 47 | + return f'http://{v3io_api}/{container_name}/{collection_path}' |
| 48 | + |
| 49 | + |
| 50 | +def _get_headers(v3io_function, v3io_access_key): |
| 51 | + return { |
| 52 | + 'Content-Type': 'application/json', |
| 53 | + 'X-v3io-function': v3io_function, |
| 54 | + 'cache-control': 'no-cache', |
| 55 | + 'x-v3io-session-key': v3io_access_key |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +def _search_kv(msg, config): |
| 60 | + v3io_api = config['v3io_api'] |
| 61 | + v3io_username = config['v3io_username'] |
| 62 | + container_name = config['container_name'] |
| 63 | + search_value = msg[config['input_stream_search_key']] |
| 64 | + table_path_and_key = f"{v3io_username}/examples/stream-enrich/{config['table_name']}/{search_value}" |
| 65 | + v3io_access_key = config['v3io_access_key'] |
| 66 | + |
| 67 | + url = _get_url(v3io_api, container_name, table_path_and_key) |
| 68 | + headers = _get_headers('GetItem', v3io_access_key) |
| 69 | + resp = requests.request('POST', url, json={}, headers=headers) |
| 70 | + |
| 71 | + json_response = json.loads(resp.text) |
| 72 | + |
| 73 | + response = {} |
| 74 | + if 'Item' in json_response: |
| 75 | + response = json_response['Item'] |
| 76 | + |
| 77 | + return response |
| 78 | + |
| 79 | + |
| 80 | +def _put_records(items, config): |
| 81 | + v3io_api = config['v3io_api'] |
| 82 | + v3io_username = config['v3io_username'] |
| 83 | + container_name = config['container_name'] |
| 84 | + output_stream_path = f"{v3io_username}/examples/stream-enrich/{config['output_stream_name']}/" |
| 85 | + v3io_access_key = config['v3io_access_key'] |
| 86 | + |
| 87 | + records = _items_to_records(items) |
| 88 | + url = _get_url(v3io_api, container_name, output_stream_path) |
| 89 | + headers = _get_headers('PutRecords', v3io_access_key) |
| 90 | + |
| 91 | + return requests.request('PUT', url, json=records, headers=headers) |
| 92 | + |
| 93 | + |
| 94 | +def _item_to_b64(item): |
| 95 | + item_string = json.dumps(item) |
| 96 | + return base64.b64encode(item_string.encode('utf-8')).decode('utf-8') |
| 97 | + |
| 98 | + |
| 99 | +def _items_to_records(items): |
| 100 | + return {'Records': [{'Data': _item_to_b64(item)} for item in items]} |
0 commit comments