-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMongoDB API.py
More file actions
116 lines (95 loc) · 4.14 KB
/
MongoDB API.py
File metadata and controls
116 lines (95 loc) · 4.14 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
from flask import Flask, request, json, Response
from pymongo import MongoClient
import logging as log
app = Flask(__name__)
class MongoAPI:
def __init__(self, data):
log.basicConfig(level=log.DEBUG, format='%(asctime)s %(levelname)s:\n%(message)s\n')
# self.client = MongoClient("mongodb://localhost:27017/") # When only Mongo DB is running on Docker.
self.client = MongoClient("mongodb://mymongo_1:27017/") # When both Mongo and This application is running on
# Docker and we are using Docker Compose
database = data['database']
collection = data['collection']
cursor = self.client[database]
self.collection = cursor[collection]
self.data = data
def read(self):
log.info('Reading All Data')
documents = self.collection.find()
output = [{item: data[item] for item in data if item != '_id'} for data in documents]
return output
def write(self, data):
log.info('Writing Data')
new_document = data['Document']
response = self.collection.insert_one(new_document)
output = {'Status': 'Successfully Inserted',
'Document_ID': str(response.inserted_id)}
return output
def update(self):
log.info('Updating Data')
filt = self.data['Filter']
updated_data = {"$set": self.data['DataToBeUpdated']}
response = self.collection.update_one(filt, updated_data)
output = {'Status': 'Successfully Updated' if response.modified_count > 0 else "Nothing was updated."}
return output
def delete(self, data):
log.info('Deleting Data')
filt = data['Filter']
response = self.collection.delete_one(filt)
output = {'Status': 'Successfully Deleted' if response.deleted_count > 0 else "Document not found."}
return output
@app.route('/')
def base():
return Response(response=json.dumps({"Status": "UP"}),
status=200,
mimetype='application/json')
@app.route('/mongodb', methods=['GET'])
def mongo_read():
data = request.json
if data is None or data == {}:
return Response(response=json.dumps({"Error": "Please provide connection information"}),
status=400,
mimetype='application/json')
obj1 = MongoAPI(data)
response = obj1.read()
return Response(response=json.dumps(response),
status=200,
mimetype='application/json')
@app.route('/mongodb', methods=['POST'])
def mongo_write():
data = request.json
if data is None or data == {} or 'Document' not in data:
return Response(response=json.dumps({"Error": "Please provide connection information"}),
status=400,
mimetype='application/json')
obj1 = MongoAPI(data)
response = obj1.write(data)
return Response(response=json.dumps(response),
status=200,
mimetype='application/json')
@app.route('/mongodb', methods=['PUT'])
def mongo_update():
data = request.json
if data is None or data == {} or 'Filter' not in data:
return Response(response=json.dumps({"Error": "Please provide connection information"}),
status=400,
mimetype='application/json')
obj1 = MongoAPI(data)
response = obj1.update()
return Response(response=json.dumps(response),
status=200,
mimetype='application/json')
@app.route('/mongodb', methods=['DELETE'])
def mongo_delete():
data = request.json
if data is None or data == {} or 'Filter' not in data:
return Response(response=json.dumps({"Error": "Please provide connection information"}),
status=400,
mimetype='application/json')
obj1 = MongoAPI(data)
response = obj1.delete(data)
return Response(response=json.dumps(response),
status=200,
mimetype='application/json')
if __name__ == '__main__':
app.run(debug=True, port=5001, host='0.0.0.0')