-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpApiUTIL.py
101 lines (96 loc) · 2.57 KB
/
httpApiUTIL.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
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
import json
import urllib.request as ureq
from custlang import *
APIROOTURI="public.ghs.wiki:7001"
from logging import basicConfig,getLogger
import logging
basicConfig(format="[%(asctime)s] [%(name)s] [%(levelname)s] -> %(message)s",level=logging.INFO)
api_log=getLogger(name="API")
logined=False
#del(dict)
"""
Returns:
return[0]
-1: http error
1: no error, request sucess
other: status code def by MossFrp
return[1]
if success, the returned dict
if error, the err msg mapping
"""
token=""
errmap={
400: langmap["argv_miss_wrong"],
401: langmap["token_err"],
423: langmap["blacklisted"]
}
sp_map={
"login":{ 404: langmap["user_login_fail"] },
"usercode": {},
"nodelist": {},
"rmcode": {},
"createcode": {},
"verifycode": {},
"reg": {},
"infoupdate": {},
"userinfo": {},
"coderenewal": {},
"codeaddband": {}
}
def reqAPI(rtype : str,cargs : dict[str,str]):
global token
api_req_uri="http://"+APIROOTURI+"/API?"
#newargs=cargs.copy()
newargs=cargs
newargs["type"]=rtype
if rtype!="login":
newargs["token"]=token
for itm in newargs.keys():
api_req_uri+=itm
api_req_uri+="="+newargs[itm]
api_req_uri+="&"
api_req_uri=api_req_uri[:-1]
#api_log.info(api_req_uri)
req=ureq.urlopen(api_req_uri)
#print(req.raw)
if req.getcode()!=200:
return (-1,"HTTP Protocol Error: "+str(req.getcode()))
retdat=json.loads(req.read()) # type: ignore
#api_log.info(retdat)
resstat=int(retdat["status"])
if(resstat==200): return (1,retdat)
if resstat in errmap.keys():
return_info=errmap[resstat]
elif resstat in sp_map[rtype].keys():
return_info=sp_map[rtype][resstat]
else: return_info=langmap["unknown_stat"] #"Unknown status"
return (resstat,return_info)
def m_login(loginType:str,acnt:str,passwd:str):
data={} #dict()
data["loginType"]=loginType
data["password"]=passwd
data["account"]=acnt
r=reqAPI("login",data) # r[0] return code; r[1] msg or data
if r[0]==1:
global token
token=r[1]["token"] #type:ignore # we love pylance
api_log.debug(str(r[0])+str(r[1]))
global logined
logined=True
return True
else:
api_log.warning(str(r[0])+": "+r[1])
return False
def m_logout(): # just simply reset token?
global token
global logined
logined=False
token=""
def userCodeList():
r=reqAPI("userCode",{})
if r[0]==1:
api_log.debug(str(r[0])+str(r[1]))
return True,list(r[1]["codeData"].values())
else:
api_log.warning(str(r[0])+": "+r[1])
return False