4
4
# you may not use this file except in compliance with the License.
5
5
# You may obtain a copy of the License at
6
6
#
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
8
#
9
9
# Unless required by applicable law or agreed to in writing, software
10
10
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,13 +22,17 @@ class HttpClient:
22
22
def do (self , request ):
23
23
pass
24
24
25
+
25
26
class 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
+ ):
27
30
self .status = status
28
31
self .msg = msg
29
32
self .data = data
30
33
self .data2 = data2
31
34
35
+
32
36
# Global HTTP client
33
37
client = requests .Session ()
34
38
@@ -49,7 +53,9 @@ def do_get_response(self, url: str) -> Response:
49
53
response = json .loads (resp_bytes )
50
54
if response ["status" ] != "ok" :
51
55
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
+ )
53
59
54
60
def do_get_bytes (self , url : str ) -> bytes :
55
61
response = self .do_get_response (url )
@@ -62,33 +68,42 @@ def do_get_bytes_raw(self, url: str) -> bytes:
62
68
raise Exception (response ["msg" ])
63
69
return resp_bytes
64
70
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 :
66
79
url = util .get_url (self .endpoint , action , query_map )
67
80
content_type , body = self .prepare_body (post_bytes , is_form , is_file )
68
81
resp_bytes = self .do_post_bytes_raw (url , content_type , body )
69
82
response = json .loads (resp_bytes )
70
83
if response ["status" ] != "ok" :
71
84
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
+ )
73
88
74
89
def do_post_bytes_raw (self , url : str , content_type : str , body : bytes ) -> bytes :
75
90
if not content_type :
76
91
content_type = "text/plain;charset=UTF-8"
77
92
headers = {
78
93
"Content-Type" : content_type ,
79
- "Authorization" : f"Basic { self .client_id } :{ self .client_secret } "
94
+ "Authorization" : f"Basic { self .client_id } :{ self .client_secret } " ,
80
95
}
81
96
resp = client .post (url , headers = headers , data = body )
82
97
return resp .content
83
98
84
99
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 } " }
88
101
resp = client .get (url , headers = headers )
89
102
return resp .content
90
103
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 ]:
92
107
if is_form :
93
108
if is_file :
94
109
return util .create_form_file ({"file" : post_bytes })
0 commit comments