Skip to content
Merged
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
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ jobs:
runs-on: ubuntu-latest

env:
SPOTIFY_API_BASE_URL: ${{ vars.SPOTIFY_API_BASE_URL }}
SPOTIFY_CLIENT_ID: ${{ vars.SPOTIFY_CLIENT_ID }}
SPOTIFY_CLIENT_SECRET: ${{ secrets.SPOTIFY_CLIENT_SECRET }}
ENV_PROFILE: production
SPOTIFY_API_BASE_URL: ${{ vars.SPOTIFY_API_BASE_URL }}
SPOTIFY_CLIENT_ID: ${{ vars.SPOTIFY_CLIENT_ID }}
SPOTIFY_CLIENT_SECRET: ${{ secrets.SPOTIFY_CLIENT_SECRET }}
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down
18 changes: 17 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ dependencies = [
"httpx (>=0.28.1,<0.29.0)",
"python-dotenv (>=1.1.0,<2.0.0)",
"pydantic (>=2.11.5,<3.0.0)",
"pydantic-settings (>=2.9.1,<3.0.0)"
"pydantic-settings (>=2.9.1,<3.0.0)",
"tenacity (>=8.2.2,<9.0.0)",
]

[tool.poetry]
Expand Down
20 changes: 20 additions & 0 deletions src/api_testing_framework/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from typing import Any, Dict, Optional

import httpx
from tenacity import (
retry,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)

from api_testing_framework.exceptions import APIError

Expand Down Expand Up @@ -52,12 +58,26 @@ def _handle_response(self, response: httpx.Response) -> dict:
raise APIError(response.status_code, data.get("error", response.text), data)
return data

@retry(
reraise=True,
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=1, max=10),
retry=retry_if_exception_type(APIError),
)
def get(self, path: str, params: Dict[str, Any] = None) -> dict:
"""GET with retries on APIError"""
self._refresh_token_if_needed()
resp = self._client.get(path, params=params)
return self._handle_response(resp)

@retry(
reraise=True,
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=1, max=10),
retry=retry_if_exception_type(APIError),
)
def post(self, path: str, json: Dict[str, Any] = None) -> dict:
"""POST with retries on APIError"""
self._refresh_token_if_needed()
resp = self._client.post(path, json=json)
return self._handle_response(resp)
Expand Down
26 changes: 22 additions & 4 deletions src/api_testing_framework/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import os
from typing import List, Optional

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
spotify_client_id: str
spotify_client_secret: str
spotify_api_base_url: str

model_config = SettingsConfigDict(env_file=".env")
# Pass env_file at instantiation so the class-level default is disabled
model_config = SettingsConfigDict(env_file=None)


def get_settings() -> Settings:
def get_settings(env_profile: Optional[str] = None) -> Settings:
"""
Read and return a fresh Settings object.
Load settings from
1) .env.{env_profile} if it exists
2) .env if it exists
The profile defaults to the ENV_PROFILE environment variable (or 'dev').
"""
return Settings()
if env_profile is None:
env_profile = os.getenv("ENV_PROFILE", "dev")

cwd = os.getcwd()
candidate_files: List[str] = [
os.path.join(cwd, f".env.{env_profile}"),
os.path.join(cwd, ".env"),
]
env_files = [f for f in candidate_files if os.path.isfile(f)]

return Settings(_env_file=env_files or None)