11from httpx import AsyncClient
22from config import GLOBAL_CONFIG
33import httpx
4+ import json
45
5- def check_slce_response (response : httpx .Response ) -> str :
6+ def get_response_data (response : httpx .Response ) -> dict :
67 if response .status_code != 200 :
78 return f"response status code: { response .status_code } "
89
910 data = response .json ()
1011 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' ]} " )
1213
1314 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 )
1524
1625 return "success"
1726
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+
1837async def get_slce_api (path : str ) -> str :
1938 if not path .startswith ("/" ):
2039 path = f"/{ path } "
@@ -24,7 +43,7 @@ async def get_slce_api(path: str) -> str:
2443 response = await client .get (f"{ GLOBAL_CONFIG .SAFELINE_ADDRESS } { path } " , headers = {
2544 "X-SLCE-API-TOKEN" : f"{ GLOBAL_CONFIG .SAFELINE_API_TOKEN } "
2645 })
27- return check_slce_response (response )
46+ return check_slce_get_response (response )
2847 except Exception as e :
2948 return str (e )
3049
0 commit comments