-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Add dedicated API Keys service (#297)
- Add dedicated API Keys service - Migrate API key-related functions to API Keys service, deprecated in User service - Migrate unit tests, re-record cassettes as needed
- Loading branch information
Showing
14 changed files
with
249 additions
and
111 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,45 @@ | ||
from typing import ( | ||
Any, | ||
Dict, | ||
List, | ||
) | ||
|
||
from easypost.constant import NO_USER_FOUND | ||
from easypost.easypost_object import convert_to_easypost_object | ||
from easypost.errors import FilteringError | ||
from easypost.models import ApiKey | ||
from easypost.requestor import ( | ||
RequestMethod, | ||
Requestor, | ||
) | ||
from easypost.services.base_service import BaseService | ||
|
||
|
||
class ApiKeyService(BaseService): | ||
def __init__(self, client): | ||
self._client = client | ||
self._model_class = ApiKey.__name__ | ||
|
||
def all(self) -> Dict[str, Any]: | ||
"""Retrieve a list of all API keys.""" | ||
url = "/api_keys" | ||
|
||
response = Requestor(self._client).request(method=RequestMethod.GET, url=url) | ||
|
||
return convert_to_easypost_object(response=response) | ||
|
||
def retrieve_api_keys_for_user(self, id: str) -> List[ApiKey]: | ||
"""Retrieve a list of API keys (works for the authenticated User or a child User).""" | ||
api_keys = self.all() | ||
|
||
if api_keys["id"] == id: | ||
# This function was called on the authenticated user | ||
return api_keys["keys"] | ||
|
||
# This function was called on a child user (authenticated as parent, only return | ||
# this child user's details). | ||
for child in api_keys["children"]: | ||
if child.id == id: | ||
return child.keys | ||
|
||
raise FilteringError(message=NO_USER_FOUND) |
This file contains 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
14 changes: 8 additions & 6 deletions
14
tests/cassettes/test_user_all_api_keys.yaml → tests/cassettes/test_all_api_keys.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.