Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasbrange committed Mar 25, 2022
1 parent 451e10e commit ce2c8e3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
16 changes: 8 additions & 8 deletions serverless-async-tasks/api/api-function/app/dynamo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
)
Expand Down Expand Up @@ -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())
13 changes: 9 additions & 4 deletions serverless-async-tasks/api/api-function/app/models.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions serverless-async-tasks/api/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ce2c8e3

Please sign in to comment.