Skip to content

Commit

Permalink
Typings for treq.response._Response
Browse files Browse the repository at this point in the history
And fix a CookieAgent failure.

Fixes #384.
  • Loading branch information
twm committed Apr 30, 2024
1 parent f0c668e commit 9e07ffb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
Empty file added changelog.d/384.misc.rst
Empty file.
45 changes: 34 additions & 11 deletions src/treq/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
from collections import abc
from http.cookiejar import Cookie, CookieJar
from json import dumps as json_dumps
from typing import (Any, Callable, Iterable, Iterator, List, Mapping,
Optional, Tuple, Union)
from typing import (
Any,
Callable,
Iterable,
Iterator,
List,
Mapping,
Optional,
Tuple,
Union,
)
from urllib.parse import quote_plus
from urllib.parse import urlencode as _urlencode

Expand All @@ -15,16 +24,30 @@
from twisted.internet.interfaces import IProtocol
from twisted.python.components import proxyForInterface, registerAdapter
from twisted.python.filepath import FilePath
from twisted.web.client import (BrowserLikeRedirectAgent, ContentDecoderAgent,
CookieAgent, FileBodyProducer, GzipDecoder,
IAgent, RedirectAgent)
from twisted.web.client import (
BrowserLikeRedirectAgent,
ContentDecoderAgent,
CookieAgent,
FileBodyProducer,
GzipDecoder,
IAgent,
RedirectAgent,
)
from twisted.web.http_headers import Headers
from twisted.web.iweb import IBodyProducer, IResponse

from treq import multipart
from treq._types import (_CookiesType, _DataType, _FilesType, _FileValue,
_HeadersType, _ITreqReactor, _JSONType, _ParamsType,
_URLType)
from treq._types import (
_CookiesType,
_DataType,
_FilesType,
_FileValue,
_HeadersType,
_ITreqReactor,
_JSONType,
_ParamsType,
_URLType,
)
from treq.auth import add_auth
from treq.response import _Response

Expand Down Expand Up @@ -254,8 +277,8 @@ def request(
if not isinstance(cookies, CookieJar):
cookies = _scoped_cookiejar_from_dict(parsed_url, cookies)

cookies = merge_cookies(self._cookiejar, cookies)
wrapped_agent: IAgent = CookieAgent(self._agent, cookies)
jar: CookieJar = merge_cookies(self._cookiejar, cookies)
wrapped_agent: IAgent = CookieAgent(self._agent, jar)

if allow_redirects:
if browser_like_redirects:
Expand Down Expand Up @@ -289,7 +312,7 @@ def gotResult(result):
if not unbuffered:
d.addCallback(_BufferedResponse)

return d.addCallback(_Response, cookies)
return d.addCallback(_Response, jar)

def _request_headers(
self, headers: Optional[_HeadersType], stacklevel: int
Expand Down
33 changes: 17 additions & 16 deletions src/treq/response.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Any, Callable, List
from requests.cookies import cookiejar_from_dict
from http.cookiejar import CookieJar
from twisted.internet.defer import Deferred
from twisted.python import reflect
from twisted.python.components import proxyForInterface
from twisted.web.iweb import UNKNOWN_LENGTH, IResponse
Expand All @@ -12,11 +15,14 @@ class _Response(proxyForInterface(IResponse)): # type: ignore
adds a few convenience methods.
"""

def __init__(self, original, cookiejar):
original: IResponse
_cookiejar: CookieJar

def __init__(self, original: IResponse, cookiejar: CookieJar):
self.original = original
self._cookiejar = cookiejar

def __repr__(self):
def __repr__(self) -> str:
"""
Generate a representation of the response which includes the HTTP
status code, Content-Type header, and body size, if available.
Expand All @@ -38,7 +44,7 @@ def __repr__(self):
size,
)

def collect(self, collector):
def collect(self, collector: Callable[[bytes], None]) -> "Deferred[None]":
"""
Incrementally collect the body of the response, per
:func:`treq.collect()`.
Expand All @@ -51,7 +57,7 @@ def collect(self, collector):
"""
return collect(self.original, collector)

def content(self):
def content(self) -> "Deferred[bytes]":
"""
Read the entire body all at once, per :func:`treq.content()`.
Expand All @@ -60,7 +66,7 @@ def content(self):
"""
return content(self.original)

def json(self, **kwargs):
def json(self, **kwargs: Any) -> "Deferred[Any]":
"""
Collect the response body as JSON per :func:`treq.json_content()`.
Expand All @@ -71,7 +77,7 @@ def json(self, **kwargs):
"""
return json_content(self.original, **kwargs)

def text(self, encoding="ISO-8859-1"):
def text(self, encoding: str = "ISO-8859-1") -> "Deferred[str]":
"""
Read the entire body all at once as text, per
:func:`treq.text_content()`.
Expand All @@ -81,13 +87,11 @@ def text(self, encoding="ISO-8859-1"):
"""
return text_content(self.original, encoding)

def history(self):
def history(self) -> "List[_Response]":
"""
Get a list of all responses that (such as intermediate redirects),
that ultimately ended in the current response. The responses are
ordered chronologically.
:returns: A `list` of :class:`~treq.response._Response` objects
"""
response = self
history = []
Expand All @@ -99,16 +103,13 @@ def history(self):
history.reverse()
return history

def cookies(self):
def cookies(self) -> CookieJar:
"""
Get a copy of this response's cookies.
:rtype: :class:`requests.cookies.RequestsCookieJar`
"""
jar = cookiejar_from_dict({})
jar: CookieJar = cookiejar_from_dict({})

if self._cookiejar is not None:
for cookie in self._cookiejar:
jar.set_cookie(cookie)
for cookie in self._cookiejar:
jar.set_cookie(cookie)

return jar

0 comments on commit 9e07ffb

Please sign in to comment.