Skip to main content

API Client for Python ^3.10 and Pytest projects. It includes the Allure request/response hooks and the status code checking inside the main method. Request hook generates cURL, Response hook represents the useful information.

Project description

allure-api-client

A lightweight API client built on top of httpx that adds convenient defaults for testing, plus optional Allure-friendly request/response logging. It ships with:

  • Synchronous and asynchronous clients
  • Optional Allure hooks to log cURL, request, and response details
  • Simple Bearer token authentication helper
  • Built-in status code verification in a single send_request call

Requirements

  • Python 3.11+

Installation

pip install allure-api-client

If you want Allure reporting, also install Allure and its pytest plugin and generate a report when you run tests:

Example test run with Allure report generation:

pytest --alluredir=./allure-results
allure serve ./allure-results

Quick start (synchronous)

from api_client import APIClient, BearerToken

client = APIClient(
    base_url="https://api.example.com",
    auth=BearerToken("YOUR_ACCESS_TOKEN"),  # optional
    verify=False,                            # optional, default False
    with_allure=True                         # optional, default True: enable Allure hooks
)

response = client.send_request(
    method="GET",
    path="/users",
    params={"page": 1},
    # By default, the client expects HTTP 200 (HTTPStatus.OK)
    # You can override the expectation per request:
    # status_code=201,
)
print(response.status_code)
print(response.json())

Quick start (asynchronous)

import asyncio
from api_client import AsyncAPIClient, BearerToken

async def main() -> None:
    async with AsyncAPIClient(
        base_url="https://api.example.com",
        auth=BearerToken("YOUR_ACCESS_TOKEN"),  # optional
        verify=False,                            # optional
        with_allure=True                         # optional
    ) as client:
        response = await client.send_request(
            method="GET",
            path="/users",
        )
        print(response.status_code)
        print(response.json())

asyncio.run(main())

Authentication

Use a Bearer token if your API requires it:

from api_client import BearerToken

auth = BearerToken("YOUR_ACCESS_TOKEN")
# pass it to APIClient/AsyncAPIClient via the auth parameter

Allure integration

  • By default, the clients are created with with_allure=True. The library will attach helpful request/response data to Allure, including a cURL snippet for easy reproduction.
  • If you do not use Allure, set with_allure=False to use minimal internal hooks instead.

Status code handling

  • send_request verifies the response status code for you using the status_code parameter (default: 200 OK).
  • If the actual status code does not match the expected value, an assertion-like error is raised coming from the underlying check.

Example expecting 201 Created:

from api_client import APIClient

client = APIClient(base_url="https://api.example.com")
response = client.send_request(
    method="POST",
    path="/users",
    json={"name": "Alice"},
    status_code=201,
)

Configuration reference

Common parameters on client initialization:

  • base_url: Base URL string for your API (e.g., https://api.example.com)
  • auth: Any httpx-compatible auth object; BearerToken helper is provided
  • cookies: Optional httpx.Cookies to send on each request
  • verify: Whether to verify TLS certificates (default False)
  • with_allure: Enable/disable Allure hooks (default True)

Common parameters on send_request:

  • method: HTTP method (e.g., "GET", "POST", ...)
  • path: Path appended to base_url
  • headers, params, data, json, files
  • follow_redirects (default True)
  • timeout (seconds, default 300)
  • status_code: expected response status (default 200)

Contributing

Contributions are welcome! Please open an issue or a pull request.

License

Released under the MIT License. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

allure_api_client-25.298.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

allure_api_client-25.298-py2.py3-none-any.whl (9.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file allure_api_client-25.298.tar.gz.

File metadata

  • Download URL: allure_api_client-25.298.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Linux/6.11.0-1018-azure

File hashes

Hashes for allure_api_client-25.298.tar.gz
Algorithm Hash digest
SHA256 1d835dadbf74ca49bf241104a5b3571df97fa03e61898aba1705501957ad99fc
MD5 83b953a091075db6d4bf82042338b030
BLAKE2b-256 bcc4fd50f8d018ad1d862a9d187a377ed956a9bed4b58a9a80d4f6a6e9603969

See more details on using hashes here.

File details

Details for the file allure_api_client-25.298-py2.py3-none-any.whl.

File metadata

  • Download URL: allure_api_client-25.298-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Linux/6.11.0-1018-azure

File hashes

Hashes for allure_api_client-25.298-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a4581ece31dccebb108c8f893bd1883b5cad8e2a38ee0eb987cde5bdcc59fb07
MD5 f6190e711eb33e83aadd0da9c907740a
BLAKE2b-256 b3c70a5787bf6224ffbb277b04b4244f79b18469a0d018ac4091b871997b15b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page