-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_manager.py
More file actions
119 lines (95 loc) 路 3.85 KB
/
Copy pathcache_manager.py
File metadata and controls
119 lines (95 loc) 路 3.85 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
from flask import Flask, request
import json
import os
import plyvel
import requests
# Time after which an entry in the database will be deleted
TIME_DELETIION = 30
# Set up databases
if not os.path.exists(r'./databases'):
os.makedirs(r'./databases')
request_db = plyvel.DB('databases/request_db', create_if_missing=True)
time_db = plyvel.DB('databases/time_db', create_if_missing=True)
app = Flask(__name__)
@app.route('/', methods=['PUT', 'GET', 'DELETE'])
def start():
if request.method == "GET":
'''
Sends back the requested retailer data for the specific
identifier given
'''
try:
identifier = request.args.get('identifier')
stored_data = request_db.get(bytes(identifier, encoding='utf-8'))
# If data was found, continue
if (stored_data != None):
# Must use [2:-1] on the string because the beginning
# of the string has 'b and the end has '
# In order for it to be a valid dictionary, must remove them
stored_data = json.loads(str(stored_data)[2:-1])
stored_data['success'] = True
return stored_data, 200
else:
return json.dumps({'success': False}), 404
except Exception as e:
print(str(e))
return json.dumps({'success': False}), 404
elif request.method == "PUT":
'''
PUT request will have just the regular JSON response stored in the LevelDB database
'''
try:
response = request.json
# The key that will be stored in the database
identifier = response['identifier']
response['success'] = True
request_db.put(bytes(identifier, encoding='utf-8'), bytes(json.dumps(response), encoding='utf-8'))
time_db.put(bytes(identifier, encoding='utf-8'), bytes([0]))
return json.dumps({'success': True}), 204
except Exception as e:
print(str(e))
return json.dumps({'success': False, 'message': str(e)}), 500
elif request.method == "DELETE":
'''
Given an identifier, delete the resource from the database
'''
try:
identifier = request.json['identifier']
print(identifier)
request_db.delete(bytes(identifier, encoding='utf-8'))
return json.dumps({'success': True}), 200
except Exception as e:
return json.dumps({'success': False, 'message': str(e)}), 500
@app.route('/check', methods=['GET'])
def check_database():
'''
Prints all the keys and values in the database.
'''
if request.method == 'GET':
try:
for key, value in request_db:
print(key, value, int.from_bytes(time_db.get(key), byteorder='big'))
return json.dumps({'success': True}), 200
except Exception as e:
print(str(e))
return json.dumps({'success': False}), 404
@app.route('/update', methods=['GET'])
def time_updater():
'''
Updates how long each entry has been in the database.
Deletes after the amount of minutes have passed as
defined in TIME_DELETION
'''
try:
minute_to_add = int(request.args.get('mins'))
for key, value in time_db:
time_db.put(key, bytes([int.from_bytes(value, byteorder='big') +
int.from_bytes([minute_to_add], byteorder='big')]))
if value == bytes([TIME_DELETIION]):
time_db.delete(key)
requests.delete('http://localhost:5001/', json={'identifier': key.decode('utf-8')})
return json.dumps({'success': True}), 204
except Exception as e:
print(str(e))
if __name__ == "__main__":
app.run(host="localhost", port=5001, threaded=True)