-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathwebapi.py
74 lines (58 loc) · 1.76 KB
/
webapi.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
import re
import json
import httpx
from typing import List
from typing import Optional, cast
def handle_response_json(res: dict) -> Optional[dict]:
if res["status"] == "error":
if match := re.search(
r"Seed is not correct, should be ([0-9a-f]+)\.",
res["message"]
):
res["seed"] = match.group(1)
return res
if match := re.search(
r"No win. The recipe should be (\[(\d+\,)*\d+\])\.",
res["message"]
):
res["recipe"] = json.loads(match.group(1))
return res
return None
class CyberCookApi:
url: str
level: int
tolen: str
client: httpx.AsyncClient
def __init__(self, url: str, level: int, token: str):
self.url = url
self.level = level
self.token = token
self.client = httpx.AsyncClient()
async def __aenter__(self) -> "CyberCookApi":
return self
async def __aexit__(self, exc_type, exc_value, traceback) -> bool:
await self.aclose()
return False
async def aclose(self) -> None:
await self.client.aclose()
async def verify(
self,
programs: List[str],
executions: List[int],
seed: str
) -> dict:
req_json = {
"level": self.level,
"token": self.token,
"programs": programs,
"executions": executions,
"seed": seed,
}
r = await self.client.post(self.url, json=req_json)
try:
res_json: dict = r.json()
except Exception as e:
raise ValueError(r, r.text) from e
if handled_json := handle_response_json(res_json.copy()):
return handled_json
raise ValueError(r, res_json)