-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
156 lines (139 loc) · 4.83 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
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
import os
from flask import Flask, jsonify, request, abort, make_response
from flask_pymongo import PyMongo, ObjectId
from flask_cors import CORS, cross_origin
import datetime
import json
from bson.json_util import dumps
app = Flask(__name__)
CORS(app)
# HTTP status code constants
HTTP_SUCCESS_GET_OR_UPDATE = 200
HTTP_SUCCESS_CREATED = 201
HTTP_SUCCESS_DELETED = 204
HTTP_SERVER_ERROR = 500
HTTP_NOT_FOUND = 404
HTTP_BAD_REQUEST = 400
# Put your MongoDB connection URL here. Use authSource only if you're authentication against
# a different database than the one you shall be querying for data.
app.config["MONGO_URI"] = "mongodb://username:password@host:port/database?authSource=admin"
mongo = PyMongo(app)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
def send(data, status_code):
"""
Create a Flask response based on the data and status_code received.
"""
return make_response(dumps(data), status_code)
@app.route('/')
def home():
"""
Say Hello to the world!
"""
return send('Hello World!', HTTP_SUCCESS_GET_OR_UPDATE)
@app.route('/time', methods=['GET'])
def server_time():
"""
Return server time. Can be useful in situations where your client wants to sync time.
"""
data = {"time": datetime.datetime.now()}
return send(data, HTTP_SUCCESS_GET_OR_UPDATE)
@app.route('/<collection_name>', methods=['POST'])
def post_item(collection_name):
"""
Post one item in collection.
"""
collection = getattr(mongo.db, collection_name)
formdata = request.json
try:
insert_id = str(collection.insert_one(formdata).inserted_id)
output = {'message': 'new item created', "_id": insert_id}
return send(output, HTTP_SUCCESS_CREATED)
except Exception as e:
output = {'error' : str(e)}
return send(output, HTTP_BAD_REQUEST)
@app.route('/<collection_name>/count', methods=['GET'])
def collection_name_count(collection_name):
"""
Count of number of documents in a collection.
"""
collection = getattr(mongo.db, collection_name)
results = collection.find()
output = {
"count": results.count()
}
return send(output, HTTP_SUCCESS_GET_OR_UPDATE)
@app.route('/<collection_name>', methods=['GET'])
def get_all_items(collection_name):
"""
Documents in a collection.
"""
collection = getattr(mongo.db, collection_name)
output = []
for q in collection.find():
output.append(q)
return send(output, HTTP_SUCCESS_GET_OR_UPDATE)
@app.route('/<collection_name>/<id>', methods=['GET'])
def get_one_item(collection_name, id):
"""
Get one item from a collection.
"""
collection = getattr(mongo.db, collection_name)
r = collection.find_one({'_id': ObjectId(id)})
if r:
return send(r, HTTP_SUCCESS_GET_OR_UPDATE)
else:
return send({'error' : 'item not found'}, HTTP_NOT_FOUND)
@app.route('/<collection_name>/<id>', methods=['PUT'])
def update_item(collection_name, id):
"""
Update one item in collection.
"""
collection = getattr(mongo.db, collection_name)
r = collection.find_one({'_id': ObjectId(id)})
if r:
for key in request.json.keys():
r[key] = request.json[key]
try:
collection.replace_one({"_id": ObjectId(id)}, r)
output = {'message' : 'item updated'}
return send(output, HTTP_SUCCESS_GET_OR_UPDATE)
except Exception as e:
output = {'error' : str(e)}
return send(output, HTTP_BAD_REQUEST)
else:
output = {'error' : 'item not found'}
return send(output, HTTP_NOT_FOUND)
@app.route('/<collection_name>/<id>', methods=['DELETE'])
def delete_item(collection_name, id):
"""
Delete one item from collection.
"""
collection = getattr(mongo.db, collection_name)
r = collection.find_one({'_id': ObjectId(id)})
if r:
try:
collection.remove(r["_id"])
return send("", HTTP_SUCCESS_DELETED)
except Exception as e:
output = {'error' : str(e)}
return send(output, HTTP_BAD_REQUEST)
else:
output = {'error' : 'item not found'}
return send(output, HTTP_NOT_FOUND)
# Error Handler 404
@app.errorhandler(404)
def not_found(error):
return send({'error': 'Not found'}, HTTP_NOT_FOUND)
# Error Handler 500
@app.errorhandler(500)
def internal_server_error(error):
return send({'error': 'Internal Server Error'}, HTTP_SERVER_ERROR)
# Exception
@app.errorhandler(Exception)
def unhandled_exception(error):
try:
return send({'error': str(error)}, HTTP_SERVER_ERROR)
except:
return send({'error': "Unknown error"}, HTTP_SERVER_ERROR)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=4000, debug=True)