1
1
from httpx import AsyncClient
2
2
from config import GLOBAL_CONFIG
3
3
import httpx
4
+ import json
4
5
5
- def check_slce_response (response : httpx .Response ) -> str :
6
+ def get_response_data (response : httpx .Response ) -> dict :
6
7
if response .status_code != 200 :
7
8
return f"response status code: { response .status_code } "
8
9
9
10
data = response .json ()
10
11
if data ["msg" ] is not None and data ["msg" ] != "" :
11
- return f"request SafeLine API failed: { data ['msg' ]} "
12
+ raise Exception ( f"request SafeLine API failed: { data ['msg' ]} " )
12
13
13
14
if data ["err" ] is not None and data ["err" ] != "" :
14
- return f"request SafeLine API failed: { data ['err' ]} "
15
+ raise Exception (f"request SafeLine API failed: { data ['err' ]} " )
16
+
17
+ return data ['data' ]
18
+
19
+ def check_slce_response (response : httpx .Response ) -> str :
20
+ try :
21
+ get_response_data (response )
22
+ except Exception as e :
23
+ return str (e )
15
24
16
25
return "success"
17
26
27
+
28
+ def check_slce_get_response (response : httpx .Response ) -> str :
29
+ try :
30
+ data = get_response_data (response )
31
+ if data :
32
+ return json .dumps (data )
33
+ return "empty response data"
34
+ except Exception as e :
35
+ return str (e )
36
+
18
37
async def get_slce_api (path : str ) -> str :
19
38
if not path .startswith ("/" ):
20
39
path = f"/{ path } "
@@ -24,7 +43,7 @@ async def get_slce_api(path: str) -> str:
24
43
response = await client .get (f"{ GLOBAL_CONFIG .SAFELINE_ADDRESS } { path } " , headers = {
25
44
"X-SLCE-API-TOKEN" : f"{ GLOBAL_CONFIG .SAFELINE_API_TOKEN } "
26
45
})
27
- return check_slce_response (response )
46
+ return check_slce_get_response (response )
28
47
except Exception as e :
29
48
return str (e )
30
49
0 commit comments