-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathserver.py
More file actions
178 lines (153 loc) · 5.51 KB
/
server.py
File metadata and controls
178 lines (153 loc) · 5.51 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
import traceback
from typing import Optional, Union
from flask import jsonify, Flask, request
import os
from utils.constants import Constants
from utils.database import Database
database_url = os.environ[Constants.DATABASE_URL]
database_replica_set = os.environ[Constants.DATABASE_REPLICA_SET]
database_port = os.environ[Constants.DATABASE_PORT]
database_name = os.environ[Constants.DATABASE_NAME]
db = Database(
database_url = database_url,
database_name = database_name,
database_port = int(database_port),
replica_set = database_replica_set
)
app = Flask(__name__)
@app.route(f'{Constants.MICROSERVICE_URI_PATH}', methods=["POST"])
def create_collection_watcher() -> jsonify:
try:
filename = request.json[Constants.REQUEST_JSON_FILENAME]
except:
return error_response('missing filename value')
try:
observer_type = request.json[Constants.REQUEST_JSON_OBSERVE_TYPE]
except:
return error_response('missing observer_type value')
try:
timeout = request.json[Constants.REQUEST_JSON_TIMEOUT]
except:
timeout = 0
try:
observer_name = request.json[Constants.REQUEST_JSON_OBSERVER_NAME]
except:
observer_name = ''
try:
pipeline = request.json[Constants.REQUEST_JSON_CUSTOM_PIPELINE]
except:
pipeline = []
try:
timeout = int(timeout)
except:
return error_response('invalid timeout value')
try:
cursor_name = db.submit(collection_name=filename,
observer_type=observer_type,
timeout=timeout,
observer_name=observer_name,
pipeline=pipeline)
return successful_response(f'{Constants.MICROSERVICE_URI_PATH}/'
f'{cursor_name}')
except KeyError as ke:
return error_response(str(ke),Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE)
except Exception as e:
return error_response(str(e))
@app.route(f'{Constants.MICROSERVICE_URI_PATH}/<filename>/<observer_name>',
methods=["GET"])
def get_collection_data(filename: str, observer_name: str) -> jsonify:
try:
change = db.watch(collection_name=filename,
observer_name=observer_name)
except ValueError as ve:
return error_response(str(ve),Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE)
except Exception as e:
return error_response(str(e))
if change is None:
return error_response('observer timed out',
code=Constants.HTTP_STATUS_CODE_TIMED_OUT)
return successful_response(result=change)
@app.route(f'{Constants.MICROSERVICE_URI_PATH}/<filename>/<observer_name>',
methods=["PATCH"])
def update_collection_watcher(filename: str, observer_name: str) -> jsonify:
try:
observer_type = request.json[Constants.REQUEST_JSON_OBSERVE_TYPE]
if(observer_type == ""):
observer_type = None
except:
observer_type = None
try:
timeout = request.json[Constants.REQUEST_JSON_TIMEOUT]
if(timeout == ""):
timeout = None
except:
timeout = None
try:
pipeline = request.json[Constants.REQUEST_JSON_CUSTOM_PIPELINE]
if (pipeline == ""):
pipeline = None
except:
pipeline = None
try:
new_observer = request.json[Constants.REQUEST_JSON_OBSERVER_NAME]
if (new_observer == ""):
new_observer = None
except:
new_observer = None
try:
new_collection = \
request.json[Constants.REQUEST_JSON_FILENAME]
if (new_collection == ""):
new_collection = None
except:
new_collection = None
try:
if timeout is not None:
timeout = int(timeout)
except:
return error_response('invalid timeout value')
try:
cursor_name = db.update_watch(filename,observer_name,new_collection,
new_observer, observer_type,timeout,
pipeline)
except ValueError as ve:
return error_response(str(ve),Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE)
except Exception as e:
return error_response(str(e))
return successful_response(f'{Constants.MICROSERVICE_URI_PATH}/'
f'{cursor_name}')
@app.route(f'{Constants.MICROSERVICE_URI_PATH}/<filename>/<observer_name>',
methods=["DELETE"])
def delete_collection_watcher(filename: str, observer_name: str) -> jsonify:
try:
db.remove_watch(collection_name=filename,
observer_name=observer_name)
except ValueError as ve:
return error_response(str(ve),Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE)
except Exception as e:
return error_response(str(e))
return successful_response(Constants.DELETED_MESSAGE)
def error_response(subject: str = '',
code: int=Constants.HTTP_STATUS_CODE_BAD_REQUEST) -> jsonify:
return (
jsonify(
{
Constants.MESSAGE_RESULT: str(subject)
}
),
code
)
def successful_response(result: Union[dict, str]) -> jsonify:
return (
jsonify(
{
Constants.MESSAGE_RESULT: result
}
),
Constants.HTTP_STATUS_CODE_SUCCESS_FULFILLED
)
if __name__ == '__main__':
app.run(
host = os.environ[Constants.MICROSERVICE_IP],
port = int(os.environ[Constants.MICROSERVICE_PORT])
)