-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
142 lines (124 loc) · 6.36 KB
/
main.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
#
# Setup
#
# Imports
from flask import Flask, request, Response
from waitress import serve
from pymongo import MongoClient
from bson import json_util, ObjectId
from os import environ
from urllib import parse
# Environment vars
dbhost = environ['HOST']
dbuser = environ['USER']
dbpass = environ['PASS']
dbname = environ['DATABASE']
# Database setup
mongodb_client = MongoClient('mongodb://' + parse.quote_plus(dbuser) + ':' + parse.quote_plus(dbpass) + '@' + parse.quote_plus(dbhost))
database = mongodb_client[dbname]
# Flask setup
app = Flask(__name__)
#
# Routes
#
# Get entries
@app.route('/<collection>',methods = ['GET'])
def get_many(collection):
try:
number = request.args.get('number', default = 100, type = int)
skip = request.args.get('skip', default = 0, type = int)
filter = request.args.get('filter', default = "{}", type = str)
data = json_util.dumps(database[collection].find(limit=number, skip=skip, filter=json_util.loads(filter)))
return Response(data, mimetype='application/json', status=200)
except Exception as e:
app.logger.error(e)
return Response('Failed. Check collection and filter are valid. Check database logs for errors.\n' + str(e) + '\n', mimetype='text/plain', status=500)
@app.route('/<collection>/<id>',methods = ['GET'])
def get_one(collection, id):
try:
data = json_util.dumps(database[collection].find_one({'_id': ObjectId(id)}))
return Response(data, mimetype='application/json', status=200)
except Exception as e:
app.logger.error(e)
return Response('Failed. Check collection and id are valid. Check database logs for errors.\n' + str(e) + '\n', mimetype='text/plain', status=500)
# Update entries
@app.route('/<collection>',methods = ['PATCH'])
def update_many(collection):
try:
data = json_util.loads(request.data)
filter = request.args.get('filter', default = "{}", type = str)
result = database[collection].update_many(json_util.loads(filter), data)
res = json_util.dumps({"matched": result.matched_count, "modified": result.modified_count})
return Response(res, mimetype='application/json', status=200)
except Exception as e:
app.logger.error(e)
return Response('Failed. Check json body, collection and filter are valid. Check database logs for errors.\n' + str(e) + '\n', mimetype='text/plain', status=500)
@app.route('/<collection>/<id>',methods = ['PATCH'])
def update_one(collection, id):
try:
data = json_util.loads(request.data)
result = database[collection].update_one({'_id': ObjectId(id)}, data)
res = json_util.dumps({'_id': ObjectId(id)})
return Response(res, mimetype='application/json', status=200)
except Exception as e:
app.logger.error(e)
return Response('Failed. Check json body, collection and id are valid. Check database logs for errors.\n' + str(e) + '\n', mimetype='text/plain', status=500)
# Replace entry
@app.route('/<collection>/<id>',methods = ['PUT'])
def replace_one(collection, id):
try:
data = json_util.loads(request.data)
result = database[collection].replace_one({'_id': ObjectId(id)}, data)
res = json_util.dumps({"matched": result.matched_count, "modified": result.modified_count})
return Response(res, mimetype='application/json', status=200)
except Exception as e:
app.logger.error(e)
return Response('Failed. Check json body, collection and id are valid. Check database logs for errors.\n' + str(e) + '\n', mimetype='text/plain', status=500)
# Delete entries
@app.route('/<collection>',methods = ['DELETE'])
def delete_many(collection):
try:
filter = request.args.get('filter', default = "{}", type = str)
result = database[collection].delete_many(json_util.loads(filter))
res = json_util.dumps({"deleted": result.deleted_count})
return Response(res, mimetype='application/json', status=200)
except Exception as e:
app.logger.error(e)
return Response('Failed. Check collection and filter are valid. Check database logs for errors.\n' + str(e) + '\n', mimetype='text/plain', status=500)
@app.route('/<collection>/<id>',methods = ['DELETE'])
def delete_one(collection, id):
try:
result = database[collection].delete_one({'_id': ObjectId(id)})
res = json_util.dumps({'_id': ObjectId(id)})
return Response(res, mimetype='application/json', status=200)
except Exception as e:
app.logger.error(e)
return Response('Failed. Check collection and id are valid. Check database logs for errors.\n' + str(e) + '\n', mimetype='text/plain', status=500)
# Add entries
@app.route('/<collection>',methods = ['POST'])
def add(collection):
try:
data = json_util.loads(request.data)
result = database[collection].insert_one(data)
res = json_util.dumps({"_id": ObjectId(result.inserted_id)})
return Response(res, mimetype='application/json', status=200)
except Exception as e:
app.logger.error(e)
return Response('Failed. Check body json body and collection are valid. Check database logs for errors.\n' + str(e) + '\n', mimetype='text/plain', status=500)
# Fallback (help)
@app.errorhandler(404)
def page_not_found(error):
return Response('Usage:\n' \
' GET /<collection>?number=<n>&skip=<s>&filter=<f> => get n entries in collection that match f (skip s)\n' \
' GET /<collection>/<id> => get entry in collection with id\n' \
' POST /<collection> [json] => add an entry to collection using json\n' \
' PATCH /<collection>?filter=<f> [json] => update the entries in collection that match f using json\n' \
' PATCH /<collection>/<id> [json] => update the entry in collection with id using json\n' \
' PUT /<collection>/<id> [json] => replace the entry in collection with id using json\n' \
' DELETE /<collection>?filter=<f> => delete all entries in collection that match f\n' \
' DELETE /<collection>/<id> => delete the entry in collection with id\n' \
'', mimetype='text/plain', status=404)
#
# Start
#
serve(app, host='0.0.0.0', port=8000)