44# you may not use this file except in compliance with the License.
55# You may obtain a copy of the License at
66#
7- # http://www.apache.org/licenses/LICENSE-2.0
7+ # http://www.apache.org/licenses/LICENSE-2.0
88#
99# Unless required by applicable law or agreed to in writing, software
1010# distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,13 +22,17 @@ class HttpClient:
2222 def do (self , request ):
2323 pass
2424
25+
2526class Response :
26- def __init__ (self , status : str , msg : str , data : Union [Dict , List ], data2 : Union [Dict , List ]):
27+ def __init__ (
28+ self , status : str , msg : str , data : Union [Dict , List ], data2 : Union [Dict , List ]
29+ ):
2730 self .status = status
2831 self .msg = msg
2932 self .data = data
3033 self .data2 = data2
3134
35+
3236# Global HTTP client
3337client = requests .Session ()
3438
@@ -49,7 +53,9 @@ def do_get_response(self, url: str) -> Response:
4953 response = json .loads (resp_bytes )
5054 if response ["status" ] != "ok" :
5155 raise Exception (response ["msg" ])
52- return Response (response ["status" ], response ["msg" ], response ["data" ], response ["data2" ])
56+ return Response (
57+ response ["status" ], response ["msg" ], response ["data" ], response ["data2" ]
58+ )
5359
5460 def do_get_bytes (self , url : str ) -> bytes :
5561 response = self .do_get_response (url )
@@ -62,33 +68,42 @@ def do_get_bytes_raw(self, url: str) -> bytes:
6268 raise Exception (response ["msg" ])
6369 return resp_bytes
6470
65- def do_post (self , action : str , query_map : Dict [str , str ], post_bytes : bytes , is_form : bool , is_file : bool ) -> Response :
71+ def do_post (
72+ self ,
73+ action : str ,
74+ query_map : Dict [str , str ],
75+ post_bytes : bytes ,
76+ is_form : bool ,
77+ is_file : bool ,
78+ ) -> Response :
6679 url = util .get_url (self .endpoint , action , query_map )
6780 content_type , body = self .prepare_body (post_bytes , is_form , is_file )
6881 resp_bytes = self .do_post_bytes_raw (url , content_type , body )
6982 response = json .loads (resp_bytes )
7083 if response ["status" ] != "ok" :
7184 raise Exception (response ["msg" ])
72- return Response (response ["status" ], response ["msg" ], response ["data" ], response ["data2" ])
85+ return Response (
86+ response ["status" ], response ["msg" ], response ["data" ], response ["data2" ]
87+ )
7388
7489 def do_post_bytes_raw (self , url : str , content_type : str , body : bytes ) -> bytes :
7590 if not content_type :
7691 content_type = "text/plain;charset=UTF-8"
7792 headers = {
7893 "Content-Type" : content_type ,
79- "Authorization" : f"Basic { self .client_id } :{ self .client_secret } "
94+ "Authorization" : f"Basic { self .client_id } :{ self .client_secret } " ,
8095 }
8196 resp = client .post (url , headers = headers , data = body )
8297 return resp .content
8398
8499 def do_get_bytes_raw_without_check (self , url : str ) -> bytes :
85- headers = {
86- "Authorization" : f"Basic { self .client_id } :{ self .client_secret } "
87- }
100+ headers = {"Authorization" : f"Basic { self .client_id } :{ self .client_secret } " }
88101 resp = client .get (url , headers = headers )
89102 return resp .content
90103
91- def prepare_body (self , post_bytes : bytes , is_form : bool , is_file : bool ) -> Tuple [str , bytes ]:
104+ def prepare_body (
105+ self , post_bytes : bytes , is_form : bool , is_file : bool
106+ ) -> Tuple [str , bytes ]:
92107 if is_form :
93108 if is_file :
94109 return util .create_form_file ({"file" : post_bytes })
0 commit comments