-
Notifications
You must be signed in to change notification settings - Fork 2
Creates Strava transfer service class and adds configuration to base transfer service class #39
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from typing import Any, Iterable, Optional, override | ||
|
|
||
| from pardner.services.base import BaseTransferService, UnsupportedVerticalException | ||
| from pardner.services.utils import scope_as_set, scope_as_string | ||
| from pardner.verticals import Vertical | ||
|
|
||
|
|
||
| class StravaTransferService(BaseTransferService): | ||
| """ | ||
| Class responsible for obtaining end-user authorization to make requests to | ||
| Strava's API. | ||
| See API documentation: https://developers.strava.com/docs/reference/ | ||
| """ | ||
|
|
||
| _authorization_url = 'https://www.strava.com/oauth/authorize' | ||
| _token_url = 'https://www.strava.com/oauth/token' | ||
|
|
||
| def __init__( | ||
| self, | ||
| client_id: str, | ||
| client_secret: str, | ||
| redirect_uri: str, | ||
| state: Optional[str] = None, | ||
| verticals: set[Vertical] = set(), | ||
| ) -> None: | ||
| super().__init__( | ||
| service_name='Strava', | ||
| client_id=client_id, | ||
| client_secret=client_secret, | ||
| redirect_uri=redirect_uri, | ||
| state=state, | ||
| supported_verticals={Vertical.FeedPost}, | ||
| verticals=verticals, | ||
| ) | ||
|
|
||
| @property | ||
| def scope(self) -> set[str]: | ||
| return scope_as_set(self._oAuth2Session.scope, delimiter=',') | ||
|
|
||
| @scope.setter | ||
| def scope(self, new_scope: Iterable[str] | str) -> None: | ||
| self._oAuth2Session.scope = scope_as_string(new_scope, delimiter=',') | ||
|
|
||
| @override | ||
| def fetch_token( | ||
| self, | ||
| code: Optional[str] = None, | ||
| authorization_response: Optional[str] = None, | ||
| include_client_id: bool = True, | ||
| ) -> dict[str, Any]: | ||
| return super().fetch_token(code, authorization_response, include_client_id) | ||
|
|
||
| @override | ||
| def scope_for_verticals(self, verticals: Iterable[Vertical]) -> set[str]: | ||
| sub_scopes: set[str] = set() | ||
| for vertical in verticals: | ||
| if vertical not in self._supported_verticals: | ||
| raise UnsupportedVerticalException([vertical], self._service_name) | ||
| if vertical == Vertical.FeedPost: | ||
| sub_scopes.update(['activity:read', 'profile:read_all']) | ||
| return sub_scopes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from typing import Any | ||
|
|
||
|
|
||
| def scope_as_string(scopes: Any, delimiter: str = ' ') -> str | None: | ||
| """ | ||
| Converts a sequence of individual scopes into a single scope string. | ||
|
|
||
| :param scopes: a sequence of scopes as strings or a scope string. | ||
| :param delimiter: the string used to separate individual scopes. Defaults to single space. | ||
|
|
||
| :returns: a string containing all scopes. | ||
| :raises :class:ValueError: if `scopes` is neither a string nor a sequence of strings | ||
| """ | ||
| if isinstance(scopes, str) or scopes is None: | ||
| return scopes | ||
| elif isinstance(scopes, (set, tuple, list)): | ||
| return delimiter.join([str(s) for s in sorted(scopes)]) | ||
| raise ValueError(f'Invalid scope ({scopes}), must be string, tuple, set, or list.') | ||
|
|
||
|
|
||
| def scope_as_set(scope: Any, delimiter: str = ' ') -> set[str]: | ||
| """ | ||
| Splits a scope with potentially more than one scope into a set of scopes. | ||
|
|
||
| :param scope: a string with one or more scopes. | ||
| :param delimiter: the string used to separate individual scopes. Defaults to single space. | ||
|
|
||
| :returns: a set of scopes. | ||
| """ | ||
| if isinstance(scope, (tuple, list, set)): | ||
| return {str(s) for s in scope} | ||
| elif scope is None: | ||
| return set() | ||
| return set(scope.strip().split(delimiter)) |
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import pytest | ||
|
|
||
| from pardner.services.strava import StravaTransferService | ||
| from pardner.services.tumblr import TumblrTransferService | ||
| from pardner.verticals.base import Vertical | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_oAuth2Session(mocker): | ||
| mock_oauth2session_request = mocker.patch('requests_oauthlib.OAuth2Session.request') | ||
| mock_client_parse_request_body_response = mocker.patch( | ||
| 'oauthlib.oauth2.rfc6749.clients.WebApplicationClient.parse_request_body_response' | ||
| ) | ||
| return [mock_oauth2session_request, mock_client_parse_request_body_response] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_vertical(): | ||
| Vertical.NEW_VERTICAL = 'new_vertical' | ||
| Vertical.NEW_VERTICAL_EXTRA_SCOPE = 'new_vertical_unsupported' | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_tumblr_transfer_service(verticals=[Vertical.FeedPost]): | ||
| return TumblrTransferService( | ||
| 'fake_client_id', 'fake_client_secret', 'https://redirect_uri', None, verticals | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_strava_transfer_service(verticals=[Vertical.FeedPost]): | ||
| return StravaTransferService( | ||
| 'fake_client_id', 'fake_client_secret', 'https://redirect_uri', None, verticals | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's set as this by default