Skip to content

Commit

Permalink
plugged Post method
Browse files Browse the repository at this point in the history
  • Loading branch information
socraticDevBlog committed Aug 11, 2023
1 parent f123eca commit 56315b0
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 57 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ one file per http verb.
#### simulate a POST request locally

```bash
pipenv run sam build

pipenv run sam local invoke -e local/events/post.json
pipenv run sam build && pipenv run sam local invoke -e local/events/post.json

# > Invoking app.lambda_handler (python3.9)
# Local image is up-to-date
Expand Down
1 change: 0 additions & 1 deletion local/events/get.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"message": "test paste number two",
"httpMethod": "GET"
}
25 changes: 17 additions & 8 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from paste import Paste
from model import Paste


def lambda_handler(event, context):
Expand All @@ -10,24 +10,33 @@ def lambda_handler(event, context):
Handles all http requests (from API Gateway)
- receives requests
- dispatch request to appropriate function
- returns http "statusCode" + a "body" with at least:
a) "message" field
...
- dispatch request to appropriate business logic
- returns http "statusCode" + a "body" with either:
a) "id" of a newly created Paste (GET)
b) "content" of a retrieved Paste (POST)
"""
method = event["httpMethod"]

if method == "GET":
paste = Paste(content="temp")
paste = Paste(
content="dummy content because Database is not plugged yet",
id=event["queryStringParameters"]["id"],
)
return {
"statusCode": 200,
"body": json.dumps({"message": paste.to_string()}),
"body": json.dumps(
{"content": paste.dict()["content"], "pasteInfos": paste.dict()}
),
}
elif method == "POST":
body = json.loads(event["body"])
paste = Paste(content=body["content"])
return {
"statusCode": 201,
"body": json.dumps({"message": "bob"}),
"body": json.dumps({"id": paste.id}),
}
else:
# APi Gateway should prevent this code for ever getting executed
# (see template.yml definition file)
return {"statusCode": 405, "body": json.dumps({"message": "bob"})}
59 changes: 59 additions & 0 deletions src/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import base64
import hashlib
import time
from typing import Dict

DEFAULT_ENCODING = "utf-8"


class Paste:
"""
Paste
Abstracts a Paste
basic Paste operations are implemented
- create a Unique Identifier for a new Paste
- Base64 encode for a new Paste
- Base64 decode an existing Paste content into a human-readable string
Public functions
- Dict(): returns Paste infos in a dictionary format
"""

def __init__(self, content: str = None, id: str = None, timestamp: int = None):
if content is not None:
self._base_64_content = self._base64_encode_str(content=content)
else:
self._base_64_content = ""
if id is None:
self.id = hashlib.sha1(
self._base_64_content.encode(DEFAULT_ENCODING)
).hexdigest()
else:
self.id = id
if timestamp is None:
self._unix_timestamp = int(time.time())
else:
self._unix_timestamp = timestamp

def dict(self) -> Dict:
return {
"id": self._id,
"content": self._base64_decode_content(),
"isBase64Encoded": False,
"encoding": DEFAULT_ENCODING,
"created_time_epoch": self._unix_timestamp,
}

def _base64_decode_content(self, encoding: str = DEFAULT_ENCODING) -> str:
base64_content_bytes = self._base_64_content.encode(encoding=encoding)
content_bytes = base64.b64decode(base64_content_bytes)
return content_bytes.decode(encoding=encoding)

def _base64_encode_str(self, content: str, encoding: str = DEFAULT_ENCODING) -> str:
b = base64.b64encode(bytes(content, encoding))
return b.decode(encoding)
39 changes: 0 additions & 39 deletions src/paste.py

This file was deleted.

10 changes: 5 additions & 5 deletions template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Resources:
Properties:
Path: /paste
Method: get
# CreatePaste:
# Type: Api
# Properties:
# Path: /paste
# Method: post
CreatePaste:
Type: Api
Properties:
Path: /paste
Method: post
2 changes: 1 addition & 1 deletion test/test_paste.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from src.paste import Paste
from src.model import Paste


def test_required_fields_to_object():
Expand Down

0 comments on commit 56315b0

Please sign in to comment.