Arrest simplifies REST API interactions in Python by offering a type-safe HTTP client. Built on httpx and Pydantic, it ensures robust validation and clean structure for API calls. Define and manage routes, request/response models, and error handling elegantly, while enjoying enhanced autocompletion and static analysis support.
Arrest is a powerful type-safe HTTP client designed for Python, leveraging the robust capabilities of httpx and Pydantic. This utility simplifies the structuring and validation of REST API calls by allowing users to define APIs declaratively in a single, organized manner, covering routes, request and response models, retries, and error handling. By using Arrest, developers benefit from enhanced static analysis, autocomplete, and runtime validation for each API call.
application/json, application/x-www-form-urlencoded, multipart/form-data, and application/xml.RequestError for handling transport failures.H() helper allows for convenient handler definitions with keyword-argument autocomplete.Here’s a simple example to demonstrate how to use Arrest:
from arrest import H, Resource, Service, GET, POST
user = Resource(
route="/users",
handlers=[
H(GET, "/"),
H(GET, "/{user_id}"),
H(POST, "/", request=NewUserRequest, response=UserResponse),
],
)
svc = Service(
name="api",
url="https://api.example.com/v1",
resources=[user],
)
# GET /users
resp = await svc.users.get("/")
if resp.is_success:
print(resp.data)
# POST /users with a type-safe request body
resp = await svc.users.post("/", request=NewUserRequest(name="Alice", email="a@b.com"))
Use cases are expanded to handle XML, form, and file uploads effortlessly:
from pydantic_xml import BaseXmlModel, element
from arrest.params import Form, File
from arrest.types import UploadFile
# XML request/response definition
class XmlPayload(BaseXmlModel, tag="payload"):
key: str = element()
value: str = element()
# Form Data Model
class Login(BaseModel):
username: str = Form(...)
password: str = Form(...)
# Profile with Multipart File Upload
class Profile(BaseModel):
name: str = Form(...)
avatar: UploadFile = File(...)
Easily generate models and services from an OpenAPI specification with the following command:
arrest --url https://petstore3.swagger.io/api/v3/openapi.json -o ./generated
For comprehensive documentation, visit Arrest Documentation.
No comments yet.
Sign in to be the first to comment.