changeset 5:2daf0dc08247

add get report endpoint
author Dennis C. M. <dennis@denniscm.com>
date Mon, 05 Jun 2023 12:48:47 +0100
parents 9005b7590008
children d15ccf5f1373
files events/get_report_event.json get_report/__init__.py get_report/app.py get_report/requirements.txt template.yaml
diffstat 4 files changed, 70 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/events/get_report_event.json	Mon Jun 05 12:48:47 2023 +0100
@@ -0,0 +1,7 @@
+{
+  "queryStringParameters": {
+    "ticker": "san",
+    "type": "balance",
+    "year": "2021"
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/get_report/app.py	Mon Jun 05 12:48:47 2023 +0100
@@ -0,0 +1,28 @@
+import json
+import boto3
+from boto3.dynamodb.conditions import Key
+
+resource = boto3.resource('dynamodb')
+table = resource.Table('FinanceParser')
+
+
+def lambda_handler(event, context):
+    query_string_parameters = event['queryStringParameters']
+    company_ticker = query_string_parameters['ticker']
+    report_type = query_string_parameters['type']
+    year = query_string_parameters['year']
+
+    pk = f'{report_type}#{company_ticker}'
+    response = table.query(
+        KeyConditionExpression=Key('pk').eq(pk) & Key('sk').begins_with(year)
+    )
+
+    return {
+        "statusCode": 200,
+        "body": json.dumps({
+            "message": {
+                "items": response['Items'],
+                "count": len(response['Items'])
+            }
+        }),
+    }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/get_report/requirements.txt	Mon Jun 05 12:48:47 2023 +0100
@@ -0,0 +1,1 @@
+boto3
\ No newline at end of file
--- a/template.yaml	Mon Jun 05 10:13:43 2023 +0100
+++ b/template.yaml	Mon Jun 05 12:48:47 2023 +0100
@@ -69,8 +69,6 @@
     Type: AWS::Serverless::Function
     Properties:
       CodeUri: analyze_document/
-      Handler: app.lambda_handler
-      Runtime: python3.7
       Policies:
         - Version: "2012-10-17"
           Statement:
@@ -91,8 +89,6 @@
     Type: AWS::Serverless::Function
     Properties:
       CodeUri: process_document/
-      Handler: app.lambda_handler
-      Runtime: python3.7
     Connectors:
       S3Connector:
         Properties:
@@ -106,8 +102,6 @@
     Type: AWS::Serverless::Function
     Properties:
       CodeUri: upload_document/
-      Handler: app.lambda_handler
-      Runtime: python3.7
     Connectors:
       DynamoConnector:
         Properties:
@@ -122,6 +116,40 @@
           Permissions:
             - Read
 
+  Api:
+    Type: AWS::Serverless::Api
+    Properties:
+      StageName: Prod
+      Models:
+        Empty:
+          type: object
+
+  GetReportFunction:
+    Type: AWS::Serverless::Function
+    Properties:
+      CodeUri: get_report/
+      Events:
+        GetProductEvent:
+          Type: Api
+          Properties:
+            RestApiId: !Ref Api
+            Path: /report
+            Method: get
+            RequestParameters:
+              - method.request.querystring.ticker:
+                  Required: true
+              - method.request.querystring.type:
+                  Required: true
+              - method.request.querystring.year:
+                  Required: true
+    Connectors:
+      DynamoConnector:
+        Properties:
+          Destination:
+            Id: DynamoTable
+          Permissions:
+            - Read
+
   DynamoTable:
     Type: AWS::DynamoDB::Table
     Properties: