Skip to content

Fix typing problems with http_requests and requests_wrapper #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions ipfshttpclient/http_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import math
import http.client
import os
import requests
import typing as ty
import urllib.parse

Expand All @@ -19,13 +19,8 @@
addr_t, auth_t, cookies_t, headers_t, params_t, reqdata_sync_t, timeout_t,
Closable,
)

PATCH_REQUESTS = (os.environ.get("PY_IPFS_HTTP_CLIENT_PATCH_REQUESTS", "yes").lower()
not in ("false", "no"))
if PATCH_REQUESTS:
from . import requests_wrapper as requests
elif not ty.TYPE_CHECKING: # pragma: no cover (always enabled in production)
import requests
from .requests_wrapper import PATCH_REQUESTS
from .requests_wrapper import wrapped_session


def map_args_to_requests(
Expand Down Expand Up @@ -70,7 +65,7 @@ def map_args_to_requests(
return kwargs


class ClientSync(ClientSyncBase[requests.Session]): # type: ignore[name-defined]
class ClientSync(ClientSyncBase[requests.Session]):
__slots__ = ("_base_url", "_default_timeout", "_request_proxies", "_session_props")
#_base_url: str
#_default_timeout: timeout_t
Expand All @@ -92,7 +87,8 @@ def _init(self, addr: addr_t, base: str, *, # type: ignore[no-any-unimported]
params=params,
)
self._default_timeout = timeout
if PATCH_REQUESTS: # pragma: no branch (always enabled in production)

if PATCH_REQUESTS:
self._session_props["family"] = family

# Ensure that no proxy lookups are done for the UDS pseudo-hostname
Expand All @@ -106,8 +102,8 @@ def _init(self, addr: addr_t, base: str, *, # type: ignore[no-any-unimported]
"no_proxy": urllib.parse.quote(uds_path, safe=""),
}

def _make_session(self) -> requests.Session: # type: ignore[name-defined]
session = requests.Session() # type: ignore[attr-defined]
def _make_session(self) -> requests.Session:
session = wrapped_session()
try:
for name, value in self._session_props.items():
setattr(session, name, value)
Expand All @@ -118,10 +114,10 @@ def _make_session(self) -> requests.Session: # type: ignore[name-defined]
session.close()
raise

def _do_raise_for_status(self, response: requests.Request) -> None: # type: ignore[name-defined]
def _do_raise_for_status(self, response: requests.Response) -> None:
try:
response.raise_for_status()
except requests.exceptions.HTTPError as error: # type: ignore[attr-defined]
except requests.exceptions.HTTPError as error:
content = []
try:
decoder = encoding.get_encoding("json")
Expand Down Expand Up @@ -155,7 +151,7 @@ def _request(
path = path[1:]

url = urllib.parse.urljoin(self._base_url, path)

try:
# Determine session object to use
closables, session = self._access_session()
Expand All @@ -172,13 +168,15 @@ def _request(
timeout=(timeout if timeout is not None else self._default_timeout),
),
proxies=self._request_proxies,
data=data,
# requests.Session.request does not accept Optional[Iterator[bytes]],
# but we seem to be passing that here.
data=data, # type: ignore[arg-type]
stream=True,
)
closables.insert(0, res)
except (requests.ConnectTimeout, requests.Timeout) as error: # type: ignore[attr-defined]
raise exceptions.TimeoutError(error) from error
except requests.ConnectionError as error: # type: ignore[attr-defined]
except requests.ConnectionError as error:
# Report protocol violations separately
#
# This used to happen because requests wouldn't catch
Expand All @@ -196,8 +194,8 @@ def _request(
# (optionally incorporating the response message, if available)
self._do_raise_for_status(res)

return closables, res.iter_content(chunk_size=chunk_size)
return closables, (portion for portion in res.iter_content(chunk_size=chunk_size))
except:
for closable in closables:
closable.close()
raise
raise
Loading