-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
71 lines (62 loc) · 2.39 KB
/
app.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
import json, requests, sys, time, ast
from collections import OrderedDict
from flask import Flask, request, send_from_directory, render_template, jsonify
from flask.ext.cors import CORS
app = Flask(__name__, static_url_path='/static')
CORS(app)
app.config.from_pyfile('settings.py')
url = app.config['MAILCHIMP_LIST_URL']
api_key = app.config['MAILCHIMP_API_KEY']
def signup(email, ip):
"""Communicates with the Mailchimp API, returns error status"""
error = False
error_detail = ""
if email != None:
print("submitting")
data = {'email_address': str(email), 'status': 'subscribed', 'ip_opt': ip}
headers = {'Content-Type': 'application/json'}
try:
r = requests.post(url, data=json.dumps(data), headers=headers, auth=("apikey", api_key))
print r.json()
if r.status_code == 200:
error = False
elif r.status_code == 400:
error = True
error_detail = r.json()['detail']
print(r.text)
else:
error = True
error_detail = "Something broke... try again later"
except:
error = True
error_detail = "Something broke... try again later"
return error, error_detail
@app.route('/', methods=['POST', 'OPTIONS'])
def signup_api():
"""API endpoint with AWS Lambda-specific stuff"""
if request.method == 'OPTIONS':
# for CORS pre-flight check
print "api options"
return jsonify(request="options")
else:
email = ast.literal_eval(request.data)['email']
ip = request.headers.get('X-Forwarded-For').split(',')[0]
print "api post", email, ip
error, error_detail = signup(email, ip)
return jsonify(email=email, error=error, error_detail=error_detail)
@app.route('/signup')
def signup_generic():
"""Generic signup method which works on any server"""
email = ""
email = request.args.get('email')
ip = request.remote_addr
error = ""
error_detail = ""
if email != None:
error, error_detail = signup(email, ip)
return render_template('thanks.html', email=email, error=error, error_detail=error_detail, mailchimp_url=app.config['MAILCHIMP_POST_URL'])
@app.route('/static')
def serve_static(path):
return send_from_directory('/static', path)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8005)