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

Window size config #1975

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Unreleased
templates.
- :meth:`~.SubredditRedditorFlairTemplates.reorder` to reorder a subreddit's redditor
flair templates.
- :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.

7.7.1 (2023/07/11)
------------------
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 @@ -72,6 +72,7 @@ order to successfully access a third-party Reddit site:
(default: ``t3_``).
:subreddit_kind: The type prefix for subreddits on the :class:`.Reddit` instance
(default: ``t5_``).
:window_size: The number of seconds between rate limit resets (default: 600).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be in a different section. This section is for configuring PRAW with a custom Reddit instance.


.. _misc_options:

Expand Down
15 changes: 10 additions & 5 deletions docs/getting_started/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ Add the following to your code to log everything available:

import logging

handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
file_handler = logging.handlers.RotatingFileHandler(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be too advanced. Maybe add a second example explaining why you recommend a rotating file handler.

"praw_log.txt", maxBytes=1024 * 1024 * 16, backupCount=5
)
file_handler.setLevel(logging.DEBUG)
for logger_name in ("praw", "prawcore"):
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is from prawcore but what do you think about changing the epoch to a timestamp? It would only be 1 or 2 more characters and would be human readable.


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
1 change: 1 addition & 0 deletions praw/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def _initialize_attributes(self): # noqa: ANN001
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 praw/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def authorize(self, code: str) -> str | None:
authenticator = self._reddit._read_only_core._authorizer._authenticator
authorizer = Authorizer(authenticator)
authorizer.authorize(code)
authorized_session = session(authorizer)
authorized_session = session(
authorizer=authorizer, window_size=self.config.window_size
)
self._reddit._core = self._reddit._authorized_core = authorized_session
return authorizer.refresh_token

Expand All @@ -77,7 +79,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.config.window_size,
)
self._reddit._core = self._reddit._authorized_core = implicit_session

Expand Down
16 changes: 12 additions & 4 deletions praw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,9 @@ def _prepare_common_authorizer(self, authenticator): # noqa: ANN001
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): # noqa: ANN001
mappings = {
Expand Down Expand Up @@ -622,13 +624,17 @@ def _prepare_trusted_prawcore(self, requestor): # noqa: ANN001
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 @@ -637,7 +643,9 @@ def _prepare_untrusted_prawcore(self, requestor): # noqa: ANN001
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)

@_deprecate_args("id", "url")
Expand Down
Loading