Skip to content

Commit

Permalink
Convert BaseAuthenticator, BaseAuthorizer, and RetryStrategy to abstr…
Browse files Browse the repository at this point in the history
…act classes

(cherry picked from commit praw-dev/prawcore@73dd4c9)
  • Loading branch information
LilSpazJoekp committed Aug 6, 2023
1 parent 79b3d06 commit 2f6cb42
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
9 changes: 7 additions & 2 deletions asyncprawcore/auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Provides Authentication and Authorization classes."""
import inspect
import time
from abc import ABC, abstractmethod
from typing import (
TYPE_CHECKING,
Awaitable,
Expand All @@ -25,7 +26,7 @@
from asyncprawcore.requestor import Requestor


class BaseAuthenticator(object):
class BaseAuthenticator(ABC):
"""Provide the base authenticator object that stores OAuth2 credentials."""

def __init__(
Expand All @@ -46,6 +47,10 @@ def __init__(
self.client_id = client_id
self.redirect_uri = redirect_uri

@abstractmethod
def _auth(self):
pass

async def _post(
self, url: str, success_status: int = codes["ok"], **data
) -> ClientResponse:
Expand Down Expand Up @@ -164,7 +169,7 @@ def _auth(self) -> BasicAuth:
return BasicAuth(self.client_id, "")


class BaseAuthorizer(object):
class BaseAuthorizer(ABC):
"""Superclass for OAuth2 authorization tokens and scopes."""

AUTHENTICATOR_CLASS: Union[Tuple, Type] = BaseAuthenticator
Expand Down
6 changes: 5 additions & 1 deletion asyncprawcore/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import logging
import random
from abc import ABC, abstractmethod
from copy import deepcopy
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
from urllib.parse import urljoin
Expand Down Expand Up @@ -41,14 +42,17 @@
log = logging.getLogger(__package__)


class RetryStrategy(object):
class RetryStrategy(ABC):
"""An abstract class for scheduling request retries.
The strategy controls both the number and frequency of retry attempts.
Instances of this class are immutable.
"""
@abstractmethod
def _sleep_seconds(self) -> Optional[float]:
pass

async def sleep(self) -> None:
"""Sleep until we are ready to attempt the request."""
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/test_authorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ async def setUp(self):
)


class InvalidAuthenticator(asyncprawcore.auth.BaseAuthenticator):
def _auth(self):
pass


class TestAuthorizer(AuthorizerBase):
async def test_authorize__fail_without_redirect_uri(self):
authorizer = asyncprawcore.Authorizer(self.authentication)
Expand Down Expand Up @@ -79,10 +84,8 @@ def test_initialize(self):
assert authorizer.scopes is None
assert not authorizer.is_valid()

def test_initialize__with_base_authenticator(self):
authenticator = asyncprawcore.Authorizer(
asyncprawcore.auth.BaseAuthenticator(None, None, None)
)
def test_initialize__with_invalid_authenticator(self):
authenticator = asyncprawcore.Authorizer(InvalidAuthenticator(None, None, None))
with pytest.raises(asyncprawcore.InvalidInvocation):
asyncprawcore.DeviceIDAuthorizer(
authenticator,
Expand Down

0 comments on commit 2f6cb42

Please sign in to comment.