Skip to content

Commit a586a7b

Browse files
authored
Uses subclasses of BaseVertical rather than the Vertical enum to identify a vertical (#66)
Closes #61 So any time we were using `Vertical.SubVerticalName` we now use `SubVerticalName`, where `SubVerticalName` is a subclass of `BaseVertical`. Note that I do mean subclass and not instance of a subclass since we're using the classes themselves as identifiers of verticals. In the future, methods will be returning instances of those same subclasses. I also adjusted the names of the vertical fetching methods to follow the structure: `fetch_<vertical_name>_vertical` for consistency and to avoid having to keep track of the plural names like before. ### Misc Anywhere where an argument `arg` to a function/method could have one or more items, like in the constructor of the exception classes, I make these into a `*arg` instead so we don't have to special case passing in different number of arguments.
1 parent ad107aa commit a586a7b

File tree

15 files changed

+240
-102
lines changed

15 files changed

+240
-102
lines changed

src/pardner/exceptions.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33

44
class InsufficientScopeException(Exception):
55
def __init__(self, *unsupported_verticals: Vertical, service_name: str) -> None:
6-
combined_verticals = ', '.join(unsupported_verticals)
6+
combined_verticals = ', '.join(
7+
[str(vertical) for vertical in unsupported_verticals]
8+
)
79
super().__init__(
810
f'Cannot add {combined_verticals} to {service_name} with current scope.'
911
)
1012

1113

1214
class UnsupportedVerticalException(Exception):
1315
def __init__(self, *unsupported_verticals: Vertical, service_name: str) -> None:
14-
combined_verticals = ', '.join(unsupported_verticals)
16+
combined_verticals = ', '.join(
17+
[str(vertical) for vertical in unsupported_verticals]
18+
)
1519
is_more_than_one_vertical = len(unsupported_verticals) > 1
1620
super().__init__(
1721
f'Cannot fetch {combined_verticals} from {service_name} because '

src/pardner/services/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,5 +263,5 @@ def fetch(
263263
vertical, service_name=self._service_name
264264
)
265265

266-
method_name = f'fetch_{vertical.plural}'
266+
method_name = f'fetch_{vertical.vertical_name}_vertical'
267267
getattr(self, method_name)(request_params=request_params, **params)

src/pardner/services/groupme.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88

99
from pardner.exceptions import UnsupportedRequestException
1010
from pardner.services import BaseTransferService
11-
from pardner.verticals import Vertical
11+
from pardner.verticals import (
12+
BlockedUserVertical,
13+
ChatBotVertical,
14+
ConversationDirectVertical,
15+
ConversationGroupVertical,
16+
Vertical,
17+
)
1218

1319

1420
class GroupMeTransferService(BaseTransferService):
@@ -35,10 +41,10 @@ def __init__(
3541
client_id=client_id,
3642
redirect_uri=redirect_uri,
3743
supported_verticals={
38-
Vertical.BlockedUser,
39-
Vertical.ChatBot,
40-
Vertical.ConversationDirect,
41-
Vertical.ConversationGroup,
44+
BlockedUserVertical,
45+
ChatBotVertical,
46+
ConversationDirectVertical,
47+
ConversationGroupVertical,
4248
},
4349
verticals=verticals,
4450
)
@@ -129,7 +135,7 @@ def fetch_user_data(self, request_params: dict[str, Any] = {}) -> Any:
129135
self._user_id = user_data['id']
130136
return user_data
131137

132-
def fetch_blocked_users(self, request_params: dict[str, Any] = {}) -> Any:
138+
def fetch_blocked_user_vertical(self, request_params: dict[str, Any] = {}) -> Any:
133139
"""
134140
Sends a GET request to fetch the users blocked by the authenticated user.
135141
@@ -144,15 +150,15 @@ def fetch_blocked_users(self, request_params: dict[str, Any] = {}) -> Any:
144150

145151
return blocked_users['blocks']
146152

147-
def fetch_chat_bots(self, request_params: dict[str, Any] = {}) -> Any:
153+
def fetch_chat_bot_vertical(self, request_params: dict[str, Any] = {}) -> Any:
148154
"""
149155
Sends a GET request to fetch the chat bots created by the authenticated user.
150156
151157
:returns: a JSON object with the result of the request.
152158
"""
153159
return self._fetch_resource_common('bots', request_params)
154160

155-
def fetch_conversations_direct(
161+
def fetch_conversation_direct_vertical(
156162
self, request_params: dict[str, Any] = {}, count: int = 10
157163
) -> Any:
158164
"""
@@ -174,7 +180,7 @@ def fetch_conversations_direct(
174180
'can only make a request for at most 10 direct conversations at a time.',
175181
)
176182

177-
def fetch_conversations_group(
183+
def fetch_conversation_group_vertical(
178184
self, request_params: dict[str, Any] = {}, count: int = 10
179185
) -> Any:
180186
"""

src/pardner/services/strava.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pardner.exceptions import UnsupportedRequestException, UnsupportedVerticalException
44
from pardner.services import BaseTransferService
55
from pardner.services.utils import scope_as_set, scope_as_string
6-
from pardner.verticals import Vertical
6+
from pardner.verticals import PhysicalActivityVertical, Vertical
77

88

99
class StravaTransferService(BaseTransferService):
@@ -31,7 +31,7 @@ def __init__(
3131
client_secret=client_secret,
3232
redirect_uri=redirect_uri,
3333
state=state,
34-
supported_verticals={Vertical.PhysicalActivity},
34+
supported_verticals={PhysicalActivityVertical},
3535
verticals=verticals,
3636
)
3737

@@ -60,11 +60,11 @@ def scope_for_verticals(self, verticals: Iterable[Vertical]) -> set[str]:
6060
raise UnsupportedVerticalException(
6161
vertical, service_name=self._service_name
6262
)
63-
if vertical == Vertical.PhysicalActivity:
63+
if vertical == PhysicalActivityVertical:
6464
sub_scopes.update(['activity:read', 'profile:read_all'])
6565
return sub_scopes
6666

67-
def fetch_physical_activities(
67+
def fetch_physical_activity_vertical(
6868
self, request_params: dict[str, Any] = {}, count: int = 30
6969
) -> list[Any]:
7070
"""

src/pardner/services/tumblr.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pardner.exceptions import UnsupportedRequestException
44
from pardner.services import BaseTransferService
5-
from pardner.verticals import Vertical
5+
from pardner.verticals import SocialPostingVertical, Vertical
66

77

88
class TumblrTransferService(BaseTransferService):
@@ -30,7 +30,7 @@ def __init__(
3030
client_secret=client_secret,
3131
redirect_uri=redirect_uri,
3232
state=state,
33-
supported_verticals={Vertical.FeedPost},
33+
supported_verticals={SocialPostingVertical},
3434
verticals=verticals,
3535
)
3636

@@ -48,7 +48,7 @@ def fetch_token(
4848
) -> dict[str, Any]:
4949
return super().fetch_token(code, authorization_response, include_client_id)
5050

51-
def fetch_feed_posts(
51+
def fetch_social_posting_vertical(
5252
self,
5353
request_params: dict[str, Any] = {},
5454
count: int = 20,

src/pardner/verticals/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
from pardner.verticals.blocked_user import BlockedUserVertical as BlockedUserVertical
55
from pardner.verticals.chat_bot import ChatBotVertical as ChatBotVertical
66
from pardner.verticals.conversation import ConversationVertical as ConversationVertical
7+
from pardner.verticals.conversation_direct import (
8+
ConversationDirectVertical as ConversationDirectVertical,
9+
)
10+
from pardner.verticals.conversation_group import (
11+
ConversationGroupVertical as ConversationGroupVertical,
12+
)
713
from pardner.verticals.message import MessageVertical as MessageVertical
814
from pardner.verticals.physical_activity import (
915
PhysicalActivityVertical as PhysicalActivityVertical,

src/pardner/verticals/base.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
11
from abc import ABC
22
from datetime import datetime
3-
from enum import StrEnum
4-
from typing import Optional
3+
from typing import Type
54

65
from pydantic import AnyHttpUrl, BaseModel, Field
76

87

9-
class Vertical(StrEnum):
10-
"""
11-
Represents the verticals, or categories of data, that are supported by this library.
12-
Not all verticals are supported by every transfer service.
13-
"""
14-
15-
BlockedUser = 'blocked_user'
16-
ChatBot = 'chat_bot'
17-
ConversationDirect = 'conversation_direct', 'conversations_direct'
18-
ConversationGroup = 'conversation_group', 'conversations_group'
19-
ConversationMessage = 'conversation_message'
20-
FeedPost = 'feed_post'
21-
PhysicalActivity = 'physical_activity', 'physical_activities'
22-
23-
plural: str
24-
25-
def __new__(cls, singular: str, plural: Optional[str] = None) -> 'Vertical':
26-
vertical_obj = str.__new__(cls, singular)
27-
vertical_obj._value_ = singular
28-
vertical_obj.plural = plural if plural else f'{singular}s'
29-
return vertical_obj
30-
31-
328
class BaseVertical(BaseModel, ABC):
339
"""
3410
Base class for all verticals, except sub-verticals. Represents the verticals, or
@@ -45,3 +21,9 @@ class BaseVertical(BaseModel, ABC):
4521

4622
created_at: datetime | None = None
4723
url: AnyHttpUrl | None = None
24+
25+
def __str__(self):
26+
return self.vertical_name
27+
28+
29+
Vertical = Type[BaseVertical]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pardner.verticals.conversation import ConversationVertical
2+
3+
4+
class ConversationDirectVertical(ConversationVertical):
5+
"""
6+
The metadata related to a conversation where messages are exchanged between one
7+
or more people.
8+
"""
9+
10+
vertical_name: str = 'conversation_direct'
11+
is_group_conversation: bool = False
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pardner.verticals.conversation import ConversationVertical
2+
3+
4+
class ConversationGroupVertical(ConversationVertical):
5+
"""
6+
The metadata related to a conversation where messages are exchanged between one
7+
or more people.
8+
"""
9+
10+
vertical_name: str = 'conversation_group'
11+
is_group_conversation: bool = True

tests/test_transfer_services/conftest.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@
77
StravaTransferService,
88
TumblrTransferService,
99
)
10-
from pardner.verticals import Vertical
10+
from pardner.verticals import (
11+
BlockedUserVertical,
12+
ConversationDirectVertical,
13+
PhysicalActivityVertical,
14+
SocialPostingVertical,
15+
)
16+
from pardner.verticals.base import BaseVertical
17+
18+
# CLASSES
19+
20+
21+
class NewVertical(BaseVertical):
22+
vertical_name: str = 'new_vertical'
23+
24+
25+
class ExtraScopeVertical(BaseVertical):
26+
vertical_name: str = 'extra_scope_vertical'
27+
1128

1229
# FIXTURES
1330

@@ -37,28 +54,22 @@ def mock_oauth2_session_response(mocker):
3754

3855

3956
@pytest.fixture
40-
def mock_vertical():
41-
Vertical.NEW_VERTICAL = 'new_vertical'
42-
Vertical.NEW_VERTICAL_EXTRA_SCOPE = 'new_vertical_unsupported'
43-
44-
45-
@pytest.fixture
46-
def mock_tumblr_transfer_service(verticals=[Vertical.FeedPost]):
57+
def mock_tumblr_transfer_service(verticals=[SocialPostingVertical]):
4758
return TumblrTransferService(
4859
'fake_client_id', 'fake_client_secret', 'https://redirect_uri', None, verticals
4960
)
5061

5162

5263
@pytest.fixture
53-
def mock_strava_transfer_service(verticals=[Vertical.PhysicalActivity]):
64+
def mock_strava_transfer_service(verticals=[PhysicalActivityVertical]):
5465
return StravaTransferService(
5566
'fake_client_id', 'fake_client_secret', 'https://redirect_uri', None, verticals
5667
)
5768

5869

5970
@pytest.fixture
6071
def mock_groupme_transfer_service(
61-
verticals=[Vertical.BlockedUser, Vertical.ConversationDirect],
72+
verticals=[BlockedUserVertical, ConversationDirectVertical],
6273
):
6374
groupme = GroupMeTransferService(
6475
client_id='fake_client_id',

0 commit comments

Comments
 (0)