feat: add `headers` configuration property by evansims · Pull Request #232 · openfga/python-sdk

This pull request adds a headers property to the SDK's client configuration class, allowing developers to pass headers that are automatically included in all API requests by default.

This pattern aligns the Python SDK with behavior already present in the other SDKs.

from openfga_sdk.client import ClientConfiguration, OpenFgaClient

# Configure default headers that apply to ALL requests
config = ClientConfiguration(
    api_url="https://api.fga.example",
    store_id="01ARZ3NDEKTSV4RRFFQ69G5FAV",
    headers={
        "X-Tenant-ID": "tenant-123",
        "X-App-Version": "1.0.0",
        "X-Request-Priority": "normal"
    }
)

async with OpenFgaClient(config) as client:
    # Default headers are automatically included in all requests
    await client.read_authorization_models()
    # Includes: X-Tenant-ID: tenant-123, X-App-Version: 1.0.0, X-Request-Priority: normal

    # Add per-request headers (merged with defaults)
    await client.check(
        user="user:alice",
        relation="viewer",
        object="document:roadmap",
        options={
            "headers": {
                "X-Request-ID": "req-456",
                "X-Correlation-ID": "corr-789"
            }
        }
    )
    # Includes: X-Tenant-ID: tenant-123, X-App-Version: 1.0.0, X-Request-Priority: normal,
    #           X-Request-ID: req-456, X-Correlation-ID: corr-789

    # Override default headers with per-request values
    await client.check(
        user="user:bob",
        relation="admin",
        object="document:secrets",
        options={
            "headers": {
                "X-Request-Priority": "high", # Overrides default "normal" priority
                "X-Request-ID": "req-789"
            }
        }
    )
    # Includes: X-Tenant-ID: tenant-123, X-App-Version: 1.0.0,
    #           X-Request-Priority: high (OVERRIDDEN), X-Request-ID: req-789