-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
185 lines (147 loc) · 5.56 KB
/
main.py
File metadata and controls
185 lines (147 loc) · 5.56 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from flask import Flask, request, Response, render_template
import json
from flask import jsonify
from crud import Capital
import logging
import utility
from google.cloud import pubsub
from storage import Storage
import urllib2
import collections
app = Flask(__name__)
capital = Capital()
@app.route('/', methods=['GET', 'POST'])
def main_page():
if request.method == 'GET':
capitals = capital.fetch_capitals()
elif request.method == 'POST':
search_value = request.form['name']
if search_value == '':
capitals = capital.fetch_capitals()
else:
capitals = capital.fetch_capitals_query('country', search_value)
results = collections.OrderedDict()
for item in capitals:
city = {}
city['lat'] = item['location']['latitude']
city['long'] = item['location']['longitude']
city['id'] = item['id']
city['country'] = item['country']
city['name'] = item['name']
results[item['country']] = city
return render_template('main.html', comment=None, results=results)
@app.route('/maps')
def show_maps():
capitals = capital.fetch_capitals()
if len(capitals) > 0:
params = []
for item in capitals:
city = {}
city['lat'] = item['location']['latitude']
city['long'] = item['location']['longitude']
city['id'] = item['id']
city['country'] = item['country']
city['name'] = item['name']
params.append(city)
return render_template('maps.html', params=params)
else:
return "not found"
@app.route('/api/status')
def api_status():
data = {}
data['insert'] = True
data['fetch'] = True
data['delete'] = True
data['list'] = True
data['query'] = True
data['search'] = True
data['pubsub'] = True
data['storage'] = True
obj = jsonify(data)
return obj, 200
@app.route('/api/capitals/<id>', methods=['DELETE'])
def api_delete(id):
items = capital.get_capital(id)
if len(items) > 0:
capital.delete_capital(id)
return "success", 200
else:
return "not found", 404
@app.route('/api/capitals/<id>', methods=['GET'])
def api_get(id):
obj = capital.get_capital(id)
if len(obj) > 0:
return Response(response=json.dumps(obj[0]), status=200, mimetype="application/json")
else:
return Response(response="{\"code\":404,\"message\":\"not found\"}", status=404, mimetype="application/json")
@app.route('/api/capitals/<id>', methods=['PUT'])
def api_update(id):
data = {}
returnMessage = ""
try:
obj = request.get_json()
except Exception as e:
# swallow up exceptions
logging.exception('Oops!')
return "exception"
capital.store_capital(id, obj)
return "success", 200
@app.route('/api/capitals', methods=['GET'])
def api_list():
query_values = str(request.args.get('query')).split(":")
search_value = request.args.get('search')
print query_values
if query_values == ['None'] and search_value == None:
data = capital.fetch_capitals()
elif search_value == None:
data = capital.fetch_capitals_query(query_values[0], query_values[1])
else:
data = capital.fetch_capitals_query("name", search_value)
data.extend(capital.fetch_capitals_query("continent", search_value))
data.extend(capital.fetch_capitals_query("country", search_value))
return jsonify(data), 200
@app.route('/api/capitals/<id>/publish', methods=['POST'])
def api_publish(id):
try:
obj = request.get_json()
topicName = obj['topic']
capitalData = capital.get_capital(id)
if len(capitalData) <= 0:
return Response(response="{\"code\":404,\"message\":\"Capital record not found\"}", status=404, mimetype="application/json")
myList = topicName.split("/")
pubsub_client = pubsub.Client(project = myList[1])
topic = pubsub_client.topic(myList[3])
message = json.dumps(capitalData[0])
data = message.encode('utf-8')
message_id = topic.publish(data)
return Response(response="{\"messageId\": "+ str(message_id) +"}", status=200, mimetype="application/json")
except Exception as e:
# swallow up exceptions
logging.exception('Unexpected error')
return Response(response="{\"code\":500,\"message\":\"Unexpected error\"}", status=500, mimetype="application/json")
@app.route('/api/capitals/<id>/store', methods=['POST'])
def api_store_capital(id):
try:
data = capital.get_capital(id)
if len(data) > 0:
obj = request.get_json()
bucket_name = obj['bucket']
logging.info("bucket name is {}".format(bucket_name))
storage = Storage(bucket_name)
storage.upload_blob(json.dumps(data[0]), str(id))
return jsonify("success"), 200
else:
return Response(response="{\"code\":404,\"message\":\"not found\"}", status=404, mimetype="application/json")
#except urllib2.HTTPError, err:
except Exception as err:
return Response(response="{\"code\":500,\"message\":\"Unexpected error\"}", status=500, mimetype="application/json")
@app.errorhandler(500)
def server_error(e):
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500
if __name__ == '__main__':
# Used for running locally
app.run(host='127.0.0.1', port=8080, debug=True)