Skip to main content

A lightweight, spec-driven builder for API clients and controllers.

Project description

saronia

A lightweight, spec-driven builder for API clients and controllers.

  • Declarative API controller syntax
  • Type-safe request/response handling with APIResult
  • Support for multiple HTTP clients (rnet, aiohttp, or custom)
  • Comprehensive error handling with StatusError mixin
  • Support for path parameters, query parameters, headers, body, JSON, form data, and file uploads
  • Built on top of msgspex for fast serialization and kungfu for functional types

Getting Started

# Base installation
pip install saronia

# With rnet client
pip install saronia[rnet]

# With aiohttp client
pip install saronia[aiohttp]
import asyncio
from uuid import UUID
from http import HTTPStatus

from kungfu import Error
from msgspex import Model
from saronia import API, APIResult, HTTPBearer, StatusError, get, post

from rnet import Client
from saronia import RnetClient


class Auth(Model):
    bearer: HTTPBearer


cool_api = API.endpoint("/coolapi/v1").bind_auth(Auth)


class ValidationError(Model, StatusError[HTTPStatus.INTERNAL_SERVER_ERROR]):
    message: str


class NotFoundError(Model, StatusError[HTTPStatus.NOT_FOUND]):
    message: str


class Book(Model):
    id: UUID
    name: str


class CreateBookDTO(Model):
    id: UUID
    name: str


@cool_api("/books")
class BooksController:
    @get("/{book_id}")
    async def get_book_by_id(self, book_id: UUID) -> APIResult[Book, ValidationError | NotFoundError]:
        ...

    @post("/create", CreateBookDTO)
    async def create_book(self) -> APIResult[Book, ValidationError | NotFoundError]:
        ...


books = BooksController()


async def main() -> None:
    client = Client()

    cool_api.build(RnetClient(client, base_url="https://api.example.com", request_timeout=45.0))
    cool_api.auth(token=HTTPBearer("abc123..."))

    book = (await books.get_book_by_id(UUID("12345678-1234-5678-1234-567812345678"))).unwrap()
    print(f"Book: {book.name}")

    match await books.create_book(id=UUID("87654321-4321-8765-4321-876543218765"), name="New Book"):
        case Error(error):
            print(f"Error: {error}")


asyncio.run(main())

License

saronia is MIT licensed

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

saronia-1.2.1.tar.gz (65.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

saronia-1.2.1-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file saronia-1.2.1.tar.gz.

File metadata

  • Download URL: saronia-1.2.1.tar.gz
  • Upload date:
  • Size: 65.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for saronia-1.2.1.tar.gz
Algorithm Hash digest
SHA256 b3d457d358c004b3f5c10ecba111380be4272cb47b6930aa2475856c3eb56781
MD5 ed295fba0c6c11ae41dd2d254ebcd5fa
BLAKE2b-256 e23b7d3f2533ee219c5f052d8b2e27f46668a9e8814261f05f7e44885234e6e9

See more details on using hashes here.

File details

Details for the file saronia-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: saronia-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 23.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for saronia-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b0ddcdc3c276b040fc4a9f6f9bf4725a82fb6a3484b1810fa54b6d2440a5d177
MD5 d39711e11fd40550a79afaf27442cbbc
BLAKE2b-256 0dd8c2c56acafbacd1d1fb8f3d8a4517453dad96aec69d775fbf303fb71ec112

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page