Skip to content
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

[Feature] Simulate aiohttp interface for async operations #202

Open
Snusmumr1000 opened this issue Jan 1, 2024 · 7 comments
Open

[Feature] Simulate aiohttp interface for async operations #202

Snusmumr1000 opened this issue Jan 1, 2024 · 7 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers
Milestone

Comments

@Snusmumr1000
Copy link
Contributor

Is your feature request related to a problem? Please describe.
aiohttp is a widely used library, which includes connection via HTTP and WebSockets. It would be extremely helpful to have a aiohttp-like interface for this library as it will:

  1. Flatten the learning curve.
  2. Make the integration in existing projects much easier.
  3. Probably will attract more people to use the library.

Describe the solution you'd like
Adapter-layer would be enough that would simulate the aiohttp behaviour.

Describe alternatives you've considered
Write an own one is a possible solution.

However, if we have a requests library interface simulation, why not implement an aiohttp interface as a standard for async version?

@Snusmumr1000 Snusmumr1000 added the enhancement New feature or request label Jan 1, 2024
@Snusmumr1000
Copy link
Contributor Author

I can help you implement this feature if you agree with this proposal

@dolfies
Copy link
Contributor

dolfies commented Jan 1, 2024

In terms of requests, the AsyncSession interface is already similar enough to aiohttp that I don't think it matters

@yifeikong
Copy link
Owner

For the asyncio interface, I trid to mimic what httpx does if possible. As @dolfies said, this is similar to what aiohttp does. Could you please describe in details about what is missing?

@Snusmumr1000
Copy link
Contributor Author

Snusmumr1000 commented Jan 2, 2024

Sure, here are the features I meant:

WebSocket

Using async / sync generator for receiving messages.

IMO, aiohttp has quite a convenient interface for receiving messages. Example:

async with session.ws_connect(ws_url) as ws:  # not necessary
    async for msg in ws:  # exactly what I mean
        # do something with msg

Unfortunately, aiohttp does not provide a way of choosing the "generated" messages type, so maybe it would also be nice to add a type of received messages (bytes / str / json).

Specific datatypes wrapper methods:

aiohttp has wrapper methods for sending and receiving specific datatypes, they encapsulate some boilerplate conversion code and make library UX more user-friendly.

async def send_str(self, data: str, compress: Optional[int] = None) -> None:
    ...

async def send_bytes(self, data: bytes, compress: Optional[int] = None) -> None:
    ...

async def send_json(
    self,
    data: Any,
    compress: Optional[int] = None,
    *,
    dumps: JSONEncoder = DEFAULT_JSON_ENCODER,
) -> None:
    ...
    
async def receive_str(self, *, timeout: Optional[float] = None) -> str:
    ...

async def receive_bytes(self, *, timeout: Optional[float] = None) -> bytes:
    ...

async def receive_json(
    self,
    *,
    loads: JSONDecoder = DEFAULT_JSON_DECODER,
    timeout: Optional[float] = None,
) -> Any:
    ...

HTTP Requests

Single request proxy passing

For me, it seems quite convenient (at least in terms of some quick prototyping or testing) to pass a proxy to a single request as a uri:

session.get("http://python.org",
            proxy="http://user:pass@some.proxy.com")

However, I understand, that it might not correlate well with already chosen way of handling proxies.

@yifeikong yifeikong added the good first issue Good for newcomers label Jan 3, 2024
@yifeikong
Copy link
Owner

All of these are valid requests, I guess. I'm happy to merge PRs if someone would be kind enough to implement them. But for the asyncio websockets, be aware that the underlying mechanism is sync-based.

@dolfies
Copy link
Contributor

dolfies commented Jan 3, 2024

I can definitely look into implementing some of these

@Snusmumr1000
Copy link
Contributor Author

Perfect, I'm glad to hear that the proposal seemed somehow useful to you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: In Progress
Development

No branches or pull requests

3 participants