Skip to main content

Reusable and richly typed wrapper over the Discord REST API

Project description

Wumpy-rest

Richly and accurately typed wrapper around the Discord REST API.

Usage

The best way to use wumpy-rest is to import APIClient:

import anyio
from wumpy.rest import APIClient


TOKEN = 'ABC123.XYZ789'


async def main():
    async with APIClient(headers={'Authentication': f'Bot {TOKEN}'}) as api:
        print(await api.fetch_my_user())


anyio.run(main)

APIClient is a class that implements all routes of the Discord API. This is made up of multiple route classes. You can create your own class with the routes you use:

from wumpy.rest import ApplicationCommandRequester, InteractionRequester


class MyAPIClient(ApplicationCommandRequester, InteractionRequester):

    __slots__ = ()  # Save some memory for this class

Files

Some endpoints support uploading files, for these a file-like object is expected that's been opened in binary-mode (for example 'rb').

For the message/interaction endpoints, remember to include a matching attachment object with 'id' set to the index of the file.

Ratelimiter

You can pass a custom ratelimiter to the requester if you want to customize that behaviour. For more, read the documentation. Here's an example of a ratelimiter that does no ratelimiting and does not handle any kind of 429-responses.

from contextlib import asynccontextmanager
from typing import (
    Any, AsyncContextManager, AsyncGenerator, Awaitable, Callable, Coroutine,
    Mapping
)

import anyio
from wumpy.rest import APIClient


class NoOpRatelimiter:
    """Ratelimiter implementation that does nothing; a no-op implementation."""

    async def __aenter__(self) -> Callable[
        [Route], AsyncContextManager[
            Callable[[Mapping[str, str]], Awaitable]
        ]
    ]:
        return self.acquire

    async def __aexit__(
        self,
        exc_type: Optional[Type[BaseException]],
        exc_val: Optional[BaseException],
        exc_tb: Optional[TracebackType]
    ) -> object:
        pass

    async def update(self, headers: Mapping[str, str]) -> object:
        pass

    @asynccontextmanager
    async def acquire(self, route: Route) -> AsyncGenerator[
        Callable[[Mapping[str, str]], Coroutine[Any, Any, object]],
        None
    ]:
        # The return type may look a little weird, but this is how
        # @asynccontextmanager works. You pass it a function that returns an
        # async generator (which yields what the asynchronous context manager
        # then returns).
        yield self.update


async def main():
    async with APIClient(
        NoOpRatelimiter(),
        headers={'Authentication': f'Bot {TOKEN}'}
    ) as api:
        print(await api.fetch_my_user())


anyio.run(main)

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

wumpy-rest-0.1.0.tar.gz (36.7 kB view details)

Uploaded Source

Built Distribution

wumpy_rest-0.1.0-py3-none-any.whl (43.4 kB view details)

Uploaded Python 3

File details

Details for the file wumpy-rest-0.1.0.tar.gz.

File metadata

  • Download URL: wumpy-rest-0.1.0.tar.gz
  • Upload date:
  • Size: 36.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.26.0

File hashes

Hashes for wumpy-rest-0.1.0.tar.gz
Algorithm Hash digest
SHA256 15fbb27a8922cb3a5c385e98754b65fa4f6c9bcf088aed065a14c0859121fdc2
MD5 81b48583e9157bd30e5add16f8f46a8c
BLAKE2b-256 3786d01566b8e44dc75e3a13c51163796497847df34851e499dd6ba8f2c02d19

See more details on using hashes here.

File details

Details for the file wumpy_rest-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: wumpy_rest-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 43.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.26.0

File hashes

Hashes for wumpy_rest-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6cf11fedb920040eb20979bb0ec7ac3de48284169af6689ef2474b3f262560f6
MD5 dbfa32f7d6834447ec05e4e29b3b1155
BLAKE2b-256 b99cd22f8803d5719bf8b9d095e0f686329477baf158ae86847e25741ff47c92

See more details on using hashes here.

Supported by

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