-
Notifications
You must be signed in to change notification settings - Fork 8
/
api.py
126 lines (98 loc) · 3.36 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import boto3
import os
import re
from botocore.exceptions import ClientError
from flask import Flask, request, jsonify
FROM_EMAIL = os.environ["FROM_EMAIL"]
DESTINATION_EMAIL = os.environ["DESTINATION_EMAIL"]
app = Flask(__name__)
app.debug = False
ses = boto3.client("ses")
REQUIRED_FIELDS = ["name", "email", "message"]
EMAIL_REGEXP = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
@app.after_request
def after_request(response):
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
response.headers.add("Access-Control-Allow-Methods", "OPTIONS,POST")
return response
class InvalidUsage(Exception):
pass
@app.errorhandler(400)
def handle_bad_request(error):
return jsonify({"status": "error", "error": str(error)}), 400
@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
response = jsonify({"status": "error", "error": str(error)})
response.status_code = 400
return response
@app.errorhandler(ClientError)
def handle_client_error(error):
response = jsonify({"status": "error", "error": str(error)})
response.status_code = 500
return response
@app.route("/", methods=["POST"])
def index():
data = request.get_json(force=True)
if not data:
raise InvalidUsage("Request body cannot be empty.")
for field in REQUIRED_FIELDS:
value = data.get(field, None)
if value is None:
raise InvalidUsage('Request body needs a "{}" field.'.format(field))
if not value.strip():
raise InvalidUsage(
'The "{}" field cannot be empty or spaces.'.format(field)
)
if not re.match(EMAIL_REGEXP, data["email"]):
raise InvalidUsage('The "email" field needs to be a valid email address.')
if "_important" in data:
# Our code should have removed this field before POSTing.
# This is probably triggered by a bot.
return (jsonify({"status": "ok"}), 200)
# Create a string with those fields that we do not recognize
other_fields = set(data.keys()) - set(REQUIRED_FIELDS)
str_other_fields = "\n".join(
"<strong>{}</strong>: {}<br>".format(field, data[field])
for field in sorted(other_fields)
)
subject = "Tryolabs contact form message from {} ({})".format(
data["name"], data["email"]
)
message = """<strong>name</strong>: {}<br>
<strong>email</strong>: {}<br>
{}
<p>
{}
</p>
""".format(
data["name"],
data["email"],
str_other_fields,
data["message"].replace("\n", "<br>"),
)
response = ses.send_email(
Source=FROM_EMAIL,
Destination={
"ToAddresses": [DESTINATION_EMAIL],
},
Message={
"Subject": {"Data": subject, "Charset": "utf-8"},
"Body": {"Html": {"Data": message, "Charset": "utf-8"}},
},
ReplyToAddresses=[data["email"]],
ReturnPath=FROM_EMAIL,
)
if response["ResponseMetadata"]["HTTPStatusCode"] != 200:
return (
jsonify(
{
"status": "error",
"error": "SES responded with {}".format(
response["ResponseMetadata"]["HTTPStatusCode"]
),
}
),
500,
)
return jsonify({"status": "ok"}), 200