Skip to content

Commit

Permalink
Add support for window size config
Browse files Browse the repository at this point in the history
CR fixes

(cherry picked from commit praw-dev/praw@ba1b87d)

Fix another test

(cherry picked from commit praw-dev/praw@bf79ea2)

Fix a test

(cherry picked from commit praw-dev/praw@b8ee8e6)

How about this

(cherry picked from commit praw-dev/praw@1c4590b)

Add to changelog

(cherry picked from commit praw-dev/praw@12ee566)

Update logging docs

(cherry picked from commit praw-dev/praw@e4bb2ec)

Add doc note here

(cherry picked from commit praw-dev/praw@7710133)

Fix black

(cherry picked from commit praw-dev/praw@c5fa92e)

Bare bones implementation

(cherry picked from commit praw-dev/praw@2f4d3d9)
  • Loading branch information
Watchful1 authored and LilSpazJoekp committed Oct 20, 2024
1 parent 7d477d8 commit 14da2b7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ Unreleased
- :func:`.stream_generator` now accepts the ``continue_after_id`` parameter, which
starts the stream after a given item ID.
- Support for new share URL format created from Reddit's mobile apps.
- :class:`.Reddit` has a new configurable parameter, ``window_size``. This tells PRAW
how long reddit's rate limit window is. This defaults to 600 seconds and shouldn't
need to be changed unless reddit changes the size of their rate limit window.

**Fixed**

- An issue where submitting a post with media would fail due to an API change.

**Changed**

Expand Down
1 change: 1 addition & 0 deletions asyncpraw/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def _initialize_attributes(self):
self.warn_additional_fetch_params = self._config_boolean(
self._fetch_default("warn_additional_fetch_params", default=True)
)
self.window_size = self._fetch_default("window_size", default=600)
self.kinds = {
x: self._fetch(f"{x}_kind")
for x in [
Expand Down
9 changes: 7 additions & 2 deletions asyncpraw/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ async def authorize(self, code: str) -> str | None:
authenticator = self._reddit._read_only_core._authorizer._authenticator
authorizer = Authorizer(authenticator)
await authorizer.authorize(code)
authorized_session = session(authorizer)
authorized_session = session(
authorizer=authorizer, window_size=self._reddit.config.window_size
)
self._reddit._core = self._reddit._authorized_core = authorized_session
return authorizer.refresh_token

Expand All @@ -83,7 +85,10 @@ def implicit(self, *, access_token: str, expires_in: int, scope: str) -> None:
if not isinstance(authenticator, UntrustedAuthenticator):
raise InvalidImplicitAuth
implicit_session = session(
ImplicitAuthorizer(authenticator, access_token, expires_in, scope)
authorizer=ImplicitAuthorizer(
authenticator, access_token, expires_in, scope
),
window_size=self._reddit.config.window_size,
)
self._reddit._core = self._reddit._authorized_core = implicit_session

Expand Down
16 changes: 12 additions & 4 deletions asyncpraw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,9 @@ def _prepare_common_authorizer(
else:
self._core = self._read_only_core
return
self._core = self._authorized_core = session(authorizer)
self._core = self._authorized_core = session(
authorizer=authorizer, window_size=self.config.window_size
)

def _prepare_objector(self):
mappings = {
Expand Down Expand Up @@ -686,13 +688,17 @@ def _prepare_trusted_asyncprawcore(self, requestor: Requestor):
self.config.redirect_uri,
)
read_only_authorizer = ReadOnlyAuthorizer(authenticator)
self._read_only_core = session(read_only_authorizer)
self._read_only_core = session(
authorizer=read_only_authorizer, window_size=self.config.window_size
)

if self.config.username and self.config.password:
script_authorizer = ScriptAuthorizer(
authenticator, self.config.username, self.config.password
)
self._core = self._authorized_core = session(script_authorizer)
self._core = self._authorized_core = session(
authorizer=script_authorizer, window_size=self.config.window_size
)
else:
self._prepare_common_authorizer(authenticator)

Expand All @@ -701,7 +707,9 @@ def _prepare_untrusted_asyncprawcore(self, requestor: Requestor):
requestor, self.config.client_id, self.config.redirect_uri
)
read_only_authorizer = DeviceIDAuthorizer(authenticator)
self._read_only_core = session(read_only_authorizer)
self._read_only_core = session(
authorizer=read_only_authorizer, window_size=self.config.window_size
)
self._prepare_common_authorizer(authenticator)

async def _resolve_share_url(self, url: str) -> str:
Expand Down
1 change: 1 addition & 0 deletions docs/getting_started/configuration/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ PRAW.
before throwing an exception.
:warn_comment_sort: When ``true``, log a warning when the ``comment_sort`` attribute of
a submission is updated after ``_fetch()`` has been called (default: ``true``).
:window_size: The number of seconds between rate limit resets (default: 600).

.. _custom_options:

Expand Down
25 changes: 22 additions & 3 deletions docs/getting_started/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,39 @@ Add the following to your code to log everything available:
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
for logger_name in ("asyncpraw", "asyncprawcore"):
for logger_name in ("praw", "prawcore"):
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
Or you can use the following to write the logs to a file for longer running bots or
scripts when you need to look back at what the bot did hours ago.

.. code-block:: python
import logging
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
file_handler = logging.handlers.RotatingFileHandler(
"praw_log.txt", maxBytes=1024 * 1024 * 16, backupCount=5
)
file_handler.setLevel(logging.DEBUG)
for logger_name in ("asyncpraw", "asyncprawcore"):
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
logger.addHandler(stream_handler)
logger.addHandler(file_handler)
When properly configured, HTTP requests that are issued should produce output similar to
the following:

.. code-block:: text
Fetching: GET https://oauth.reddit.com/api/v1/me
Fetching: GET https://oauth.reddit.com/api/v1/me at 1691743155.4952002
Data: None
Params: {'raw_json': 1}
Response: 200 (876 bytes)
Response: 200 (876 bytes) (rst-45:rem-892:used-104 ratelimit) at 1691743156.3847592
Furthermore, any API ratelimits from POST actions that are handled will produce a log
entry with a message similar to the following message:
Expand Down

0 comments on commit 14da2b7

Please sign in to comment.