Skip to content

Commit 703b0d8

Browse files
committed
feat: httpc_params_loader (httpc default timeout refactor)
1 parent e3a6c43 commit 703b0d8

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

src/cryptojwt/key_bundle.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
from .jwk.rsa import RSAKey
3030
from .jwk.rsa import new_rsa_key
3131
from .utils import as_unicode
32+
from .utils import httpc_params_loader
3233

3334
__author__ = "Roland Hedberg"
3435

3536
KEYLOADERR = "Failed to load %s key from '%s' (%s)"
3637
REMOTE_FAILED = "Remote key update from '{}' failed, HTTP status {}"
3738
MALFORMED = "Remote key update from {} failed, malformed JWKS."
38-
DEFAULT_HTTPC_TIMEOUT = 10
3939

4040
LOGGER = logging.getLogger(__name__)
4141

@@ -254,9 +254,7 @@ def __init__(
254254
else:
255255
self.httpc = requests.request
256256

257-
self.httpc_params = httpc_params or {}
258-
if "timeout" not in self.httpc_params:
259-
self.httpc_params["timeout"] = DEFAULT_HTTPC_TIMEOUT
257+
self.httpc_params = httpc_params_loader(httpc_params)
260258

261259
if keys:
262260
self.source = None

src/cryptojwt/key_issuer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
from .jwe.utils import alg2keytype as jwe_alg2keytype
1010
from .jws.utils import alg2keytype as jws_alg2keytype
11-
from .key_bundle import DEFAULT_HTTPC_TIMEOUT
1211
from .key_bundle import KeyBundle
1312
from .key_bundle import build_key_bundle
1413
from .key_bundle import key_diff
1514
from .key_bundle import update_key_bundle
15+
from .utils import httpc_params_loader
1616
from .utils import importer
1717
from .utils import qualified_name
1818

@@ -58,9 +58,7 @@ def __init__(
5858

5959
self.ca_certs = ca_certs
6060
self.httpc = httpc or request
61-
self.httpc_params = httpc_params or {}
62-
if "timeout" not in self.httpc_params:
63-
self.httpc_params["timeout"] = DEFAULT_HTTPC_TIMEOUT
61+
self.httpc_params = httpc_params_loader(httpc_params)
6462
self.keybundle_cls = keybundle_cls
6563
self.name = name
6664
self.remove_after = remove_after

src/cryptojwt/key_jar.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
from .exception import IssuerNotFound
99
from .jwe.jwe import alg2keytype as jwe_alg2keytype
1010
from .jws.utils import alg2keytype as jws_alg2keytype
11-
from .key_bundle import DEFAULT_HTTPC_TIMEOUT
1211
from .key_bundle import KeyBundle
1312
from .key_issuer import KeyIssuer
1413
from .key_issuer import build_keyissuer
1514
from .key_issuer import init_key_issuer
1615
from .utils import deprecated_alias
16+
from .utils import httpc_params_loader
1717
from .utils import importer
1818
from .utils import qualified_name
1919

@@ -51,9 +51,7 @@ def __init__(
5151
self.keybundle_cls = keybundle_cls
5252
self.remove_after = remove_after
5353
self.httpc = httpc or request
54-
self.httpc_params = httpc_params or {}
55-
if "timeout" not in self.httpc_params:
56-
self.httpc_params["timeout"] = DEFAULT_HTTPC_TIMEOUT
54+
self.httpc_params = httpc_params_loader(httpc_params)
5755

5856
# Now part of httpc_params
5957
# self.verify_ssl = verify_ssl

src/cryptojwt/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
from cryptojwt.exception import BadSyntax
1212

13+
DEFAULT_HTTPC_TIMEOUT = 10
14+
1315
# ---------------------------------------------------------------------------
1416
# Helper functions
1517

@@ -255,3 +257,10 @@ def rename_kwargs(func_name, kwargs, aliases):
255257
raise TypeError("{} received both {} and {}".format(func_name, alias, new))
256258
warnings.warn("{} is deprecated; use {}".format(alias, new), DeprecationWarning)
257259
kwargs[new] = kwargs.pop(alias)
260+
261+
262+
def httpc_params_loader(httpc_params):
263+
httpc_params = httpc_params or {}
264+
if "timeout" not in httpc_params:
265+
httpc_params["timeout"] = DEFAULT_HTTPC_TIMEOUT
266+
return httpc_params

0 commit comments

Comments
 (0)