This repository was archived by the owner on Jun 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathget_extracted_document.py
More file actions
52 lines (44 loc) · 1.73 KB
/
get_extracted_document.py
File metadata and controls
52 lines (44 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import base64
import json
import logging
from src import context
from src.database.database import Database
from src.documents import get_document
from src.external.aws.dynamodb import DynamoDb
from src.external.aws.s3 import S3
from src.storage import CloudStorage
appContext = context.ApplicationContext()
appContext.register(CloudStorage, S3())
appContext.register(Database, DynamoDb())
def lambda_handler(event, context):
document_id = event.get("pathParameters", {}).get("document_id")
if document_id is None:
return {
"statusCode": 400,
"body": json.dumps({"error": "Missing document_id in path parameter"}),
}
try:
document_info, storage_access_url, document_data = get_document.get_document(document_id)
if document_info is None:
return {
"statusCode": 404,
"body": json.dumps(f"Document {document_id} not found"),
}
response = {
"status": document_info["status"],
"document_id": document_id,
"document_key": document_info.get("document_url"),
"document_type": document_info.get("document_type"),
"extracted_data": document_info.get("extracted_data", {}),
"signed_url": storage_access_url,
"base64_encoded_file": base64.b64encode(document_data).decode("utf-8"),
}
except Exception as e:
exception_message = f"An internal error happened while trying to get document {document_id}"
logging.error(exception_message)
logging.exception(e)
return {
"statusCode": 500,
"body": json.dumps(exception_message),
}
return {"statusCode": 200, "body": json.dumps(response)}