-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
190 lines (174 loc) · 6.25 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
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
186
187
188
189
190
from flask import Flask, request
from settings import APP_HOST, APP_PORT, DEBUG
import logging.config
import json
from controllers import controller
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
# Logging
logging.config.fileConfig('logging.cfg')
logger = logging.getLogger('DATA-MANUFACTURING')
# 0.0.0.0:5000/keys/
class Keys(Resource):
# if api calls all keys
def get(self):
try:
if request.args.get('filter') is not None:
request_filter = request.args.get('filter')
result = controller.get_keys(request_filter)
logger.info('Getting all with filter -->' + str(request_filter))
else:
###
# * means get all keys without search expression
###
result = controller.get_keys('*')
logger.info('Getting all keys')
_response = {
"result": result[0],
"result_type": result[1]
}
response = app.response_class(
response=json.dumps(_response),
status=200,
mimetype='application/json'
)
except Exception as e:
response = app.response_class(
response='request data wrong --->' + str(e),
status=400,
mimetype='application/json'
)
logger.error('request data wrong --->' + str(e))
return response
# if api creating new key, or updating existing key
def put(self):
try:
request_data = request.get_json()
key = request_data['key']
value = request_data['value']
if request.args.get('expire_in') is not None:
expire_time = request.args.get('expire_in') # type --> seconds
result = controller.set_value_expire_time(key, value, expire_time)
else:
result = controller.set_value(key, value)
_response = {
"result": result[0],
"result_type": result[1]
}
response = app.response_class(
response=json.dumps(_response),
status=201,
mimetype='application/json'
)
logger.info('Putting new key')
except Exception as e:
response = app.response_class(
response='request data wrong --->' + str(e),
status=400,
mimetype='application/json'
)
logger.error('request data wrong --->' + str(e))
return response
# if api calls delete all keys
def delete(self):
try:
result = controller.delete_all_keys()
_response = {
"result": result[0],
"result_type": result[1]
}
response = app.response_class(
response=json.dumps(_response),
status=200,
mimetype='application/json'
)
logger.info('Deleting all keys')
except Exception as e:
response = app.response_class(
response='request data wrong --->' + str(e),
status=400,
mimetype='application/json'
)
logger.error('request data wrong --->' + str(e))
return response
# 0.0.0.0:5000/keys/parameter/
class Key(Resource):
# if api calls get a key
def get(self, parameter):
try:
result = controller.get_value(parameter)
if result[0] is not None:
_response = {
"result": result[0],
"result_type": result[1]
}
response = app.response_class(
response=json.dumps(_response),
status=200,
mimetype='application/json'
)
logger.info('Getting a key which -->' + str(parameter))
else:
response = app.response_class(
response='Not found',
status=204,
mimetype='application/json'
)
except Exception as e:
response = app.response_class(
response='request data wrong --->' + str(e),
status=400,
mimetype='application/json'
)
logger.error('request data wrong --->' + str(e))
return response
# if api calls is a key exist
def head(self, parameter):
"""
HEAD does not return response body
200 --> exist
204 --> no content, do not exist
"""
try:
_response = controller.key_exist(parameter)
response = app.response_class(
status=_response,
mimetype='application/json'
)
logger.info('Key checking is exist which' + str(parameter))
except Exception as e:
response = app.response_class(
response='request data wrong --->' + str(e),
status=400,
mimetype='application/json'
)
logger.error('request data wrong --->' + str(e))
return response
# if api calls delete a key
def delete(self, parameter):
try:
result = controller.delete_key(parameter)
_response = {
"result": bool(result[0]),
"result_type": result[1]
}
response = app.response_class(
response=json.dumps(_response),
status=200,
mimetype='application/json'
)
logger.info('Deleting a key which --->' + str(parameter))
except Exception as e:
response = app.response_class(
response='request data wrong --->' + str(e),
status=400,
mimetype='application/json'
)
logger.error('request data wrong --->' + str(e))
return response
api.add_resource(Keys, '/keys/')
api.add_resource(Key, '/keys/<string:parameter>/')
if __name__ == '__main__':
# App host, port, debug mode imported from settings py
app.run(host=APP_HOST, port=APP_PORT, debug=DEBUG)