-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (47 loc) · 1.49 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
import json
import socketio
from werkzeug.exceptions import BadRequestKeyError
from resources.operations import KeyValueOperations
from resources.stream import socket_server
from resources.helpers import generate_secret, HelperFunction
from flask import Flask, request
app = Flask(__name__)
# Adding socket to flask as WSGI App
app.wsgi_app = socketio.WSGIApp(socket_server, app.wsgi_app)
# Setting secret key
app.config['SECRET_KEY'] = generate_secret()
# Get Value request
@app.route('/', methods=['GET'])
def get():
"""
This function return the value for provided key in the request args.
"""
try:
KVObj = KeyValueOperations()
return KVObj.getValue(request.args['key'])
except BadRequestKeyError:
return "Error: Missing argument 'KEY'", 400
# Get All value request
@app.route('/all', methods=['GET'])
def get_all():
"""
This function returns all the key value pairs stored.
"""
try:
return json.dumps(HelperFunction().loaddata(), sort_keys=True, indent=4)
except Exception as error:
return format(error), 502
# Put key value request
@app.route('/', methods=['PUT'])
def put():
"""
This function creates/updates the key value pair provided.
"""
try:
KVObj = KeyValueOperations()
data = json.loads(request.data)
return KVObj.putKeyValue(data['key'], data['value'])
except Exception as error:
raise SystemExit(error)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)