/unihttp:unihttpfrom unihttp
Writes idiomatic unihttp client code: typed BaseMethod endpoints, correct submodule imports, marker usage, serializer/backend selection, error handling, and middleware.
Ships in unihttp. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
unihttp is a library for building declarative, type-safe API clients. You
define API endpoints as typed BaseMethod subclasses and bind them to a client
backed by httpx, httpx2, aiohttp, requests, niquests, zapros, or
stdlib urllib. This guidance targets unihttp 0.4.0 and later; check the
installed version before applying it to an older project.
Here is a list of best practices for different parts of a unihttp client. For deeper reference material, see:
unihttp packageThe top-level unihttp/__init__.py is intentionally empty — it re-exports
nothing. Importing names from unihttp directly raises ImportError at
runtime.
Wrong:
# Raises: ImportError: cannot import name 'BaseMethod' from 'unihttp'
from unihttp import BaseMethod, Path, Query, Body, bind_method
Correct:
from unihttp.method import BaseMethod
from unihttp.markers import Body, File, Form, Header, Path, Query
from unihttp.bind_method import bind_method
from unihttp.http import UploadFile # for multipart uploads
The only packages that DO re-export a public API are the serializer subpackages
and unihttp.http / unihttp.middlewares / unihttp.exceptions:
from unihttp.serializers.adaptix import DEFAULT_RETORT, AdaptixDumper, AdaptixLoader
from unihttp.clients.aiohttp import AiohttpAsyncClient
from unihttp.exceptions import ClientError, ServerError
@dataclass subclasses of BaseMethod[ReturnType]The return type goes in the generic parameter — that is what the client returns
and what the response is deserialized into. Declare __url__ and __method__
as class attributes, and annotate fields with markers.
Wrong:
from unihttp.method import BaseMethod
from unihttp.markers import Path
class GetUser(BaseMethod): # no return type — client returns `Any`
url = "/users/{id}" # wrong attribute name
method = "GET" # wrong attribute name
id: int # no marker — adaptix cannot place it in the request
Correct:
from dataclasses import dataclass
from unihttp.method import BaseMethod
from unihttp.markers import Path
@dataclass
class GetUser(BaseMethod[User]):
__url__ = "/users/{id}"
__method__ = "GET"
id: Path[int]
__returning__ is filled automatically from the generic argument, so
client.get_user(id=1) is statically typed as User.
{placeholder} in __url__ must have a matching Path field__url__.format(**path_data) fails with KeyError at request time if a
placeholder has no corresponding Path field.
Wrong:
@dataclass
class GetUserPost(BaseMethod[Post]):
__url__ = "/users/{user_id}/posts/{post_id}"
__method__ = "GET"
user_id: Path[int]
# post_id is missing -> KeyError: 'post_id' when the request is built
Correct:
@dataclass
class GetUserPost(BaseMethod[Post]):
__url__ = "/users/{user_id}/posts/{post_id}"
__method__ = "GET"
user_id: Path[int]
post_id: Path[int]
bind_method for the common casebind_method is a descriptor that exposes the method on the client with the
exact typed signature of its constructor. For BaseMethod[T], sync clients
return T and async clients return an awaitable of T. For StreamMethod, the
result contains an open byte stream; follow the
streaming reference for consumption and cleanup.
Reach for binding whenever an endpoint needs no extra per-call logic.
Wrong (hand-written passthrough that duplicates the signature and loses typing):
class UserClient(AiohttpAsyncClient):
async def get_user(self, id: int) -> User:
return await self.call_method(GetUser(id=id))
Correct:
from unihttp.bind_method import bind_method
class UserClient(AiohttpAsyncClient):
get_user = bind_method(GetUser)
call_method only when an endpoint needs real custom logicWhen you must preprocess arguments, combine calls, or adapt names, write an
explicit method and call call_method. Do not use it just to rename parameters
that bind_method already handles.
Correct:
class UserClient(AiohttpAsyncClient):
create_user = bind_method(CreateUser) # plain endpoint -> bind_method
async def register(self, email: str) -> User:
# genuine extra logic -> call_method
normalized = email.strip().lower()
return await self.call_method(CreateUser(email=normalized))
See references/markers.md for the full table. The common mistakes:
Body, not Query.application/x-www-form-urlencoded fields -> Form.File (wrap content in UploadFile to set filename
and content type).Body cannot be combined with Form/File on the same method — the clients
receive requests only after build_http_request checks for conflicting
payloads and raises ValueError. Use Form for multipart scalar fields.Raw[bytes] or Raw[str] for a prebuilt request body, with Adaptix or a
custom dumper. Pydantic and msgspec dumpers do not currently handle Raw.
Do not combine it with Body, Form, or File.Wrong:
@dataclass
class CreateUser(BaseMethod[User]):
__url__ = "/users"
__method__ = "POST"
name: Query[str] # this belongs in the JSON body, not the query string
email: Query[str]
Correct:
from unihttp.markers import Body
@dataclass
class CreateUser(BaseMethod[User]):
__url__ = "/users"
__method__ = "POST"
name: Body[str]
email: Body[str]
For plain dataclasses / TypedDicts, adaptix is the recommended default: fast, no
runtime dependency inside your model classes, and field-level customization.
Choose msgspec when raw speed matters and pydantic when you need its rich
validation. See references/serializers.md.
Pass the serializer explicitly with AdaptixDumper / AdaptixLoader:
from unihttp.serializers.adaptix import DEFAULT_RETORT, AdaptixDumper, AdaptixLoader
client = UserClient(
base_url="https://api.example.com",
request_dumper=AdaptixDumper(DEFAULT_RETORT),
response_loader=AdaptixLoader(DEFAULT_RETORT),
)
Retort instead of renaming your Python fieldsWhen an API uses camelCase (or any non-Pythonic naming), keep idiomatic
snake_case fields and let adaptix translate them. Do not pollute your dataclasses
with userName-style attributes.
Wrong:
@dataclass
class User:
userName: str # noqa: N815 — leaks the wire format into Python field names
emailAddress: str # noqa: N815
Correct:
from dataclasses import dataclass
from adaptix import name_mapping
from unihttp.serializers.adaptix import DEFAULT_RETORT, AdaptixDumper, AdaptixLoader
@dataclass
class User:
user_name: str
email_address: str
# Define the model first, then extend DEFAULT_RETORT (keyword-only `recipe`) so
# its built-in providers are kept and `name_mapping` can reference the class.
retort = DEFAULT_RETORT.extend(
recipe=[name_mapping(User, map={"user_name": "userName", "email_address": "emailAddress"})],
)
client = UserClient(
base_url="https://api.example.com",
request_dumper=AdaptixDumper(retort),
response_loader=AdaptixLoader(retort),
)
name_mapping also applies to BaseMethod subclasses — a method's Body and
Query fields are renamed the same way, so map the method classes too, not only
the response models. When many classes share the same snake↔camel fields, use one
global name_mapping(map={...}) with no class predicate; it renames those
fields everywhere (models, request bodies, query filters) in a single recipe
entry.
Async backends need an async client class and await; sync backends a sync one.
Install the matching extra (unihttp[aiohttp], unihttp[requests], …). See
references/backends.md for the full matrix.
Correct (async):
from unihttp.clients.aiohttp import AiohttpAsyncClient
class UserClient(AiohttpAsyncClient):
get_user = bind_method(GetUser)
# bind_method returns an awaitable on async clients
user = await client.get_user(id=1)
Correct (sync):
from unihttp.clients.requests import RequestsSyncClient
class UserClient(RequestsSyncClient):
get_user = bind_method(GetUser)
user = client.get_user(id=1)
async withAsync clients own an HTTP session that must be closed, or you leak connections.
The base async client implements __aenter__/__aexit__, so use a context
manager. Construct the client inside a running event loop.
Wrong:
client = UserClient(base_url=..., request_dumper=..., response_loader=...)
user = await client.get_user(id=1)
# session never closed -> "Unclosed client session" warnings, leaked sockets
Correct:
async with UserClient(
base_url="https://api.example.com",
request_dumper=AdaptixDumper(DEFAULT_RETORT),
response_loader=AdaptixLoader(DEFAULT_RETORT),
) as client:
user = await client.get_user(id=1)
unihttp handles errors in layers — pick the narrowest one that fits.
Import transport exceptions from unihttp.exceptions. Since 0.4.0, backend
error mappings cover more failures when sending requests and reading responses:
NetworkError and RequestTimeoutError: candidates for retries when replaying
the operation is safe.NonRetryableError: deterministic failures such as invalid URLs and redirect
loops; correct the request or configuration.UniHTTPError: other mapped backend errors; do not retry the base
class, which also includes NonRetryableError and HTTP status errors.Mapped exceptions retain the original as __cause__. Exceptions outside a
backend's mapping propagate unchanged, such as RuntimeError from a closed
HTTPX client. Some misuse errors, including HTTPX's StreamConsumed and
CookieConflict, are mapped to plain UniHTTPError.
HTTP status errors require explicit handling. Keep backend session status raising disabled so unihttp hooks and middleware can inspect the response.
ifsFor "turn HTTP status N into exception X" rules that apply across endpoints, use the error-mapper middleware instead of repeating status checks. Match the sync/async variant to your client.
Wrong:
class UserClient(AiohttpAsyncClient):
async def get_user(self, id: int) -> User:
user = await self.call_method(GetUser(id=id))
# status handling buried in every method, duplicated everywhere
return user
Correct:
from http import HTTPStatus
from unihttp.middlewares import AsyncErrorMapperMiddleware
client = UserClient(
base_url="https://api.example.com",
request_dumper=AdaptixDumper(DEFAULT_RETORT),
response_loader=AdaptixLoader(DEFAULT_RETORT),
middleware=[
AsyncErrorMapperMiddleware({
HTTPStatus.NOT_FOUND: UserNotFound,
HTTPStatus.UNAUTHORIZED: SessionExpired,
}),
],
)
Keys may be an int/HTTPStatus, a range, or a tuple of codes. An exception
class is instantiated as ExcClass(message, response) (so it must accept those
two positional args — a plain Exception subclass does). Pass lambda r: ... when
you need custom construction, e.g. 429: lambda r: RateLimited(r.headers["Retry-After"]).
on_error (method) or handle_error (client) for targeted handlingUse a method's on_error to raise an endpoint-specific exception, and the
client's handle_error for cross-cutting concerns like auth. on_error is a
raise-or-pass hook: its return value is ignored, so return None does
not make the call return None — it just lets the pipeline fall through and
deserialize the error body. To turn a 404 into a None result, override
make_response or wrap the call in a call_method method that catches the error.
@dataclass
class GetUser(BaseMethod[User]):
__url__ = "/users/{id}"
__method__ = "GET"
id: Path[int]
def on_error(self, response: HTTPResponse) -> None:
if response.status_code == 404:
raise UserNotFound(self.id) # endpoint-specific error
return super().on_error(response)
validate_responseSome APIs return 200 with {"ok": false}. For buffered responses, override
validate_response on the method or client. The method hook below takes only
response; the client hook takes (self, response, method).
def validate_response(self, response: HTTPResponse) -> None:
if isinstance(response.data, dict) and response.data.get("ok") is False:
raise ApiError(response.data.get("error", "unknown error"))
make_responseFor BaseMethod, bind_method returns only the deserialized body — headers,
status code, and cookies are invisible to callers by default. When you need them (a Link
pagination header, a rate-limit header, a Location), override the method's
make_response; it receives the full HTTPResponse (.headers, .status_code,
.cookies, .data). The return type then becomes your wrapper, not the bare body.
from unihttp.http.response import HTTPResponse
from unihttp.serialize import ResponseLoader
@dataclass
class ListIssues(BaseMethod[IssuePage]): # IssuePage wraps items + next_link
__url__ = "/repos/{owner}/{repo}/issues"
__method__ = "GET"
owner: Path[str]
repo: Path[str]
def make_response(
self,
response: HTTPResponse,
response_loader: ResponseLoader,
) -> IssuePage:
# The base method's parameter is `response_loader` and the client calls it
# by keyword — keep that exact name or the override raises TypeError.
items = response_loader.load(response.data, list[Issue])
return IssuePage(items=items, next_link=parse_link(response.headers.get("Link")))
Pass middleware=[...] to the client constructor for all operations, to
bind_method(GetUser, middleware=[...]) for one binding, or to
client.call_method(GetUser(id=1), middleware=[...]) for one call.
call_method_stream accepts the same keyword; async clients require await
and async middleware. Do not put __middleware__ on method classes or pass
middleware= to a bound call, whose arguments construct the method itself.
Client middleware wraps binding/call middleware. Within each list, the first
entry is outermost; the response passes through in reverse order. A direct
call_method bypasses middleware attached to a binding.
For buffered status retries, place the error mapper outside the retry
middleware: [status_mapper, retry]. Hooks run inside the chain, so a hook
that raises on a retryable status prevents status-based retries. Exceptions
are retried only when listed in exceptions; opt into
[NetworkError, RequestTimeoutError] when safe. retries=3 means up to four
attempts; defaults cover statuses 500, 502, 503, and 504. Retries do not check
idempotency or implement Retry-After handling. Read the
streaming reference before adding retries to downloads.
next_handlerSync clients take Middleware (sync handle); async clients take
AsyncMiddleware (async handle). Always pass the request to next_handler and
return its response, unless you deliberately short-circuit.
Correct (async client -> async middleware):
from unihttp.middlewares import AsyncHandler, AsyncMiddleware
from unihttp.http.request import HTTPRequest
from unihttp.http.response import HTTPResponse
class AuthMiddleware(AsyncMiddleware):
def __init__(self, token: str) -> None:
self._token = token
async def handle(
self,
request: HTTPRequest,
next_handler: AsyncHandler,
) -> HTTPResponse:
request.header["Authorization"] = f"Bearer {self._token}"
return await next_handler(request)
Type next_handler (AsyncHandler for async, Handler for sync — both from
unihttp.middlewares); leaving it unannotated only passes mypy because the
generated mypy.ini mutes no-untyped-def. Note the field name asymmetry: a
request carries request.header (singular), a response uses
response.headers (plural). For bearer/API-key auth, take the token as a client
__init__ parameter and attach this middleware there. For transient failures,
prefer the built-in RetryMiddleware / AsyncRetryMiddleware over a hand-rolled
retry loop.
json_dumps / json_loadsimport orjson
client = UserClient(
base_url="https://api.example.com",
request_dumper=AdaptixDumper(DEFAULT_RETORT),
response_loader=AdaptixLoader(DEFAULT_RETORT),
json_dumps=lambda obj: orjson.dumps(obj).decode(),
json_loads=orjson.loads,
)
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
2 skills in this plugin
npx claudepluginhub goduni/unihttp --plugin unihttpOther plugins' skills for the same job