Skip to content

Commit

Permalink
Merge branch 'v3.0' of github.com:jawah/niquests into v3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ousret committed Sep 22, 2023
2 parents 897b1d3 + 03fcdf5 commit 96c25ba
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Release History

**Added**
- Oriented-object headers. Access them through the new property `oheaders` in your `Response`.
- Propagated the argument `retries` in `niquests.api` for all functions.
- Added argument `retries` in the `Session` constructor.

**Fixed**
- No configured retry of your HTTP requests but getting exception `MaxRetryError` nonetheless.

3.0.0b0 (2023-09-21)
-------------------
Expand Down
4 changes: 2 additions & 2 deletions src/niquests/_constant.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import wassima

from ._typing import TimeoutType
from ._typing import RetryType, TimeoutType

#: Default timeout (total) assigned for GET, HEAD, and OPTIONS methods.
READ_DEFAULT_TIMEOUT: TimeoutType = 30
Expand All @@ -9,6 +9,6 @@

DEFAULT_POOLBLOCK: bool = False
DEFAULT_POOLSIZE: int = 10
DEFAULT_RETRIES: int = 0
DEFAULT_RETRIES: RetryType = False

DEFAULT_CA_BUNDLE: str = wassima.generate_ca_bundle()
2 changes: 2 additions & 0 deletions src/niquests/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,5 @@
CacheLayerAltSvcType: typing.TypeAlias = typing.MutableMapping[
typing.Tuple[str, int], typing.Optional[typing.Tuple[str, int]]
]

RetryType: typing.TypeAlias = typing.Union[bool, int]
22 changes: 20 additions & 2 deletions src/niquests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import typing

from . import sessions
from ._constant import READ_DEFAULT_TIMEOUT, WRITE_DEFAULT_TIMEOUT
from ._constant import DEFAULT_RETRIES, READ_DEFAULT_TIMEOUT, WRITE_DEFAULT_TIMEOUT
from ._typing import (
BodyType,
CacheLayerAltSvcType,
Expand All @@ -25,6 +25,7 @@
MultiPartFilesType,
ProxyType,
QueryParameterType,
RetryType,
TimeoutType,
TLSClientCertType,
TLSVerifyType,
Expand Down Expand Up @@ -55,6 +56,7 @@ def request(
stream: bool = False,
cert: TLSClientCertType | None = None,
hooks: HookType | None = None,
retries: RetryType = DEFAULT_RETRIES,
) -> Response:
"""Constructs and sends a :class:`Request <Request>`.
Expand Down Expand Up @@ -99,7 +101,9 @@ def request(
# By using the 'with' statement we are sure the session is closed, thus we
# avoid leaving sockets open which can trigger a ResourceWarning in some
# cases, and look like a memory leak in others.
with sessions.Session(quic_cache_layer=_SHARED_QUIC_CACHE) as session:
with sessions.Session(
quic_cache_layer=_SHARED_QUIC_CACHE, retries=retries
) as session:
return session.request(
method=method,
url=url,
Expand Down Expand Up @@ -134,6 +138,7 @@ def get(
stream: bool = False,
cert: TLSClientCertType | None = None,
hooks: HookType | None = None,
retries: RetryType = DEFAULT_RETRIES,
) -> Response:
r"""Sends a GET request.
Expand All @@ -158,6 +163,7 @@ def get(
stream=stream,
cert=cert,
hooks=hooks,
retries=retries,
)


Expand All @@ -175,6 +181,7 @@ def options(
stream: bool = False,
cert: TLSClientCertType | None = None,
hooks: HookType | None = None,
retries: RetryType = DEFAULT_RETRIES,
) -> Response:
r"""Sends an OPTIONS request.
Expand All @@ -197,6 +204,7 @@ def options(
stream=stream,
cert=cert,
hooks=hooks,
retries=retries,
)


Expand All @@ -214,6 +222,7 @@ def head(
stream: bool = False,
cert: TLSClientCertType | None = None,
hooks: HookType | None = None,
retries: RetryType = DEFAULT_RETRIES,
) -> Response:
r"""Sends a HEAD request.
Expand All @@ -236,6 +245,7 @@ def head(
stream=stream,
cert=cert,
hooks=hooks,
retries=retries,
)


Expand All @@ -256,6 +266,7 @@ def post(
stream: bool = False,
cert: TLSClientCertType | None = None,
hooks: HookType | None = None,
retries: RetryType = DEFAULT_RETRIES,
) -> Response:
r"""Sends a POST request.
Expand Down Expand Up @@ -284,6 +295,7 @@ def post(
stream=stream,
cert=cert,
hooks=hooks,
retries=retries,
)


Expand All @@ -304,6 +316,7 @@ def put(
stream: bool = False,
cert: TLSClientCertType | None = None,
hooks: HookType | None = None,
retries: RetryType = DEFAULT_RETRIES,
) -> Response:
r"""Sends a PUT request.
Expand Down Expand Up @@ -332,6 +345,7 @@ def put(
stream=stream,
cert=cert,
hooks=hooks,
retries=retries,
)


Expand All @@ -352,6 +366,7 @@ def patch(
stream: bool = False,
cert: TLSClientCertType | None = None,
hooks: HookType | None = None,
retries: RetryType = DEFAULT_RETRIES,
) -> Response:
r"""Sends a PATCH request.
Expand Down Expand Up @@ -380,6 +395,7 @@ def patch(
stream=stream,
cert=cert,
hooks=hooks,
retries=retries,
)


Expand All @@ -396,6 +412,7 @@ def delete(
stream: bool = False,
cert: TLSClientCertType | None = None,
hooks: HookType | None = None,
retries: RetryType = DEFAULT_RETRIES,
) -> Response:
r"""Sends a DELETE request.
Expand All @@ -417,4 +434,5 @@ def delete(
stream=stream,
cert=cert,
hooks=hooks,
retries=retries,
)
17 changes: 13 additions & 4 deletions src/niquests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from http.cookiejar import CookieJar
from urllib.parse import urljoin, urlparse

from ._constant import READ_DEFAULT_TIMEOUT, WRITE_DEFAULT_TIMEOUT
from ._constant import DEFAULT_RETRIES, READ_DEFAULT_TIMEOUT, WRITE_DEFAULT_TIMEOUT
from ._internal_utils import to_native_string
from ._typing import (
BodyType,
Expand All @@ -32,6 +32,7 @@
MultiPartFilesType,
ProxyType,
QueryParameterType,
RetryType,
TimeoutType,
TLSClientCertType,
TLSVerifyType,
Expand Down Expand Up @@ -165,7 +166,12 @@ class Session:
"quic_cache_layer",
]

def __init__(self, quic_cache_layer: CacheLayerAltSvcType | None = None):
def __init__(
self,
*,
quic_cache_layer: CacheLayerAltSvcType | None = None,
retries: RetryType = DEFAULT_RETRIES,
):
#: A case-insensitive dictionary of headers to be sent on each
#: :class:`Request <Request>` sent from this
#: :class:`Session <Session>`.
Expand Down Expand Up @@ -228,8 +234,11 @@ def __init__(self, quic_cache_layer: CacheLayerAltSvcType | None = None):

# Default connection adapters.
self.adapters: OrderedDict[str, BaseAdapter] = OrderedDict()
self.mount("https://", HTTPAdapter(quic_cache_layer=self.quic_cache_layer))
self.mount("http://", HTTPAdapter())
self.mount(
"https://",
HTTPAdapter(quic_cache_layer=self.quic_cache_layer, max_retries=retries),
)
self.mount("http://", HTTPAdapter(max_retries=retries))

def __enter__(self):
return self
Expand Down

0 comments on commit 96c25ba

Please sign in to comment.