Skip to content

Async Client? #1

@Ali-Parandeh

Description

@Ali-Parandeh

Hey guys, Thanks a lot for building this Python client. Just wondering if you have an async client baked in to the SDK? I normally can't use non-async clients since my whole API is async and it'll block the server

Use httpx or aiohttp package for sending HTTP requests to your server instead of requests package.

import json
import aiohttp
from .encoder import Encoder


class Client:
    def __init__(self, config): ...
    async def _request(self, method, endpoint, payload=None):
         url = f"{self.base_url}{endpoint}"
        async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=self.timeout)) as session:
             async with session.request(
               method,
               url,
               data=json.dumps(payload, cls=Encoder) if payload is not None else None
        ) as response:
            response.raise_for_status()
            return await response.json()

    async def get(self, endpoint):
        return await self._request("GET", endpoint)

    async def post(self, endpoint, payload=None):
        return await self._request("POST", endpoint, payload)

    async def put(self, endpoint, payload=None):
        return await self._request("PUT", endpoint, payload)

    async def delete(self, endpoint, payload=None):
        return await self._request("DELETE", endpoint, payload)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions