Coder Social home page Coder Social logo

Comments (3)

jarikujansuu avatar jarikujansuu commented on May 26, 2024

Both underlying libraries aiohttp and httpx support timeout for both verbs so would make sense to have support in both.

from aiodynamo.

ojii avatar ojii commented on May 26, 2024

get is used by credentials which has a different timeout logic than post which is used in combination with ThrottleConfig. I think that's why those are different.

To be honest, I think the whole aiodynamo.http.base.HTTP interface was a mistake (at least the way it was implemented). I still think that not tying aiodynamo tightly to a specific http library/version is a good goal (since those usually move quite fast and version conflicts would be quite probable), but the interface is terrible. The API disparity between get and post is a good example of that. It kind of makes sense if you look at the call sites why they return different things, but it's just confusing.

I think the actual interface for pluggable HTTP should be something more like the following and maybe it should be changed to that. Then, the timeout logic and handling dynamo-level errors (400s, exception_from_response) could be handled internally and the http-glue code doesn't have to worry about it:

from dataclasses import dataclass
from typing import Optional, Callable, Awaitable, Literal, Union, Dict

import aiohttp
import httpx


@dataclass(frozen=True)
class Request:
    method: Union[Literal["GET"], Literal["POST"]]
    url: str
    headers: Dict[str, str]  # Mapping is more appropriate but httpx doesn't like it...
    body: Optional[bytes]


@dataclass(frozen=True)
class Response:
    status: int
    body: bytes  # may be empty bytes


@dataclass
class RequestFailed(Exception):
    inner: Exception


HTTPInterface = Callable[
    [Request], Awaitable[Response]
]  # may throw RequestFailed for connection based errors.


# Example implementations, actual implementations would likely be a class with an async __call__ so sessions etc
# can be re-used, for simplicity, that is ignored here.


async def aiohttp_impl(request: Request) -> Response:
    session: aiohttp.ClientSession
    try:
        async with session.request(
            method=request.method,
            url=request.url,
            headers=request.headers,
            body=request.body,
        ) as response:
            return Response(response.status, await response.read())
    except aiohttp.ClientError as exc:
        raise RequestFailed(exc)


async def httpx_impl(request: Request) -> Response:
    client: httpx.AsyncClient
    try:
        response = await client.request(
            method=request.method,
            url=request.url,
            headers=request.headers,
            content=request.body,
        )
        return Response(response.status_code, await response.aread())
    except httpx.HTTPError as exc:
        raise RequestFailed(exc)

Would that be better and would this change be worth it?

from aiodynamo.

dimaqq avatar dimaqq commented on May 26, 2024

I quite like it!

Now is a good time to consider breaking changes, since 2022 is coming up and that changes the major version :)

from aiodynamo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.