|
1 | | -""" |
2 | | -Status Query Function |
3 | | -Retrieves webhook processing status from DynamoDB |
4 | | -""" |
5 | | -import json |
6 | | -import os |
7 | | -from typing import Dict, Any |
8 | | -import boto3 |
9 | | -from boto3.dynamodb.conditions import Key |
10 | | - |
11 | | -dynamodb = boto3.resource('dynamodb') |
12 | | -table = dynamodb.Table(os.environ['EVENTS_TABLE_NAME']) |
13 | | - |
14 | | - |
15 | | -def lambda_handler(event: Dict[str, Any], context: Any) -> Dict[str, Any]: |
16 | | - """ |
17 | | - Query webhook processing status |
18 | | - |
19 | | - Args: |
20 | | - event: API Gateway event with executionToken in path parameters |
21 | | - context: Lambda context |
22 | | - |
23 | | - Returns: |
24 | | - HTTP response with status information |
25 | | - """ |
26 | | - print(f"Status query event: {json.dumps(event)}") |
27 | | - |
28 | | - # Extract execution token from path parameters |
29 | | - execution_token = event.get('pathParameters', {}).get('executionToken') |
30 | | - |
31 | | - if not execution_token: |
32 | | - return { |
33 | | - 'statusCode': 400, |
34 | | - 'headers': { |
35 | | - 'Content-Type': 'application/json', |
36 | | - 'Access-Control-Allow-Origin': '*' |
37 | | - }, |
38 | | - 'body': json.dumps({ |
39 | | - 'error': 'Missing executionToken in path' |
40 | | - }) |
41 | | - } |
42 | | - |
43 | | - print(f"Querying status for execution token: {execution_token}") |
44 | | - |
45 | | - try: |
46 | | - # Query DynamoDB for the execution token |
47 | | - response = table.get_item( |
48 | | - Key={'executionToken': execution_token} |
49 | | - ) |
50 | | - |
51 | | - if 'Item' not in response: |
52 | | - print(f"Execution token not found: {execution_token}") |
53 | | - return { |
54 | | - 'statusCode': 404, |
55 | | - 'headers': { |
56 | | - 'Content-Type': 'application/json', |
57 | | - 'Access-Control-Allow-Origin': '*' |
58 | | - }, |
59 | | - 'body': json.dumps({ |
60 | | - 'error': 'Execution token not found', |
61 | | - 'executionToken': execution_token |
62 | | - }) |
63 | | - } |
64 | | - |
65 | | - item = response['Item'] |
66 | | - |
67 | | - # Build response |
68 | | - status_response = { |
69 | | - 'executionToken': item['executionToken'], |
70 | | - 'status': item['status'], |
71 | | - 'currentStep': item.get('currentStep'), |
72 | | - 'createdAt': item['createdAt'], |
73 | | - 'lastUpdated': item['lastUpdated'] |
74 | | - } |
75 | | - |
76 | | - # Include error if present |
77 | | - if 'error' in item: |
78 | | - status_response['error'] = item['error'] |
79 | | - |
80 | | - # Include webhook payload summary (not full payload for security) |
81 | | - if 'webhookPayload' in item: |
82 | | - payload = item['webhookPayload'] |
83 | | - status_response['webhookSummary'] = { |
84 | | - 'type': payload.get('type', payload.get('event', 'unknown')), |
85 | | - 'source': payload.get('source', payload.get('sender', 'unknown')), |
86 | | - 'keys': list(payload.keys())[:10] # Limit to first 10 keys |
87 | | - } |
88 | | - |
89 | | - print(f"Status retrieved successfully: {execution_token}, status: {item['status']}") |
90 | | - |
91 | | - return { |
92 | | - 'statusCode': 200, |
93 | | - 'headers': { |
94 | | - 'Content-Type': 'application/json', |
95 | | - 'Access-Control-Allow-Origin': '*' |
96 | | - }, |
97 | | - 'body': json.dumps(status_response) |
98 | | - } |
99 | | - |
100 | | - except Exception as e: |
101 | | - print(f"Error querying status: {str(e)}") |
102 | | - return { |
103 | | - 'statusCode': 500, |
104 | | - 'headers': { |
105 | | - 'Content-Type': 'application/json', |
106 | | - 'Access-Control-Allow-Origin': '*' |
107 | | - }, |
108 | | - 'body': json.dumps({ |
109 | | - 'error': 'Internal server error', |
110 | | - 'message': str(e) |
111 | | - }) |
112 | | - } |
| 1 | +""" |
| 2 | +Status Query Function |
| 3 | +Retrieves webhook processing status from DynamoDB |
| 4 | +""" |
| 5 | +import json |
| 6 | +import os |
| 7 | +from typing import Dict, Any |
| 8 | +import boto3 |
| 9 | +from boto3.dynamodb.conditions import Key |
| 10 | + |
| 11 | +dynamodb = boto3.resource('dynamodb') |
| 12 | +table = dynamodb.Table(os.environ['EVENTS_TABLE_NAME']) |
| 13 | + |
| 14 | + |
| 15 | +def lambda_handler(event: Dict[str, Any], context: Any) -> Dict[str, Any]: |
| 16 | + """ |
| 17 | + Query webhook processing status |
| 18 | + |
| 19 | + Args: |
| 20 | + event: API Gateway event with executionToken in path parameters |
| 21 | + context: Lambda context |
| 22 | + |
| 23 | + Returns: |
| 24 | + HTTP response with status information |
| 25 | + """ |
| 26 | + print(f"Status query event: {json.dumps(event)}") |
| 27 | + |
| 28 | + # Extract execution token from path parameters |
| 29 | + execution_token = event.get('pathParameters', {}).get('executionToken') |
| 30 | + |
| 31 | + if not execution_token: |
| 32 | + return { |
| 33 | + 'statusCode': 400, |
| 34 | + 'headers': { |
| 35 | + 'Content-Type': 'application/json', |
| 36 | + 'Access-Control-Allow-Origin': '*' |
| 37 | + }, |
| 38 | + 'body': json.dumps({ |
| 39 | + 'error': 'Missing executionToken in path' |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + print(f"Querying status for execution token: {execution_token}") |
| 44 | + |
| 45 | + try: |
| 46 | + # Query DynamoDB for the execution token |
| 47 | + response = table.get_item( |
| 48 | + Key={'executionToken': execution_token} |
| 49 | + ) |
| 50 | + |
| 51 | + if 'Item' not in response: |
| 52 | + print(f"Execution token not found: {execution_token}") |
| 53 | + return { |
| 54 | + 'statusCode': 404, |
| 55 | + 'headers': { |
| 56 | + 'Content-Type': 'application/json', |
| 57 | + 'Access-Control-Allow-Origin': '*' |
| 58 | + }, |
| 59 | + 'body': json.dumps({ |
| 60 | + 'error': 'Execution token not found', |
| 61 | + 'executionToken': execution_token |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + item = response['Item'] |
| 66 | + |
| 67 | + # Build response |
| 68 | + status_response = { |
| 69 | + 'executionToken': item['executionToken'], |
| 70 | + 'status': item['status'], |
| 71 | + 'currentStep': item.get('currentStep'), |
| 72 | + 'createdAt': item['createdAt'], |
| 73 | + 'lastUpdated': item['lastUpdated'] |
| 74 | + } |
| 75 | + |
| 76 | + # Include error if present |
| 77 | + if 'error' in item: |
| 78 | + status_response['error'] = item['error'] |
| 79 | + |
| 80 | + # Include webhook payload summary (not full payload for security) |
| 81 | + if 'webhookPayload' in item: |
| 82 | + payload = item['webhookPayload'] |
| 83 | + status_response['webhookSummary'] = { |
| 84 | + 'type': payload.get('type', payload.get('event', 'unknown')), |
| 85 | + 'source': payload.get('source', payload.get('sender', 'unknown')), |
| 86 | + 'keys': list(payload.keys())[:10] # Limit to first 10 keys |
| 87 | + } |
| 88 | + |
| 89 | + print(f"Status retrieved successfully: {execution_token}, status: {item['status']}") |
| 90 | + |
| 91 | + return { |
| 92 | + 'statusCode': 200, |
| 93 | + 'headers': { |
| 94 | + 'Content-Type': 'application/json', |
| 95 | + 'Access-Control-Allow-Origin': '*' |
| 96 | + }, |
| 97 | + 'body': json.dumps(status_response) |
| 98 | + } |
| 99 | + |
| 100 | + except Exception as e: |
| 101 | + print(f"Error querying status: {str(e)}") |
| 102 | + return { |
| 103 | + 'statusCode': 500, |
| 104 | + 'headers': { |
| 105 | + 'Content-Type': 'application/json', |
| 106 | + 'Access-Control-Allow-Origin': '*' |
| 107 | + }, |
| 108 | + 'body': json.dumps({ |
| 109 | + 'error': 'Internal server error' |
| 110 | + }) |
| 111 | + } |
0 commit comments