-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbromide.py
52 lines (38 loc) · 1.73 KB
/
bromide.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
import requests, aiohttp, typing
class ResultDict(typing.TypedDict):
result: str | int | list | bool | None
class Bromide:
def __init__(self, url: str = "http://127.0.0.1:8080", password: str = ""):
self.url = url
self.headers = {"Authorization": password, "Content-Type": "application/json"}
def request(self, data, url_ext):
r = requests.post(f"{self.url}{url_ext}", headers=self.headers, json=data)
if r.status_code != 200:
return None
else:
return r.json()
def read(self, data: str) -> typing.Optional[ResultDict]:
return self.request({"entry": data}, "/read")
def create(self, data: dict) -> dict:
return self.request(data, "/create")
def delete(self, data: str) -> dict:
return self.request({"entry": data}, "/delete")
class AsyncBromide:
def __init__(self, url: str = "http://127.0.0.1:8080", password: str = ""):
self.url = url
self.headers = {"Authorization": password, "Content-Type": "application/json"}
async def request(self, data, url_ext):
async with aiohttp.ClientSession() as cs:
async with cs.post(
f"{self.url}{url_ext}", json=data, headers=self.headers
) as resp:
if resp.status != 200:
return None
else:
return await resp.json()
async def read(self, data: str) -> typing.Optional[ResultDict]:
return await self.request({"entry": data}, "/read")
async def create(self, data: dict) -> dict:
return await self.request(data, "/create")
async def delete(self, data: str) -> typing.Optional[dict]:
return await self.request({"entry": data}, "/delete")