diff --git a/serverless-async-tasks/api/api-function/app/dynamo.py b/serverless-async-tasks/api/api-function/app/dynamo.py index 0c17c3e8..0139f7da 100644 --- a/serverless-async-tasks/api/api-function/app/dynamo.py +++ b/serverless-async-tasks/api/api-function/app/dynamo.py @@ -28,7 +28,7 @@ def create_task(task_type: str, payload: dict) -> str: "id": task_id, "task_type": task_type, "status": "CREATED", - "payload": _encode_dict(payload), + "payload": _encode(payload), "created_time": _get_timestamp(), } ) @@ -83,26 +83,26 @@ def list_tasks(next_token: str = None) -> dict: } if next_token: - scan_args["ExclusiveStartKey"] = _decode_dict(next_token) + scan_args["ExclusiveStartKey"] = _decode(next_token) res = table.scan(**scan_args) response = {"tasks": res["Items"]} if "LastEvaluatedKey" in res: - response["next_token"] = _encode_dict(res["LastEvaluatedKey"]) + response["next_token"] = _encode(res["LastEvaluatedKey"]) return response -def _encode_dict(d: dict) -> str: - json_string = json.dumps(d) +def _encode(data: dict) -> str: + json_string = json.dumps(data) return base64.b64encode(json_string.encode("utf-8")).decode("utf-8") -def _decode_dict(token: str) -> dict: - json_string = base64.b64decode(token.encode("utf-8")).decode("utf-8") +def _decode(data: str) -> dict: + json_string = base64.b64decode(data.encode("utf-8")).decode("utf-8") return json.loads(json_string) -def _get_timestamp(): +def _get_timestamp() -> int: return int(datetime.utcnow().timestamp()) diff --git a/serverless-async-tasks/api/api-function/app/models.py b/serverless-async-tasks/api/api-function/app/models.py index 96601b84..71c6eb65 100644 --- a/serverless-async-tasks/api/api-function/app/models.py +++ b/serverless-async-tasks/api/api-function/app/models.py @@ -1,8 +1,13 @@ +from typing import Literal from pydantic import BaseModel +task_types = Literal["TASK1", "TASK2", "TASK3"] +status_types = Literal["CREATED", "IN_PROGRESS", "COMPLETED", "FAILED"] + + class CreatePayload(BaseModel): - task_type: str + task_type: task_types data: dict @@ -11,14 +16,14 @@ class CreateResponse(BaseModel): class UpdatePayload(BaseModel): - status: str + status: status_types status_msg: str = "" class GetResponse(BaseModel): id: str - task_type: str - status: str + task_type: task_types + status: status_types status_msg: str = "" created_time: int = None updated_time: int = None diff --git a/serverless-async-tasks/api/template.yml b/serverless-async-tasks/api/template.yml index 387458b1..73f94b3d 100644 --- a/serverless-async-tasks/api/template.yml +++ b/serverless-async-tasks/api/template.yml @@ -2,10 +2,6 @@ AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Task API -Globals: - Function: - Timeout: 15 - Resources: Table: Type: AWS::DynamoDB::Table