Skip to content

Commit 139bd10

Browse files
authored
allow passing custom HTTP adapter to BaseClient (#115)
1 parent 1f81f27 commit 139bd10

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

.github/workflows/python-test.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
name: Python Tests
22

3-
on: [push]
3+
on:
4+
# This should run the workflow for all non-forked pushes
5+
push:
6+
pull_request:
7+
branches:
8+
# This should run the workflow for forked PRs
9+
- '**:**'
10+
411

512
jobs:
613

src/commercetools/base_client.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from oauthlib.oauth2 import BackendApplicationClient
99
from requests.adapters import HTTPAdapter
1010
from requests_oauthlib import OAuth2Session
11-
from urllib3.util.retry import Retry
11+
from urllib3 import Retry
1212

1313
from commercetools.constants import HEADER_CORRELATION_ID
1414
from commercetools.exceptions import CommercetoolsError
@@ -37,6 +37,8 @@ class BaseClient:
3737
path to the token url.
3838
:param token_saver: optional custom token saver to store and retrieve the
3939
oauth2 tokens.
40+
:param http_adapter: optional custom http adapter, useful for settings
41+
custom timeouts, custom retries, or for testing.
4042
4143
"""
4244

@@ -49,6 +51,7 @@ def __init__(
4951
url: str = None,
5052
token_url: str = None,
5153
token_saver: BaseTokenSaver = None,
54+
http_adapter: HTTPAdapter = None,
5255
) -> None:
5356

5457
# Use environment variables as fallback
@@ -88,11 +91,12 @@ def __init__(
8891
token_updater=self._save_token,
8992
)
9093

91-
# Register retry handling for Connection errors and 502, 503, 504.
92-
retry = Retry(status=3, connect=3, status_forcelist=[502, 503, 504])
93-
adapter = HTTPAdapter(max_retries=retry)
94-
self._http_client.mount("http://", adapter)
95-
self._http_client.mount("https://", adapter)
94+
if not http_adapter:
95+
# Register retry handling for Connection errors and 502, 503, 504.
96+
retry = Retry(status=3, connect=3, status_forcelist=[502, 503, 504])
97+
http_adapter = HTTPAdapter(max_retries=retry)
98+
self._http_client.mount("http://", http_adapter)
99+
self._http_client.mount("https://", http_adapter)
96100

97101
if token:
98102
self._http_client.token = token

tests/platform/test_client.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import uuid
33

44
import pytest
5+
from requests_mock.adapter import Adapter
56

67
from commercetools import CommercetoolsError
78
from commercetools.client import Client
@@ -69,6 +70,24 @@ def test_cache_token(commercetools_api):
6970
assert len(commercetools_api.requests_mock.request_history) == 1
7071

7172

73+
def test_allows_passing_custom_http_adapter():
74+
my_adapter = Adapter()
75+
my_adapter.register_uri(
76+
"POST",
77+
"https://auth.sphere.io/oauth/token",
78+
json=dict(access_token="my_mock_access_token"),
79+
)
80+
Client(
81+
client_id="unittest",
82+
client_secret="none",
83+
project_key="test",
84+
url="https://api.sphere.io",
85+
token_url="https://auth.sphere.io/oauth/token",
86+
http_adapter=my_adapter,
87+
)
88+
assert len(my_adapter.request_history) == 1
89+
90+
7291
def test_resource_update_conflict(old_client):
7392
"""Test the return value of the update methods.
7493
@@ -124,9 +143,9 @@ def test_resource_update_conflict(old_client):
124143

125144

126145
def test_resource_delete_conflict(old_client):
127-
"""Test the return value of the update methods.
146+
"""Test the return value of the delete methods.
128147
129-
It doesn't test the actual update itself.
148+
It doesn't test the actual delete itself.
130149
TODO: See if this is worth testing since we're using a mocking backend
131150
"""
132151
product = old_client.products.create(

0 commit comments

Comments
 (0)