Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Documentation

on:
push:
branches: [master]
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
with:
persist-credentials: false
- uses: actions/setup-python@v6
with:
python-version: "3.12"
- uses: astral-sh/setup-uv@v8.1.0
with:
version: "0.10.8"
- run: uv sync --group docs --group tests
- run: >-
uv run --group docs --group tests pytest
tests/test_docs_examples.py tests/test_docs_serialization.py tests/test_docs_responses.py tests/test_docs_build.py -q
- run: uv run --group docs python scripts/build_docs.py
env:
DOCS_NOINDEX: ${{ github.event_name == 'pull_request' && '1' || '0' }}
- uses: actions/upload-artifact@v4
with:
name: documentation
path: .build/docs
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ venv.bak/
# Rope project settings
.ropeproject

# mkdocs documentation
/site
# Generated documentation (sources live in /site)
/.build/
/graphify-out/

# mypy
.mypy_cache/
Expand Down
12 changes: 12 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2

build:
os: ubuntu-24.04
tools:
python: "3.12"
commands:
- pip install uv==0.10.8
- uv sync --group docs
- uv run --group docs python scripts/build_docs.py
- mkdir -p "$READTHEDOCS_OUTPUT/html"
- cp -a .build/docs/. "$READTHEDOCS_OUTPUT/html/"
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

**unihttp** is a modern library for creating declarative API clients.

[Documentation](https://unihttp.readthedocs.io/en/latest/)

## Table of Contents

- [Features](#features)
Expand Down Expand Up @@ -556,4 +558,4 @@ cp -r /tmp/unihttp/.agents/skills/unihttp ~/.claude/skills/
cp -r /tmp/unihttp/.agents/skills/unihttp-client ~/.claude/skills/
```

Restart the agent (or reload its skills) and both skills become available.
Restart the agent (or reload its skills) and both skills become available.
1 change: 1 addition & 0 deletions examples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Runnable examples and reusable snippets for the unihttp documentation."""
51 changes: 51 additions & 0 deletions examples/async_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""A standalone async version of the first-client example."""

import asyncio
from dataclasses import dataclass

import httpx
from unihttp.bind_method import bind_method
from unihttp.clients.httpx import HTTPXAsyncClient
from unihttp.markers import Path
from unihttp.method import BaseMethod
from unihttp.serializers.adaptix import DEFAULT_RETORT, AdaptixDumper, AdaptixLoader


@dataclass
class User:
id: int
name: str


@dataclass
class GetUser(BaseMethod[User]):
__url__ = "/users/{user_id}"
__method__ = "GET"

user_id: Path[int]


class UserClient(HTTPXAsyncClient):
get_user = bind_method(GetUser)


def mock_api(request: httpx.Request) -> httpx.Response:
return httpx.Response(
200, json={"id": int(request.url.path.rsplit("/", 1)[1]), "name": "Ada"}
)


async def main() -> User:
async with UserClient(
base_url="https://api.example.com",
request_dumper=AdaptixDumper(DEFAULT_RETORT),
response_loader=AdaptixLoader(DEFAULT_RETORT),
session=httpx.AsyncClient(transport=httpx.MockTransport(mock_api)),
) as client:
user = await client.get_user(user_id=1)
print(user) # User(id=1, name='Ada')
return user


if __name__ == "__main__":
asyncio.run(main())
1 change: 1 addition & 0 deletions examples/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Standalone clients used by the documentation's backend tabs."""
51 changes: 51 additions & 0 deletions examples/backends/aiohttp_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""aiohttp async quickstart; start examples/demo_api.py first."""

import asyncio
from dataclasses import dataclass

from unihttp.bind_method import bind_method

# --8<-- [start:backend-import]
from unihttp.clients.aiohttp import AiohttpAsyncClient

# --8<-- [end:backend-import]
from unihttp.markers import Path
from unihttp.method import BaseMethod
from unihttp.serializers.adaptix import DEFAULT_RETORT, AdaptixDumper, AdaptixLoader


# --8<-- [start:models]
@dataclass
class User:
id: int
name: str


@dataclass
class GetUser(BaseMethod[User]):
__url__ = "/users/{user_id}"
__method__ = "GET"

user_id: Path[int]
# --8<-- [end:models]


# --8<-- [start:binding]
class UserClient(AiohttpAsyncClient):
get_user = bind_method(GetUser)
# --8<-- [end:binding]


async def main(base_url: str = "http://127.0.0.1:8000") -> User:
async with UserClient(
base_url=base_url,
request_dumper=AdaptixDumper(DEFAULT_RETORT),
response_loader=AdaptixLoader(DEFAULT_RETORT),
) as client:
user = await client.get_user(user_id=1)
print(user) # User(id=1, name='Ada')
return user


if __name__ == "__main__":
asyncio.run(main())
51 changes: 51 additions & 0 deletions examples/backends/httpx2_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""HTTPX2 async quickstart; start examples/demo_api.py first."""

import asyncio
from dataclasses import dataclass

from unihttp.bind_method import bind_method

# --8<-- [start:backend-import]
from unihttp.clients.httpx2 import HTTPX2AsyncClient

# --8<-- [end:backend-import]
from unihttp.markers import Path
from unihttp.method import BaseMethod
from unihttp.serializers.adaptix import DEFAULT_RETORT, AdaptixDumper, AdaptixLoader


# --8<-- [start:models]
@dataclass
class User:
id: int
name: str


@dataclass
class GetUser(BaseMethod[User]):
__url__ = "/users/{user_id}"
__method__ = "GET"

user_id: Path[int]
# --8<-- [end:models]


# --8<-- [start:binding]
class UserClient(HTTPX2AsyncClient):
get_user = bind_method(GetUser)
# --8<-- [end:binding]


async def main(base_url: str = "http://127.0.0.1:8000") -> User:
async with UserClient(
base_url=base_url,
request_dumper=AdaptixDumper(DEFAULT_RETORT),
response_loader=AdaptixLoader(DEFAULT_RETORT),
) as client:
user = await client.get_user(user_id=1)
print(user) # User(id=1, name='Ada')
return user


if __name__ == "__main__":
asyncio.run(main())
50 changes: 50 additions & 0 deletions examples/backends/httpx2_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""HTTPX2 sync quickstart; start examples/demo_api.py first."""

from dataclasses import dataclass

from unihttp.bind_method import bind_method

# --8<-- [start:backend-import]
from unihttp.clients.httpx2 import HTTPX2SyncClient

# --8<-- [end:backend-import]
from unihttp.markers import Path
from unihttp.method import BaseMethod
from unihttp.serializers.adaptix import DEFAULT_RETORT, AdaptixDumper, AdaptixLoader


# --8<-- [start:models]
@dataclass
class User:
id: int
name: str


@dataclass
class GetUser(BaseMethod[User]):
__url__ = "/users/{user_id}"
__method__ = "GET"

user_id: Path[int]
# --8<-- [end:models]


# --8<-- [start:binding]
class UserClient(HTTPX2SyncClient):
get_user = bind_method(GetUser)
# --8<-- [end:binding]


def main(base_url: str = "http://127.0.0.1:8000") -> User:
with UserClient(
base_url=base_url,
request_dumper=AdaptixDumper(DEFAULT_RETORT),
response_loader=AdaptixLoader(DEFAULT_RETORT),
) as client:
user = client.get_user(user_id=1)
print(user) # User(id=1, name='Ada')
return user


if __name__ == "__main__":
main()
51 changes: 51 additions & 0 deletions examples/backends/httpx_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""HTTPX async quickstart; start examples/demo_api.py first."""

import asyncio
from dataclasses import dataclass

from unihttp.bind_method import bind_method

# --8<-- [start:backend-import]
from unihttp.clients.httpx import HTTPXAsyncClient

# --8<-- [end:backend-import]
from unihttp.markers import Path
from unihttp.method import BaseMethod
from unihttp.serializers.adaptix import DEFAULT_RETORT, AdaptixDumper, AdaptixLoader


# --8<-- [start:models]
@dataclass
class User:
id: int
name: str


@dataclass
class GetUser(BaseMethod[User]):
__url__ = "/users/{user_id}"
__method__ = "GET"

user_id: Path[int]
# --8<-- [end:models]


# --8<-- [start:binding]
class UserClient(HTTPXAsyncClient):
get_user = bind_method(GetUser)
# --8<-- [end:binding]


async def main(base_url: str = "http://127.0.0.1:8000") -> User:
async with UserClient(
base_url=base_url,
request_dumper=AdaptixDumper(DEFAULT_RETORT),
response_loader=AdaptixLoader(DEFAULT_RETORT),
) as client:
user = await client.get_user(user_id=1)
print(user) # User(id=1, name='Ada')
return user


if __name__ == "__main__":
asyncio.run(main())
50 changes: 50 additions & 0 deletions examples/backends/httpx_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""HTTPX sync quickstart; start examples/demo_api.py first."""

from dataclasses import dataclass

from unihttp.bind_method import bind_method

# --8<-- [start:backend-import]
from unihttp.clients.httpx import HTTPXSyncClient

# --8<-- [end:backend-import]
from unihttp.markers import Path
from unihttp.method import BaseMethod
from unihttp.serializers.adaptix import DEFAULT_RETORT, AdaptixDumper, AdaptixLoader


# --8<-- [start:models]
@dataclass
class User:
id: int
name: str


@dataclass
class GetUser(BaseMethod[User]):
__url__ = "/users/{user_id}"
__method__ = "GET"

user_id: Path[int]
# --8<-- [end:models]


# --8<-- [start:binding]
class UserClient(HTTPXSyncClient):
get_user = bind_method(GetUser)
# --8<-- [end:binding]


def main(base_url: str = "http://127.0.0.1:8000") -> User:
with UserClient(
base_url=base_url,
request_dumper=AdaptixDumper(DEFAULT_RETORT),
response_loader=AdaptixLoader(DEFAULT_RETORT),
) as client:
user = client.get_user(user_id=1)
print(user) # User(id=1, name='Ada')
return user


if __name__ == "__main__":
main()
Loading
Loading