forked from ash-nyt/fpa_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
executable file
·130 lines (103 loc) · 3.13 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
127
128
129
130
# [START all]
from flask import request, url_for
from flask.ext.api import FlaskAPI, status, exceptions
from accessControl import crossdomain
import MySQLdb
import os
import logging
env = os.getenv('SERVER_SOFTWARE')
# Only allow connections from App Engine
if (env and env.startswith('Google App Engine/')):
# Setup Flask API and use custom JSON encoder for decimal issue
app = FlaskAPI(__name__)
@app.route("/routes/", methods=['GET', 'OPTIONS'])
@crossdomain(origin='*')
def routes():
# Create DB connection
#cnx = mysql.connector.connect(user='root', password='root', host='localhost', database='cis_delivery')
cnx = MySQLdb.connect(unix_socket='/cloudsql/nyt-del-companion-api:us-central1:nyt-del-companion-db', user='root')
logging.error('HERE WE ARE')
logging.error(cnx)
# Create DB cursor
q_cursor = cnx.cursor(buffered=True)
# Generate and perform query on DB to get routes
query = ('SELECT id, name FROM route')
q_cursor.execute(query)
# Loop through the routes and populate each route
routes = []
for (route_id, name) in q_cursor:
# Add route information and add to routes list
route = {
"id": route_id,
"name": name
}
routes.append(route)
# Cleanup DB stuff
q_cursor.close()
cnx.close()
# Process response and return
response = {
"meta": {
"num_records": len(routes)
},
"data": {
"routes": routes
}
}
return response
@app.route("/routes/<int:route_id>/", methods=['GET', 'OPTIONS'])
@crossdomain(origin='*')
def route_detail(route_id):
# Create DB connection
cnx = mysql.connector.connect(user='root', password='root', host='localhost', database='cis_delivery')
# Create DB cursor
q_cursor = cnx.cursor(buffered=True)
# Generate and perform query on DB to get routes
query = ('SELECT cs.id, name, address_line1, city, state, zip, phone_home, lat, lng, esc.id, type, level, status, product, complaint \
FROM cis_subscriber cs INNER JOIN cis_subscriber_route csr ON cs.id = csr.cis_subscriber_id LEFT JOIN escalations esc ON cs.id = esc.cis_subscriber_id \
WHERE route_id = ' + str(route_id))
q_cursor.execute(query)
# Loop through the customers and populate each customer
customers = []
for (customer_id, name, address, city, state, zip, phone, lat, lng, esc_id, esc_type, level, status, product, complaint) in q_cursor:
# Populate escalation information first
escalation = None
if esc_id is not None:
escalation = {
"type": esc_type,
"level": level,
"status": status,
"product": product,
"complaint": complaint
}
# Add customer information and add to customers list
customer = {
"id": customer_id,
"name": name,
"address": address,
"city": city,
"state": state,
"zip": zip,
"phone_home": phone,
"geo": {
"lat": str(lat),
"lng": str(lng)
},
"escalation": escalation
}
customers.append(customer)
# Cleanup DB stuff
q_cursor.close()
cnx.close()
# Process response and return
response = {
"meta": {
"num_records": len(customers),
"route_id": route_id
},
"data": {
"customers": customers
}
}
return response
# [END all]