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

Replace Requests http client by Niquests and add built-in async support #186

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ jobs:
name: CI
secrets: inherit
uses: praw-dev/.github/.github/workflows/ci.yml@main
env:
# this avoids pytest loading betamax+Requests at boot.
# this allows us to patch betamax and makes it use Niquests instead.
PYTEST_DISABLE_PLUGIN_AUTOLOAD: "1"
Ousret marked this conversation as resolved.
Show resolved Hide resolved
with:
package: prawcore
name: CI
Expand Down
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ prawcore follows `semantic versioning <https://semver.org/>`_.
Unreleased
----------

**Added**

- Asynchronous interfaces from our synchronous ones. Find :class:`AsyncRequestor` for
:class:`Requestor` and :class:`AsyncSession` for :class:`Session`.

**Changed**

- Switch HTTP Client Requests for the compatible Niquests. Support for HTTP/2+
introduced with mirrored sync / async interfaces.

2.4.0 (2023/10/01)
------------------

Expand Down
26 changes: 26 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ Save the above as ``trophies.py`` and then execute via:
Additional examples can be found at:
https://github.com/praw-dev/prawcore/tree/main/examples

Or! with async/await!

.. code-block:: python

#!/usr/bin/env python
import os
import pprint
import asyncio
import prawcore


async def main():
authenticator = prawcore.AsyncTrustedAuthenticator(
prawcore.Requestor("YOUR_VALID_USER_AGENT"),
os.environ["PRAWCORE_CLIENT_ID"],
os.environ["PRAWCORE_CLIENT_SECRET"],
)
authorizer = prawcore.AsyncReadOnlyAuthorizer(authenticator)
await authorizer.refresh()

async with prawcore.async_session(authorizer) as session:
pprint.pprint(await session.request("GET", "/api/v1/user/bboe/trophies"))


asyncio.run(main())

Depending on prawcore
---------------------

Expand Down
4 changes: 2 additions & 2 deletions examples/caching_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import os
import sys

import requests
import niquests

import prawcore


class CachingSession(requests.Session):
class CachingSession(niquests.Session):
"""Cache GETs in memory.

Toy example of custom session to showcase the ``session`` parameter of
Expand Down
11 changes: 11 additions & 0 deletions prawcore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

import logging

from ._async.auth import (
AsyncAuthorizer,
AsyncDeviceIDAuthorizer,
AsyncImplicitAuthorizer,
AsyncReadOnlyAuthorizer,
AsyncScriptAuthorizer,
AsyncTrustedAuthenticator,
AsyncUntrustedAuthenticator,
)
from ._async.requestor import AsyncRequestor
from ._async.sessions import AsyncSession, async_session
from .auth import (
Authorizer,
DeviceIDAuthorizer,
Expand Down
2 changes: 2 additions & 0 deletions prawcore/_async/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# this part should be autogenerated
# mirror of our synchronous part.
Loading