Skip to content

refactor all handlers #55

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

Merged
merged 1 commit into from
May 20, 2024
Merged
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
150 changes: 37 additions & 113 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from model import PasteDataAware
from api_handler import ApiHandler
from dynamodb import DB
from lambda_handlers import get_pastes_handler
from lambda_handlers import (
get_pastes_handler,
get_handler,
options_handler,
post_handler,
)

logger = logging.getLogger()
logger.setLevel("INFO")
Expand Down Expand Up @@ -37,113 +42,6 @@ def _dynamodb_endpoint_by_os(os: str):
return "http://host.docker.internal:8000"


def get_handler(event, context, db: DB, is_web_browser: bool = False):
try:
id = event["queryStringParameters"]["id"]
paste = PasteDataAware(db=db, id=id)
content = paste.read()
except:
logger.error(f"GET-failed to retrieve requested paste-id {id}")
return {"statusCode": 404}

if is_web_browser:
response_html = """
<!DOCTYPE html>
<html>
<head>
<title>Saved Content</title>
<meta charset="UTF-8">
</head>
<body>
<pre>{}</pre>
</body>
</html>
""".format(
content
)
return {
"statusCode": 200,
"isBase64Encoded": paste.is_base64_encoded(content=content),
"headers": {
"Content-Type": "text/html; charset=utf-8",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST,GET,OPTIONS,DELETE",
"Access-Control-Allow-Headers": "Content-Type",
},
"body": response_html,
}

return {
"statusCode": 200,
"isBase64Encoded": False,
"headers": {
"content-type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST,GET,OPTIONS,DELETE",
"Access-Control-Allow-Headers": "Content-Type",
},
"body": json.dumps(content),
}


def post_handler(event, context, db: DB):
try:
# Extract the IP address from the event
client_ip = event["requestContext"]["identity"]["sourceIp"]
logger.info(f"POST-client IP address: {client_ip}")
except:
logger.warn(
"POST- client IP address not available at event['requestContext']['identity']['sourceIp']"
)

try:
content = json.loads(event["body"], strict=False)["content"]
except:
logger.warning(f'POST- unable to load content from event["body"])["content"]')
content = event["content"]

client_id = ""
try:
client_id = json.loads(event["body"], strict=False)["client_id"]
except:
client_id = client_ip

paste = PasteDataAware(
content=content, db=db, client_identifier=hash_value(client_id)
)

try:
id = paste.insert()
except Exception as e:
return {"statusCode": 500, "body": e.args[0]}

return {
"statusCode": 201,
"isBase64Encoded": False,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST,GET,OPTIONS,DELETE",
"Access-Control-Allow-Headers": "Content-Type",
},
"body": json.dumps({"id": id}),
}


def options_handler(event, context):
return {
"statusCode": 200,
"isBase64Encoded": False,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST,GET,OPTIONS,DELETE",
"Access-Control-Allow-Headers": "Content-Type",
},
"body": json.dumps({"message": "probably checking your OPTIONS"}),
}


def lambda_handler(event, context):
"""
lambda_handler
Expand Down Expand Up @@ -183,18 +81,44 @@ def lambda_handler(event, context):
client_id=hash_value(client_id),
)

id = event["queryStringParameters"]["id"]
paste = PasteDataAware(db=db, id=id)

if "/api" in path:
return get_handler(context=context, event=event, db=db)
return get_handler(paste=paste)

try:
user_agent = event.get("headers", {}).get("User-Agent", "")
is_web_browser = "Mozilla" in user_agent or "AppleWebKit" in user_agent
return get_handler(
context=context, event=event, db=db, is_web_browser=is_web_browser
)
return get_handler(paste=paste, is_web_browser=is_web_browser)
except Exception as e:
return get_handler(context=context, event=event, db=db)
elif method == "POST":
return post_handler(context=context, event=event, db=db)
try:
client_ip = event["requestContext"]["identity"]["sourceIp"]
logger.info(f"POST-client IP address: {client_ip}")
except:
logger.warn(
"POST- client IP address not available at event['requestContext']['identity']['sourceIp']"
)

try:
content = json.loads(event["body"], strict=False)["content"]
except:
logger.warning(
f'POST- unable to load content from event["body"])["content"]'
)
content = event["content"]

client_id = ""
try:
client_id = json.loads(event["body"], strict=False)["client_id"]
except:
client_id = client_ip

paste = PasteDataAware(
content=content, db=db, client_identifier=hash_value(client_id)
)
return post_handler(paste=paste)
else:
return options_handler(context=context, event=event)
80 changes: 80 additions & 0 deletions src/lambda_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,85 @@
import json
from api_handler import ApiHandler
from model import PasteDataAware


def get_handler(paste: PasteDataAware, is_web_browser: bool = False):
try:
content = paste.read()
except:
return {"statusCode": 404}

if is_web_browser:
response_html = """
<!DOCTYPE html>
<html>
<head>
<title>Saved Content</title>
<meta charset="UTF-8">
</head>
<body>
<pre>{}</pre>
</body>
</html>
""".format(
content
)
return {
"statusCode": 200,
"isBase64Encoded": paste.is_base64_encoded(content=content),
"headers": {
"Content-Type": "text/html; charset=utf-8",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST,GET,OPTIONS,DELETE",
"Access-Control-Allow-Headers": "Content-Type",
},
"body": response_html,
}

return {
"statusCode": 200,
"isBase64Encoded": False,
"headers": {
"content-type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST,GET,OPTIONS,DELETE",
"Access-Control-Allow-Headers": "Content-Type",
},
"body": json.dumps(content),
}


def post_handler(paste: PasteDataAware):
try:
id = paste.insert()
except Exception as e:
return {"statusCode": 500, "body": e.args[0]}

return {
"statusCode": 201,
"isBase64Encoded": False,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST,GET,OPTIONS,DELETE",
"Access-Control-Allow-Headers": "Content-Type",
},
"body": json.dumps({"id": id}),
}


def options_handler(event, context):
return {
"statusCode": 200,
"isBase64Encoded": False,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST,GET,OPTIONS,DELETE",
"Access-Control-Allow-Headers": "Content-Type",
},
"body": json.dumps({"message": "probably checking your OPTIONS"}),
}


def get_pastes_handler(api_handler: ApiHandler, client_id: str):
Expand Down
Loading