Skip to main content

Descanso

PyPI version Supported versions Downloads License GitHub Actions Workflow Status Doc Telegram

A modern and simple way to create clients for REST like APIs

If you are looking for dataclass-rest, this is the same project with a new name.

We had reworked it a lot, but old code is still available in a separate branch.

Quickstart

Step 1. Install

pip install descanso requests adaptix

requests and adaptix are optional dependencies, but we will use them in quickstart.

Step 2. Declare models

from dataclasses import dataclass

@dataclass
class Todo:
    id: int
    user_id: int
    title: str
    completed: bool

Step 3. Select serialization library. We recommend using adaptix.

You need to have Loader and Dumper implementations, adaptix.Retort would be fine

Step 4. Configure RestBuilder instance. It is needed to reuse common step during request.

from adaptix import Retort
from descanso import RestBuilder


rest = RestBuilder(
    request_body_dumper=Retort(),
    response_body_loader=Retort(),
    query_param_dumper=Retort(),
)

Step 5. Create client class. Use RequestsClient or AiohttpClient

from descanso.http.requests import RequestsClient

class RealClient(RequestsClient):
    ...

Step 6. Declare methods using rest.get/rest.post/rest.delete/rest.patch/rest.put decorators. You can override RestBuilder params if needed. Type hints are required. Body of method is ignored.

Use any method arguments to format URL. body argument is sent as request body with json. Other arguments, not used in the URL are passed as query parameters.

from descanso.http.requests import RequestsClient

class RealClient(RequestsClient):
    @rest.get("todos/{id}")
    def get_todo(self, id: str) -> Todo:
        pass

    @rest.get("todos")
    def list_todos(self, user_id: int | None) -> list[Todo]:
        pass

    @rest.delete("todos/{id}")
    def delete_todo(self, id: int):
        pass

    @rest.post("todos")
    def create_todo(self, body: Todo) -> Todo:
        pass

Step 7. Create client instance and use it.

from requests import Session

client = RealClient(
    base_url="https://example.com/api",
    session=Session()
)
client.get_todo(5)

Asyncio

To use async client instead of sync:

  1. Install aiohttp (instead of requests)
  2. Change descanso.http.requests.RequestsClient to descanso.http.aiohttp.AiohttpClient
  3. All methods will be async, but you can add async keyword to make it more verbose

Metadata

Release files for descanso 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for descanso 0.7.1
File Size Uploaded
descanso-0.7.1.tar.gz 40.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for descanso 0.7.1
File Interpreter ABI Platform
descanso-0.7.1-py3-none-any.whl Python 3 none any Details

Total release size: 64.9 kB

Release files / descanso-0.7.1.tar.gz

Download URL descanso-0.7.1.tar.gz
Size 40.8 kB
Tags Source
SHA-256 checksum
How to use checksums
43fffd408b31a02ebafde56d603ecd0f60886a945d8fb87a3859c993630e7ed5
BLAKE2b-256 checksum
How to use checksums
6bd529786db6e85c961853f1bbfc9988b7f34c98c2a20479916d8a125095ad48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / descanso-0.7.1-py3-none-any.whl

Download URL descanso-0.7.1-py3-none-any.whl
Size 24.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d3d5509b7f36ab19691ab9f64b7edc51f24739e7f9ea3ec237fb913f0cff5a49
BLAKE2b-256 checksum
How to use checksums
2ee9ae2ceeedc2cdadd5fce8ced4d271ad83921f2acd6b82972a45f466746868
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.7.1 This release

2 release files

0.7.0

2 release files

0.6.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page