praw-dev / prawcore

Low-level communication layer for PRAW 4+.
BSD 2-Clause "Simplified" License
21 stars 36 forks source link

Support alternate HTTP client libraries, like httpx #141

Open kevinlul opened 1 year ago

kevinlul commented 1 year ago

Describe the solution you'd like

prawcore is currently tied to requests and asyncprawcore to aiohttp. I like how httpx has both async and sync capabilities and would like to use one HTTP library in my project as I will be calling other APIs as well. I would also like to see if httpx's HTTP/2 implementation provides any latency improvement over HTTP/1.1 implementations.

httpx is broadly compatible with requests but httpx.Client does not implement the exact API as requests.Session: https://www.python-httpx.org/compatibility/

Naively constructing praw.Reddit with requestor_kwargs={"session": client} where client = Client(http2=True), I was able to run praw's quickstart example listing 10 hot submissions with the below two patches:

https://github.com/praw-dev/prawcore/blob/16fe48c3b4ea82c18dca0985e79828676c988321/prawcore/auth.py#L51

replace with data=data, httpx only supports dict syntax and not a list of tuples

https://github.com/praw-dev/prawcore/blob/16fe48c3b4ea82c18dca0985e79828676c988321/prawcore/sessions.py#L210

remove entirely, as httpx names the parameter differently and defaults to no redirects anyway

Describe alternatives you've considered

No response

Additional context

https://praw.slack.com/archives/CRZ00QLC9/p1681666057106319

Ousret commented 9 months ago

Hello there,

Sorry to barge in, There's an alternative you may be interested in. https://github.com/jawah/niquests

This is a drop-in-worthy replacement for Requests that ships with modern capabilities and allows you a painless transition. I can answer any concerns you may have.