-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
question: connector get/post parity #45
Comments
Both underlying libraries |
To be honest, I think the whole 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, 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? |
I quite like it! Now is a good time to consider breaking changes, since 2022 is coming up and that changes the major version :) |
How come
get
has atimeout=
mandatory keyword argument, butpost
does not have atimeout=
argument?The text was updated successfully, but these errors were encountered: