Skip to main content

An utility for writing simple clients for REST like APIs

Project description

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

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

descanso-0.6.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

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

descanso-0.6.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file descanso-0.6.0.tar.gz.

File metadata

  • Download URL: descanso-0.6.0.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for descanso-0.6.0.tar.gz
Algorithm Hash digest
SHA256 3679217b7f117d5355f0a60e32ee146c81222bb4cb16f55dca35d44843e93b98
MD5 763a3ca91a159e053f8aaa1c52f54f88
BLAKE2b-256 69ba7af620b8af2872b8aed654948c077eee23da79cf6e2cddda5fb1259ac102

See more details on using hashes here.

File details

Details for the file descanso-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: descanso-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for descanso-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b300fc36fdd2485a20ae8fb696ec9509c0b571fa6291310587e4e6d8171170e5
MD5 43370b9cedf5520e9827e696bd4e9655
BLAKE2b-256 2a328bd53b4e1a9e8550cbd1e286e8dad4bbed3ce3ccad031a564daef499bac7

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