Skip to content

Add Retry/Back-off and Environment Profiles Enhancements#2

Merged
sethorpe merged 1 commit intomainfrom
feature/retries-back-off
Jun 2, 2025
Merged

Add Retry/Back-off and Environment Profiles Enhancements#2
sethorpe merged 1 commit intomainfrom
feature/retries-back-off

Conversation

@sethorpe
Copy link
Owner

Overview

This PR builds on our existing test-reporting, logging, Makefile, and CI setup by adding two core framework features:

  1. Automatic Retries & Exponential Back-off
  2. Environment "Profiles" for Configuration

What's Changed

  1. Retry & Back-off (via Tenacity)
  • Added Dependency

    # pyroject.toml
    tenacity = ">=8.2.2,<9.0.0"
  • APIClient Enhancements

    In src/api_testing_framework/client.py, the get and post methods are now decorated with Tenacity's @retry:

    @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...): ...
    • Retries up to 3 attempts
    • Back-off doubles wait time between failures (max 10s)
    • Only on APIError (non-2xx or custom exceptions)

    Benefit: Makes test resilient to transient HTTP errors (5xx, rate-limits, network blips).

2. Environment Profiles

  • Config Loader Update

    Replaced top-level .env-only logic in src/api_testing_framework/config.py with:

    def get_settings(env_profile: Optional[str] = None) -> Settings:
        # Picks up ENV_PROFILE (default "dev"), then loads
        #   .env.{profile} -> .env -> os.environ
        return Settings(_env_file=files_list or None)
  • Multiple Files Supported

    • .env.dev, .env.staging, .env.prod, etc.
    • Falls back to .env if profile-specific file is missing
    • If no files exist, reads actual environment variables

Benefit: Easily switch between configurations (e.g. dev vs prod) without code changes.

How to Verify Locally

  1. Install dependencies

    make install
  2. Run unit tests (with retry decorators in place)

    make test
  3. Smoke test environment profiles

    ENV_PROFILE=staging make test
  4. Generate and view report

    make report
    make serve-report

CI Integration

The existing .github/workflows/ci.yml now also benefits:

  • Retries occur automatically in CI runs.
  • ENV_PROFILE can be set via env: in the workflow to target different test environments.

Example snippet:

env:
  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 }}

@sethorpe sethorpe merged commit 9993994 into main Jun 2, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant