38
38
DEFAULT_UA = "Python " + sys .version .split ()[0 ]
39
39
40
40
Headers = MutableMapping [str , str ] | HTTPMessage
41
+ JsonValue = None | bool | int | float | str | list ["JsonValue" ] | dict [str , "JsonValue" ]
41
42
42
43
43
44
def request (method , url , * , read_limit = None , ** kwargs ):
@@ -145,7 +146,7 @@ def yield_response(
145
146
:return: http.client.HTTPResponse, yielded as context manager
146
147
:rtype: http.client.HTTPResponse
147
148
:raises: HTTPException
148
- """
149
+ """ # noqa: E501
149
150
method = method .upper ()
150
151
headers = cast ("MutableMapping[str, str]" , _prepare_outgoing_headers (headers ))
151
152
enc_params = _prepare_params (params )
@@ -203,45 +204,51 @@ class Response:
203
204
204
205
__slots__ = ("body" , "headers" , "status_code" , "url" )
205
206
206
- def __init__ (self , url , status_code , headers , body ):
207
+ def __init__ (
208
+ self ,
209
+ url : str ,
210
+ status_code : int ,
211
+ headers : Headers ,
212
+ body : bytes ,
213
+ ) -> None :
207
214
self .url , self .status_code , self .headers , self .body = (
208
215
url ,
209
216
status_code ,
210
217
headers ,
211
218
body ,
212
219
)
213
220
214
- def __repr__ (self ):
221
+ def __repr__ (self ) -> str :
215
222
return f"Response(status_code={ self .status_code :d} )"
216
223
217
224
@property
218
- def ok (self ):
225
+ def ok (self ) -> bool :
219
226
"""Ok returns whether the response had a successful status code
220
227
(anything other than a 40x or 50x).
221
228
"""
222
229
return not (400 <= self .status_code < 600 )
223
230
224
231
@property
225
- def content (self ):
232
+ def content (self ) -> bytes :
226
233
"""Content returns the response body (the `body` member). This is an
227
234
alias for compatibility with requests.Response.
228
235
"""
229
236
return self .body
230
237
231
- def raise_for_status (self ):
238
+ def raise_for_status (self ) -> None :
232
239
"""raise_for_status checks the response's success code, raising an
233
240
exception for error codes.
234
241
"""
235
242
if not self .ok :
236
243
raise HTTPErrorStatus (self .status_code )
237
244
238
- def json (self ):
245
+ def json (self ) -> JsonValue :
239
246
"""Attempts to deserialize the response body as UTF-8 encoded JSON."""
240
247
import json as jsonlib
241
248
242
249
return jsonlib .loads (self .body )
243
250
244
- def _debugstr (self ):
251
+ def _debugstr (self ) -> str :
245
252
buf = io .StringIO ()
246
253
print ("HTTP" , self .status_code , file = buf )
247
254
for k , v in self .headers .items ():
@@ -267,10 +274,10 @@ class HTTPErrorStatus(HTTPException):
267
274
called explicitly.
268
275
"""
269
276
270
- def __init__ (self , status_code ) :
277
+ def __init__ (self , status_code : int ) -> None :
271
278
self .status_code = status_code
272
279
273
- def __str__ (self ):
280
+ def __str__ (self ) -> str :
274
281
return f"HTTP response returned error code { self .status_code :d} "
275
282
276
283
@@ -285,11 +292,11 @@ class UnixHTTPConnection(HTTPConnection):
285
292
Unix domain stream socket instead of a TCP address.
286
293
"""
287
294
288
- def __init__ (self , path , timeout = DEFAULT_TIMEOUT ):
289
- super (UnixHTTPConnection , self ).__init__ ("localhost" , timeout = timeout )
295
+ def __init__ (self , path : str , timeout : float = DEFAULT_TIMEOUT ) -> None :
296
+ super ().__init__ ("localhost" , timeout = timeout )
290
297
self ._unix_path = path
291
298
292
- def connect (self ):
299
+ def connect (self ) -> None :
293
300
sock = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
294
301
try :
295
302
sock .settimeout (self .timeout )
@@ -300,7 +307,7 @@ def connect(self):
300
307
self .sock = sock
301
308
302
309
303
- def _check_redirect (url , status , response_headers ) :
310
+ def _check_redirect (url : str , status : int , response_headers : Headers ) -> str | None :
304
311
"""Return the URL to redirect to, or None for no redirection."""
305
312
if status not in (301 , 302 , 303 , 307 , 308 ):
306
313
return None
0 commit comments