Skip to content

Commit c62262b

Browse files
authored
Modifies BaseTransferService to have OAuth2 session as an attribute rather than extending it (#24)
While working on #5 , I realized that extending [`OAuth2Session`](https://github.com/requests/requests-oauthlib/blob/master/requests_oauthlib/oauth2_session.py) was a bit overkill since we don't need the level of configurability it provides (e.g., [`fetch_token`](https://github.com/requests/requests-oauthlib/blob/master/requests_oauthlib/oauth2_session.py#L202)'s 16 arguments, of which we'll only ever use two). IIRC, it's usually better not to use inheritance in cases like these anyway (composition over inheritance?).
1 parent b4b6e9e commit c62262b

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/pardner/services/base.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from abc import ABC, abstractmethod
22
from typing import Iterable
33

4-
from oauthlib.oauth2 import BackendApplicationClient
54
from requests_oauthlib import OAuth2Session
65

76
from pardner.verticals.base import Vertical
@@ -27,18 +26,14 @@ def __init__(
2726
)
2827

2928

30-
class BaseTransferService(OAuth2Session, ABC):
29+
class BaseTransferService(ABC):
3130
"""
3231
A base class to be extended by service-specific classes that implement logic for
3332
OAuth 2.0 and data transfers.
34-
35-
:param client_id: Client identifier given by the OAuth provider upon registration.
36-
:param client_secret: The `client_secret` paired to the `client_id`.
37-
:param redirect_url: The registered callback URI.
3833
"""
3934

4035
_client_secret: str
41-
_scope: set[str] = set()
36+
_oAuth2Session: OAuth2Session
4237
_service_name: str
4338
_supported_verticals: set[Vertical] = set()
4439
_verticals: set[Vertical] = set()
@@ -52,11 +47,20 @@ def __init__(
5247
supported_verticals: set[Vertical],
5348
verticals: set[Vertical] = set(),
5449
) -> None:
55-
background_application_client = BackendApplicationClient(client_id)
56-
super().__init__(
57-
client_id=client_id,
58-
client=background_application_client,
59-
redirect_uri=redirect_uri,
50+
"""
51+
Initializes an instance of BaseTransferService, which shouldn't be done unless
52+
by classes that extend it.
53+
54+
:param service_name: Name of the service for which the transfer is being built.
55+
:param client_id: Client identifier given by the OAuth provider upon registration.
56+
:param client_secret: The `client_secret` paired to the `client_id`.
57+
:param redirect_uri: The registered callback URI.
58+
:param supported_verticals: The `Vertical`s that can be fetched on the service.
59+
:param verticals: The `Vertical`s for which the transfer service has
60+
appropriate scope to fetch.
61+
"""
62+
self._oAuth2Session = OAuth2Session(
63+
client_id=client_id, redirect_uri=redirect_uri
6064
)
6165
self._client_secret = client_secret
6266
self._supported_verticals = supported_verticals
@@ -67,6 +71,14 @@ def __init__(
6771
def name(self) -> str:
6872
return self._service_name
6973

74+
@property
75+
def scope(self) -> set[str]:
76+
return self._oAuth2Session.scope if self._oAuth2Session.scope else set()
77+
78+
@scope.setter
79+
def scope(self, new_scope: Iterable[str]) -> None:
80+
self._oAuth2Session.scope = set(new_scope)
81+
7082
@property
7183
def verticals(self) -> set[Vertical]:
7284
return self._verticals
@@ -110,7 +122,7 @@ def add_verticals(
110122
raise InsufficientScopeException(verticals, self.name)
111123
elif not new_scopes.issubset(original_scopes):
112124
self.verticals = new_verticals | self.verticals
113-
del self.access_token
125+
del self._oAuth2Session.access_token
114126
self.scope = original_scopes | new_scopes
115127
return False
116128

tests/test_transfer_services/test_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ def _mock_scope_for_verticals(verticals):
7474
return {'new_scope'}
7575
return sample_scope
7676

77-
transfer_service.access_token = 'access_token'
77+
transfer_service._oAuth2Session.access_token = 'access_token'
7878
monkeypatch.setattr(
7979
transfer_service, 'scope_for_verticals', _mock_scope_for_verticals
8080
)
8181
assert not transfer_service.add_verticals(
8282
[Vertical.NEW_VERTICAL_EXTRA_SCOPE], should_reauth=True
8383
)
84-
assert not transfer_service.access_token
84+
assert not transfer_service._oAuth2Session.access_token
8585
assert transfer_service.scope == {'fake', 'scope', 'new_scope'}
8686
assert transfer_service.verticals == {
8787
Vertical.FAKE_VERTICAL,

0 commit comments

Comments
 (0)