11# -*- test-case-name: treq.test.test_api -*-
22from __future__ import annotations
33
4- from typing import TypeVar , Union
4+ from typing import TypeVar
55
6- from hyperlink import DecodedURL , EncodedURL
76from twisted .internet .defer import Deferred
87from twisted .internet .interfaces import IReactorTCP
98from twisted .web .client import Agent , HTTPConnectionPool
1716 _JSONType ,
1817 _Nothing ,
1918 _ParamsType ,
19+ _SomeURL ,
2020)
2121from treq .client import HTTPClient
2222from treq .response import _Response
2525
2626R = TypeVar ("R" )
2727
28- SomeURL = Union [DecodedURL , EncodedURL , str , bytes ]
29-
3028
3129def head (
32- url : SomeURL ,
30+ url : _SomeURL ,
3331 * ,
3432 agent : IAgent | None = None ,
3533 pool : HTTPConnectionPool | None = None ,
@@ -71,7 +69,7 @@ def head(
7169
7270
7371def get (
74- url : SomeURL ,
72+ url : _SomeURL ,
7573 headers : _HeadersType | None = None ,
7674 * ,
7775 agent : IAgent | None = None ,
@@ -113,7 +111,7 @@ def get(
113111
114112
115113def post (
116- url : SomeURL ,
114+ url : _SomeURL ,
117115 data : _DataType | None = None ,
118116 * ,
119117 agent : IAgent | None = None ,
@@ -155,7 +153,7 @@ def post(
155153
156154
157155def put (
158- url : SomeURL ,
156+ url : _SomeURL ,
159157 data : _DataType | None = None ,
160158 * ,
161159 agent : IAgent | None = None ,
@@ -197,7 +195,7 @@ def put(
197195
198196
199197def patch (
200- url : SomeURL ,
198+ url : _SomeURL ,
201199 data : _DataType | None = None ,
202200 * ,
203201 agent : IAgent | None = None ,
@@ -239,7 +237,7 @@ def patch(
239237
240238
241239def delete (
242- url : SomeURL ,
240+ url : _SomeURL ,
243241 * ,
244242 agent : IAgent | None = None ,
245243 pool : HTTPConnectionPool | None = None ,
@@ -280,7 +278,26 @@ def delete(
280278 )
281279
282280
283- def request (method , url , ** kwargs ):
281+ def request (
282+ method : str ,
283+ url : _SomeURL ,
284+ data : _DataType | None = None ,
285+ * ,
286+ agent : IAgent | None = None ,
287+ pool : HTTPConnectionPool | None = None ,
288+ persistent : bool | None = None ,
289+ params : _ParamsType | None = None ,
290+ headers : _HeadersType | None = None ,
291+ files : _FilesType | None = None ,
292+ json : _JSONType | _Nothing = _NOTHING ,
293+ auth : tuple [str | bytes , str | bytes ] | None = None ,
294+ cookies : _CookiesType | None = None ,
295+ allow_redirects : bool = True ,
296+ browser_like_redirects : bool = False ,
297+ unbuffered : bool = False ,
298+ reactor : _ITreqReactor | None = None ,
299+ timeout : float | None = None ,
300+ ) -> Deferred [_Response ]:
284301 """
285302 Make an HTTP request.
286303
@@ -292,27 +309,25 @@ def request(method, url, **kwargs):
292309 :class:`hyperlink.EncodedURL`
293310
294311 :param headers: Optional HTTP Headers to send with this request.
295- :type headers: :class:`~twisted.web.http_headers.Headers` or None
296312
297313 :param params: Optional parameters to be append to the URL query string.
298314 Any query string parameters in the *url* will be preserved.
299- :type params: dict w/ str or list/tuple of str values, list of 2-tuples, or
300- None.
301315
302316 :param data:
303317 Arbitrary request body data.
304318
305319 If *files* is also passed this must be a :class:`dict`,
306320 a :class:`tuple` or :class:`list` of field tuples as accepted by
307- :class:`MultiPartProducer`. The request is assigned a Content-Type of
308- ``multipart/form-data``.
321+ :class:`~treq.multipart. MultiPartProducer`. The request is assigned
322+ a Content-Type of ``multipart/form-data``.
309323
310324 If a :class:`dict`, :class:`list`, or :class:`tuple` it is URL-encoded
311325 and the request assigned a Content-Type of
312326 ``application/x-www-form-urlencoded``.
313327
314328 Otherwise, any non-``None`` value is passed to the client's
315- *data_to_body_producer* callable (by default, :class:`IBodyProducer`),
329+ *data_to_body_producer* callable (by default,
330+ :class:`~twisted.web.iweb.IBodyProducer`),
316331 which accepts :class:`bytes` and binary files like returned by
317332 ``open(..., "rb")``.
318333 :type data: `bytes`, `typing.BinaryIO`, `IBodyProducer`, or `None`
@@ -332,8 +347,8 @@ def request(method, url, **kwargs):
332347
333348 Each ``binary_file`` is a file-like object open in binary mode (like
334349 returned by ``open("filename", "rb")``). The filename is taken from
335- the file's ``name`` attribute if not specified. The Content-Type is
336- guessed based on the filename using :func:`mimetypes.guess_type()` if
350+ the file's ``name`` attribute, if not specified. The Content-Type is
351+ guessed based on the filename using :func:`mimetypes.guess_type()`, if
337352 not specified, falling back to ``application/octet-stream``.
338353
339354 While uploading Treq will measure the length of seekable files to
@@ -352,19 +367,20 @@ def request(method, url, **kwargs):
352367 :type auth: tuple of ``('username', 'password')``
353368
354369 :param cookies: Cookies to send with this request. The HTTP kind, not the
355- tasty kind.
356- :type cookies: ``dict`` or ``cookielib.CookieJar``
370+ tasty kind. If you pass a :class:`dict`, the cookies therein will be
371+ scoped to the origin of *url* (see :func:`~treq.cookies.scoped_cookie()`).
372+ :type cookies: :class:`dict` or :class:`http.cookiejar.CookieJar`
357373
358374 :param int timeout: Request timeout seconds. If a response is not
359375 received within this timeframe, a connection is aborted with
360- `` CancelledError` `.
376+ :exc:`~twisted.internet.defer. CancelledError`.
361377
362378 :param bool allow_redirects: Follow HTTP redirects. Default: ``True``
363379
364380 :param bool browser_like_redirects: Follow redirects like a web browser:
365381 When a 301 or 302 redirect is received in response to a POST request
366382 convert the method to GET.
367- See :rfc:`7231 <7231 #section-6 .4.3>` and
383+ See :rfc:`RFC 9110 <9110 #section-15 .4.3>` and
368384 :class:`~twisted.web.client.BrowserLikeRedirectAgent`). Default: ``False``
369385
370386 :param bool unbuffered: Pass ``True`` to to disable response buffering. By
@@ -387,7 +403,23 @@ def request(method, url, **kwargs):
387403 The *url* param now accepts :class:`hyperlink.DecodedURL` and
388404 :class:`hyperlink.EncodedURL` objects.
389405 """
390- return _client (** kwargs ).request (method , url , _stacklevel = 3 , ** kwargs )
406+ return _client (agent , pool , persistent , reactor ).request (
407+ method ,
408+ url ,
409+ _stacklevel = 3 ,
410+ params = params ,
411+ headers = headers ,
412+ data = data ,
413+ files = files ,
414+ json = json ,
415+ auth = auth ,
416+ cookies = cookies ,
417+ allow_redirects = allow_redirects ,
418+ browser_like_redirects = browser_like_redirects ,
419+ unbuffered = unbuffered ,
420+ reactor = reactor ,
421+ timeout = timeout ,
422+ )
391423
392424
393425#
@@ -432,6 +464,9 @@ def default_pool(reactor, pool, persistent):
432464 if get_global_pool () is None :
433465 set_global_pool (HTTPConnectionPool (reactor , persistent = True ))
434466
467+ # NOTE: This doesn't necessarily return a pool that matches
468+ # the *reactor* parameter, which can produce confusing behavior
469+ # in tests that use a fake reactor.
435470 return get_global_pool ()
436471
437472
0 commit comments