BodyCountAdapter

Count extraction from the API response body using a configurable JSON path.

  1. Overview
  2. count_path
  3. pages_path

Overview

Some REST APIs return total count information in the response body rather than headers. This adapter navigates to a value in the response body using a dot-notation path and returns it as the total count.

Simple top-level field

If your API returns the count at the top level of the response body:

{
    "data": [...],
    "total": 150
}
import clearskies

backend = clearskies.backends.ApiBackend(
    base_url="https://api.example.com",
    count_adapter=clearskies.backends.adapters.BodyCountAdapter(
        count_path="total",
    ),
)

Nested path

If your API nests the count inside a metadata envelope:

{
    "data": [...],
    "meta": {
        "pagination": {
            "total": 150,
            "pages": 10
        }
    }
}
import clearskies

backend = clearskies.backends.ApiBackend(
    base_url="https://api.example.com",
    count_adapter=clearskies.backends.adapters.BodyCountAdapter(
        count_path="meta.pagination.total",
        pages_path="meta.pagination.pages",
    ),
)

count_path

Optional

Dot-notation path to the total count value in the response body (e.g. "total", "meta.total", "meta.pagination.total"). Leave empty to skip count extraction.

pages_path

Optional

Dot-notation path to the total pages value in the response body (e.g. "pages", "meta.pages"). Leave empty to skip pages extraction.