Skip to content

Commit 5a10ae9

Browse files
committed
chore: add support for vcr.py
1 parent fb338d1 commit 5a10ae9

File tree

10 files changed

+767
-8
lines changed

10 files changed

+767
-8
lines changed

.github/workflows/checks.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,4 @@ jobs:
4141
SCW_DEFAULT_PROJECT_ID: ${{ secrets.SCW_DEFAULT_PROJECT_ID }}
4242
SCW_DEFAULT_ORGANIZATION_ID: ${{ secrets.SCW_DEFAULT_ORGANIZATION_ID }}
4343
SCW_DEFAULT_REGION: ${{ secrets.SCW_DEFAULT_REGION }}
44-
run: poetry run python -m unittest discover -s tests -v
45-
44+
run: poetry run pytest --record-mode=none

scaleway-async/poetry.lock

Lines changed: 611 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scaleway-async/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ ruff = ">=0.5.0,<0.12.5"
3232
mypy = "^1.5.1"
3333
ty = "^0.0.1a15"
3434
pyrefly = "^0.24.2"
35+
pytest = "^8.4.1"
36+
pytest-recording = "^0.13.4"
3537

3638
[build-system]
3739
requires = ["poetry-core"]

scaleway-core/poetry.lock

Lines changed: 112 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scaleway-core/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ruff = ">=0.5.0,<0.12.5"
3434
mypy = "^1.5.1"
3535
ty = "^0.0.1a15"
3636
pyrefly = "^0.24.2"
37+
pytest = "^8.4.1"
3738

3839
[build-system]
3940
requires = ["poetry-core"]

scaleway-core/scaleway_core/api.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
Params = Mapping[str, Any]
1616

17+
from requests.adapters import HTTPAdapter
18+
from urllib3.util import Retry
19+
20+
1721

1822
@dataclass
1923
class APILogger:
@@ -154,6 +158,20 @@ def _request(
154158

155159
logger = APILogger(self._log, self.client._increment_request_count())
156160

161+
# define the retry strategy
162+
retry_strategy = Retry(
163+
total=3, # maximum number of retries
164+
status_forcelist=[
165+
500,
166+
], # the HTTP status codes to retry on
167+
)
168+
169+
# create an HTTP adapter with the retry strategy and mount it to the session
170+
adapter = HTTPAdapter(max_retries=retry_strategy)
171+
# create a new session object
172+
session = requests.Session()
173+
session.mount("https://", adapter)
174+
157175
logger.log_request(
158176
method=method,
159177
url=url,
@@ -163,7 +181,7 @@ def _request(
163181
if isinstance(raw_body, bytes)
164182
else raw_body,
165183
)
166-
response = requests.request(
184+
response = session.request(
167185
method=method,
168186
url=url,
169187
params=request_params,

scaleway-core/scaleway_core/utils/resolve_one_of.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from __future__ import annotations
12
from collections.abc import Callable
23
from dataclasses import dataclass
34
from typing import Any, Dict, Generic, List, Optional, TypeVar
4-
from _typeshed import SupportsKeysAndGetItem
5+
from typing import TYPE_CHECKING
6+
if TYPE_CHECKING:
7+
from _typeshed import SupportsKeysAndGetItem
58

69
from scaleway_core.profile import ProfileDefaults
710

scaleway/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ ruff = ">=0.5.0,<0.12.5"
3333
mypy = "^1.5.1"
3434
ty = "^0.0.1a15"
3535
pyrefly = "^0.24.2"
36+
pytest = "^8.4.1"
37+
pytest-recording = "^0.13.4"
3638

3739
[build-system]
3840
requires = ["poetry-core"]

scaleway/tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pytest
2+
3+
4+
@pytest.fixture(scope="session")
5+
def vcr_config():
6+
return {
7+
# Replace the Authorization request header with "DUMMY" in cassettes
8+
"filter_headers": ["x-auth-token"],
9+
}

scaleway/tests/test_vpc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import unittest
2+
3+
import pytest
4+
from vcr.unittest import VCRTestCase
25
from scaleway.vpc.v2 import VpcV2API
36
from scaleway_core.api import ScalewayException
47
from scaleway_core.client import Client
@@ -10,10 +13,11 @@
1013
created_vpc_count = 1
1114

1215

13-
class TestScalewayVPCV2(unittest.TestCase):
16+
@pytest.mark.vcr()
17+
class TestScalewayVPCV2(VCRTestCase):
1418
@classmethod
1519
def setUpClass(self):
16-
self.client = Client.from_env()
20+
self.client = Client.from_config_file_and_env()
1721
self.vpcAPI = VpcV2API(self.client)
1822
self.project_id = self.client.default_project_id
1923
self.region = region

0 commit comments

Comments
 (0)