Skip to main content

REST client

ApiClient is a small httpx client for one REST API behind Entra ID. You subclass it once per API: the class says where the API is and where its credentials come from, and its methods call fetch().

A client for one API

from dazapiz import ApiClient


class AppApiClient(ApiClient):
    host_url = "https://app-api.example.com"
    scope = "api://00000000-0000-0000-0000-000000000000/.default"
    client_id_env = "APP_API_CLIENT_ID"     # this API's service principal
    client_secret_env = "APP_API_SECRET"
    # tenant_id_env = "APP_API_TENANT"      # only if not AZURE_TENANT_ID

    def get_item(self, item_id: str) -> dict:
        return self.fetch("/items/{item_id}", item_id=item_id)


with AppApiClient() as client:
    item = client.get_item("42")

fetch(path, method="GET", *, query, json, headers, content_type, parse, **path_params) fills the {name} placeholders from keyword arguments, URL-encoded, sends the request and returns the parsed body: JSON as Python objects, XML as an ElementTree.Element, anything else as text, an empty body as None. A non-2xx response raises ApiError, which is also an httpx.HTTPStatusError. scope defaults to f"{host_url}/.default", and __init__ arguments (host_url, credential, scope, timeouts) override the class attributes per instance.

Where the credential comes from

credential= wins: any TokenCredential, such as DefaultAzureCredential() or RefreshTokenCredential(refresh_token_from_az_cli()). Otherwise, when the class names client_id_env and client_secret_env, app_credential() chooses one when the client is created, by mode (the class attribute), else $AZURE_TOKEN_CREDENTIALS, else automatically:

Mode Credential
automatic the API’s service principal when both of its variables are set, else "dev"
"dev" AzureCliCredential, then dazapiz.DeviceCodeCredential in an interactive session
"prod" the API’s service principal if set, else WorkloadIdentityCredential when $AZURE_FEDERATED_TOKEN_FILE is set, else ManagedIdentityCredential
"client_secret", "workload_identity", "managed_identity", "azure_cli", "device_code" that credential alone

So a developer with neither variable set gets their own az login, and a deployment that sets them gets the service principal. A client ID without its secret, or the reverse, is a ValueError naming the missing variable, never a silent fall-back. A class with no credential variables and no credential= sends requests unauthenticated.

Tokens are kept until five minutes before they expire. A 401 is not retried, since the credential would hand back the same token; the token is dropped so the next request asks for a new one.

Typed results and model bodies

parse= validates the body and types the result:

from pydantic import TypeAdapter

pets = client.fetch("/pets", parse=TypeAdapter(list[Pet]).validate_python)  # list[Pet]
pet = client.fetch("/pets/{pet_id}", parse=Pet.model_validate, pet_id="7")  # Pet

parse is called with the parsed body of every successful response (None for an empty one). If it raises, fetch() raises ResponseParseError, with the method and URL but not the body (a validation error can quote it), and the original error as __cause__. The same error is raised when a body labelled JSON or XML does not parse.

json= takes a pydantic model (sent as model_dump(mode="json", by_alias=True, exclude_unset=True)) or a dataclass, also nested in lists and dicts. dazapiz does not depend on pydantic: any object with a model_dump() method is treated as a model.

Logging and retries

verbose=True logs each call on the dazapiz.http logger, in azr’s format:

ℹ >>> POST https://petstore3.swagger.io/api/v3/pet
ℹ >>> body:
{
  "id": 4711,
  "name": "dazapiz-example",
  "photoUrls": ["https://example.com/dazapiz.png"],
  "tags": [
    {
      "id": 1,
      "name": "dazapiz"
    }
  ],
  "status": "available"
}
✔ <<< status = 200 | time = 0.412 secs. | size = 0.13 Kb

JSON bodies (the request’s, and the response’s on failure) are abbreviated: an object or array of more than 10 items keeps the first and last 5, an array of more than 12 numbers or strings the first and last 6, with ... between. Headers are never logged, so neither is the bearer token; a secret you put in a body is logged as sent.

429, 503 and connection failures are retried, up to max_tries=5 attempts, waiting for Retry-After when the server sends it, else a random 1 to 2.5^attempt seconds (at most 60). A read timeout is not retried: the server may already have acted.

⚠ Request failed. Retrying in 2.1 secs. (attempt 1 of 5).

Example: the Swagger Petstore

examples/petstore.py in the repository is a complete, typed client for the public Swagger Petstore v3, with pydantic models for every body. It needs no Azure, so you can run it:

uv run python examples/petstore.py

The code below is included from that file when the site is built, so it is the code that runs. These are the choices it makes, and why.

One base, two kinds of model

class _Model(BaseModel):
    """camelCase aliases, validated by alias or by name, immutable."""

    model_config = ConfigDict(
        alias_generator=to_camel,
        validate_by_name=True,
        validate_by_alias=True,
        frozen=True,
    )


class _Request(_Model):
    """A request body: an unknown field is an error, before anything is sent."""

    model_config = ConfigDict(extra="forbid")


class _Response(_Model):
    """A response body: unknown fields are ignored, the rest is validated."""

    model_config = ConfigDict(extra="ignore")
  • Aliases. The API is camelCase (photoUrls), Python is snake_case (photo_urls). alias_generator=to_camel maps one to the other, and validate_by_name with validate_by_alias accept either when validating. fetch() sends models by_alias=True, so the wire format is the API’s.
  • Frozen. A validated object cannot be changed behind the validator’s back; pet.model_copy(update={"status": "sold"}) makes a changed copy (pydantic does not validate update, so give it values of the right type).
  • Strict requests, tolerant responses. A request model forbids unknown fields, so a typo (photoUrl=) fails in Python, before anything is sent. A response model ignores fields it does not know, so the server adding one does not break the client; a missing required field or a wrong type still fails, as ResponseParseError.

Separate input and output models

class PetCreate(_Request):
    """A pet to add. The store requires `name` and `photo_urls`, which must be URLs.

    The schema leaves `id` optional, but the public store answers 500 to a
    pet without one (2026-11-21), so pass your own.
    """

    id: Id | None = None
    name: Annotated[str, Field(min_length=1)]
    photo_urls: list[HttpUrl]
    category: Category | None = None
    tags: list[Tag] | None = None
    status: PetStatus | None = None


class Pet(_Response):
    """A pet as the store returns it: always with its `id`.

    `photo_urls` are strings, as the schema types them (no `format: uri`):
    the public store holds pets whose photo URL is `"string"`, and a
    response is checked against the API's contract, not a stricter one.
    `PetCreate` checks the URLs it sends.
    """

    id: Id
    name: str
    photo_urls: list[str]
    category: Category | None = None
    tags: list[Tag] | None = None
    status: PetStatus | None = None

What you send and what you get back are different contracts. PetCreate checks what the store needs on the way in: a non-empty name and real URLs. Pet always has an id, since the store assigned one, and types the photo URLs as the schema does, as plain strings. Request models are sent with only the fields you set (exclude_unset=True), so an optional field you left out is not sent as null.

Where the live store differs from its schema

An example client meets the real server, not only its OpenAPI document. The public store differs in ways the models and methods account for, each noted in the code:

  • The schema leaves a new pet’s id optional, but the store answers 500 to a pet without one: pass your own (as PetCreate’s docstring says).
  • The store holds pets whose photo URL is the string "string", so Pet checks the schema’s contract (list[str]), not a stricter one; only PetCreate checks for URLs.
  • It answers XML unless asked for JSON, so every call sends Accept: application/json.
  • Its status query takes one value, so find_pets_by_status() makes one request per status.
  • Its delete reply is plain text labelled as JSON, which would not parse, so the delete calls read it as text:
        # The store labels its plain-text reply ("Pet deleted") as JSON, which would
        # not parse (ResponseParseError): read it as the text it is.
        self.fetch(
            "/pet/{pet_id}", "DELETE", headers=_JSON, content_type="text/plain", pet_id=str(pet_id)
        )

Build validators once

# The store answers XML unless asked for JSON.
_JSON = {"Accept": "application/json"}

# Built once: a TypeAdapter compiles its validator when it is created.
_PETS = TypeAdapter(list[Pet])
_STATUSES = TypeAdapter(tuple[PetStatus, ...])

A TypeAdapter compiles its validator when it is created, so the module builds each one once and the methods reuse it:

        if isinstance(status, str):
            raise TypeError('status is a sequence of statuses: pass ["sold"], not "sold".')
        statuses = _STATUSES.validate_python(tuple(status))
        if not statuses:
            raise ValueError("Pass at least one status.")
        pets: list[Pet] = []
        for one in dict.fromkeys(statuses):
            pets += self.fetch(
                "/pet/findByStatus",
                query={"status": one},
                headers=_JSON,
                parse=_PETS.validate_python,
            )
        return pets

Domain errors

A 404 from get_pet() means “no such pet”, which callers want to catch by name, not by status code. The client turns that one case into its own error, chained to the ApiError, and lets every other failure through:

class PetNotFoundError(LookupError):
    """No pet with that id; `__cause__` is the store's 404 `dazapiz.ApiError`."""
    def get_pet(self, pet_id: int) -> Pet:
        """Find a pet by id.

        Parameters
        ----------
        pet_id : int
            The pet's id.

        Returns
        -------
        Pet

        Raises
        ------
        PetNotFoundError
            The store has no such pet (HTTP 404), chained to the `ApiError`.
        """
        try:
            return self.fetch(
                "/pet/{pet_id}", headers=_JSON, parse=Pet.model_validate, pet_id=str(pet_id)
            )
        except ApiError as err:
            if err.response.status_code == 404:
                raise PetNotFoundError(f"No pet with id {pet_id}.") from err
            raise

Putting it together

def main() -> None:
    """Add a pet, find it, sell it, order it and delete it, on the public store."""
    # The public store is shared: an id of our own, high enough to avoid others'.
    pet_id = random.randrange(10**12, 10**13)
    with PetStoreClient(verbose=True) as store:
        pet = store.add_pet(
            PetCreate(
                id=pet_id,
                name="dazapiz-example",
                photo_urls=[HttpUrl("https://example.com/dazapiz.png")],
                tags=[Tag(id=1, name="dazapiz")],
                status="available",
            )
        )
        print(f"Added {pet!r}")
        try:
            print(f"Found {store.get_pet(pet.id)!r}")
            sold = store.update_pet(pet.model_copy(update={"status": "sold"}))
            print(f"Updated to {sold.status}")
            order = store.place_order(OrderCreate(id=pet_id, pet_id=pet.id, quantity=1))
            print(f"Placed {order!r}")
            store.delete_order(order.id)
        finally:
            store.delete_pet(pet.id)
        try:
            store.get_pet(pet.id)
        except PetNotFoundError as err:
            print(f"Deleted: {err}")

tests/test_petstore_example.py checks the models against the store’s OpenAPI document and the requests the client sends; a live test against the public store runs by hand.

To test a client of your own in CI, without Azure or the network, see Testing your API client.