Skip to content

Commit

Permalink
Sort praw.models.auth.Auth.url arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Jan 14, 2022
1 parent 711c0f8 commit d9799f8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/examples/obtain_refresh_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def main():
user_agent="obtain_refresh_token/v0 by u/bboe",
)
state = str(random.randint(0, 65000))
url = reddit.auth.url(scopes, state, "permanent")
url = reddit.auth.url(duration="permanent", scopes=scopes, state=state)
print(f"Now open this url in your browser: {url}")

client = receive_connection()
Expand Down
4 changes: 2 additions & 2 deletions docs/getting_started/authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ can do that as follows:
redirect_uri="http://localhost:8080",
user_agent="testscript by u/fakebot3",
)
print(reddit.auth.url(["identity"], "...", "permanent"))
print(reddit.auth.url(scopes=["identity"], state="...", duration="permanent"))
The above will output an authorization URL for a permanent token (i.e., the resulting
authorization will include both a short-lived ``access_token``, and a longer-lived,
Expand Down Expand Up @@ -191,7 +191,7 @@ redirect. For the implicit flow call :meth:`.url` like so:

.. code-block:: python
print(reddit.auth.url(["identity"], "...", implicit=True))
print(reddit.auth.url(scopes=["identity"], state="...", implicit=True))
Then use :meth:`.implicit` to provide the authorization to the :class:`.Reddit`
instance.
Expand Down
14 changes: 8 additions & 6 deletions praw/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,17 @@ def scopes(self) -> Set[str]:
authorizer.refresh()
return authorizer.scopes

@_deprecate_args("scopes", "state", "duration", "implicit")
def url(
self,
scopes: List[str],
state: str,
*,
duration: str = "permanent",
implicit: bool = False,
scopes: List[str],
state: str,
) -> str:
"""Return the URL used out-of-band to grant access to your application.
:param scopes: A list of OAuth scopes to request authorization for.
:param state: A string that will be reflected in the callback to
``redirect_uri``. This value should be temporarily unique to the client for
whom the URL was generated for.
:param duration: Either ``"permanent"`` or ``"temporary"`` (default:
``"permanent"``). ``"temporary"`` authorizations generate access tokens that
last only 1 hour. ``"permanent"`` authorizations additionally generate a
Expand All @@ -114,6 +112,10 @@ def url(
:param implicit: For **installed** applications, this value can be set to use
the implicit, rather than the code flow. When ``True``, the ``duration``
argument has no effect as only temporary tokens can be retrieved.
:param scopes: A list of OAuth scopes to request authorization for.
:param state: A string that will be reflected in the callback to
``redirect_uri``. This value should be temporarily unique to the client for
whom the URL was generated for.
"""
authenticator = self._reddit._read_only_core._authorizer._authenticator
Expand Down
14 changes: 9 additions & 5 deletions tests/unit/models/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_limits(self):
assert expected == app.auth.limits

def test_url__installed_app(self):
url = installed_app().auth.url(["dummy scope"], "dummy state")
url = installed_app().auth.url(scopes=["dummy scope"], state="dummy state")
assert "client_id=dummy+client" in url
assert "duration=permanent" in url
assert "redirect_uri=https%3A%2F%2Fdummy.tld%2F" in url
Expand All @@ -79,7 +79,9 @@ def test_url__installed_app(self):
assert "state=dummy+state" in url

def test_url__installed_app__implicit(self):
url = installed_app().auth.url(["dummy scope"], "dummy state", implicit=True)
url = installed_app().auth.url(
implicit=True, scopes=["dummy scope"], state="dummy state"
)
assert "client_id=dummy+client" in url
assert "duration=temporary" in url
assert "redirect_uri=https%3A%2F%2Fdummy.tld%2F" in url
Expand All @@ -88,7 +90,7 @@ def test_url__installed_app__implicit(self):
assert "state=dummy+state" in url

def test_url__web_app(self):
url = web_app().auth.url(["dummy scope"], "dummy state")
url = web_app().auth.url(scopes=["dummy scope"], state="dummy state")
assert "client_id=dummy+client" in url
assert "secret" not in url
assert "redirect_uri=https%3A%2F%2Fdummy.tld%2F" in url
Expand All @@ -98,11 +100,13 @@ def test_url__web_app(self):

def test_url__web_app__implicit(self):
with pytest.raises(ClientException):
web_app().auth.url(["dummy scope"], "dummy state", implicit=True)
web_app().auth.url(
implicit=True, scopes=["dummy scope"], state="dummy state"
)

def test_url__web_app_without_redirect_uri(self):
reddit = Reddit(
client_id="dummy client", client_secret="dummy secret", user_agent="dummy"
)
with pytest.raises(ClientException):
reddit.auth.url(["dummy scope"], "dummy state")
reddit.auth.url(scopes=["dummy scope"], state="dummy state")
2 changes: 1 addition & 1 deletion tools/generate_award_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_request_params(client_id, redirect_uri, thing):
user_agent="Award fetcher by u/Lil_SpazJoekp",
)
state = str(random.randint(0, 65000))
url = reddit.auth.url(scopes, state, "temporary")
url = reddit.auth.url(duration="temporary", scopes=scopes, state=state)
print(f"Open this url in your browser: {url}")
sys.stdout.flush()

Expand Down

0 comments on commit d9799f8

Please sign in to comment.