forked from Daniel1464/DSMC-Contest-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataStorageAPI.py
More file actions
37 lines (32 loc) · 1.5 KB
/
dataStorageAPI.py
File metadata and controls
37 lines (32 loc) · 1.5 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
import ast
import requests
import os
from customExceptions import DataAPIException
class DataStorageAPI:
def __init__(self,passwordStringKey: str = "password"):
self.passwordStringKey = passwordStringKey
self.session = requests.Session()
self.session.get("https://data-storage-system.danielchen1464.repl.co")
def getValue(self,key: str,evaluate: bool = False):
data = self.session.get("https://data-storage-system.danielchen1464.repl.co/database?password={passcode}&key={inputKey}".format(passcode = os.environ[self.passwordStringKey], inputKey = key))
if str(data) == "<Response [200]>":
if evaluate:
return ast.literal_eval(str(data.content)[2:-1])
else:
return data.content[2:-1]
else:
raise DataAPIException
return
def setValue(self,key:str,value):
input = str(value)
data = self.session.post("https://data-storage-system.danielchen1464.repl.co/database?password={passcode}&key={inputKey}".format(passcode = os.environ[self.passwordStringKey], inputKey = key), data = {"value":input})
if str(data) == "<Response [200]>":
return data.content[2:-1]
else:
raise DataAPIException
def delValue(self,key:str):
data = self.session.get("https://data-storage-system.danielchen1464.repl.co/delete_key?password={passcode}&key={inputKey}".format(passcode = os.environ[self.passwordStringKey], inputKey = key))
if str(data) == "<Response [200]>":
return data.content[2:-1]
else:
raise DataAPIException