Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions handler_c_contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json
import logging
import urllib
from datetime import datetime

from src.create_signup import CreateSignup
from src.response import Response


def handle(event, context):
try:
event_body = json.loads(urllib.parse.unquote_plus(event["body"]))

signs = CreateSignup()
create = signs.create(
event["requestContext"]["authorizer"]["claims"]["email"],
datetime.now(),
event_body[0],
)

return Response.handle(create, 200)

except Exception as e:
msg = f"Unable{str(e)}"
logging.exception(msg)
return Response.handle({"error": msg}, 500)
Empty file added src/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions src/create_signup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import uuid
from datetime import datetime
from multiprocessing import Process

import boto3


class CreateSignup:
def __init__(self):
self.dynamodb = boto3.resource("dynamodb", region_name="eu-west-1")
self.table = self.dynamodb.Table("signups")

def create(
self, email: str, request_datetime: datetime, full_name: str
) -> dict:

signup = self.createSignupObject(full_name, request_datetime)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signup not being used anywhere?


p = Process(target=self.c, args=(email, full_name, request_datetime))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why a child process is being created to make a call to dynamodb

p.start()

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function isn't returning anything. It's supposed to return a dict

def c(self, email, full_name, request_datetime):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function name not explicit

self.table.put_item(
Item={
'email': email,
"uuid": str(uuid.uuid4()),
"name": full_name,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate code between this and createSignupObject

"time": request_datetime.strftime("%H:%M:%S"),
}
)

def createSignupObject(self, full_name, request_datetime):
return {
"uuid": str(uuid.uuid4()),
"name": full_name,
"time": request_datetime.strftime("%H:%M:%S"),
}
14 changes: 14 additions & 0 deletions src/response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json


class Response:
@staticmethod
def handle(message, status_code=200):
return {
"statusCode": str(status_code),
"body": json.dumps(message),
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
}
51 changes: 51 additions & 0 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,57 @@ Globals:
Timeout: 3

Resources:

SignupApi:
Type: AWS::Serverless::Api
Properties:
Name: SignupApo
StageName: test
Cors:
AllowMethods: "'*'"
AllowHeaders: "'*'"
AllowOrigin: "'*'"

SignupTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: "signups"
AttributeDefinitions:
-
AttributeName: "email"
AttributeType: "S"
-
AttributeName: "day"
AttributeType: "S"
KeySchema:
-
AttributeName: "email"
KeyType: "HASH"
-
AttributeName: "day"
KeyType: "RANGE"
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5



ContactWrite:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: handler_c_contact.h
Runtime: python3.7
Policies:
- AmazonDynamoDBFullAccess
Events:
Write:
Type: Api
Properties:
Path: /write
RestApiId: !Ref SignupApi
Method: POST
Comment on lines +49 to +63
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing wrong, handler name sucks, policy is too much (just be read/write on the signups table would be idea)


HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_create_signup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from datetime import datetime

import boto3
from moto import mock_dynamodb2

from src.create_signup import CreateSignup


class TestCreateSignup(unittest.TestCase):
@mock_dynamodb2
def test_creates_signup(self):

dynamodb = boto3.resource("dynamodb", region_name="eu-west-1")

table = dynamodb.create_table(
TableName="contacts",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Table used is signup, but this is creating contacts

KeySchema=[
{"AttributeName": "email", "KeyType": "HASH"},
{"AttributeName": "day", "KeyType": "RANGE"},
],
AttributeDefinitions=[
{"AttributeName": "email", "AttributeType": "S"},
{"AttributeName": "day", "AttributeType": "S"},
],
ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
)

client = CreateSignup()
response = client.create('[email protected]', '11/10/2020', 'Stu Mason')


print(result)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No assertion?

8 changes: 8 additions & 0 deletions tests/unit/test_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import unittest

from src.response import Response


class TestResponse(unittest.TestCase):
def test_response(self):
Response.handle("Foo", 200)