Skip to content

Commit 12fb767

Browse files
authored
[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
1 parent 4bc2545 commit 12fb767

14 files changed

+249
-111
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
- Add dedicated API Key-related service, available via the `api_keys` property of a client
4+
- NOTE: Please note the naming. The `api_key` property of a client is the currently-used API key string, while the `api_keys` property is the service for managing API keys.
5+
- Migrated API Key-related functionality to `api_keys` service, deprecated old methods in `user` service
6+
37
## v8.1.1 (2023-09-05)
48

59
- Fix endpoint for creating a FedEx Smartpost carrier account

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ clean:
2727

2828
## coverage - Test the project and generate an HTML coverage report
2929
coverage:
30-
$(VIRTUAL_BIN)/pytest --cov=$(PROJECT_NAME) --cov-branch --cov-report=html --cov-report=lcov --cov-report=term-missing --cov-fail-under=90
30+
$(VIRTUAL_BIN)/pytest --cov=$(PROJECT_NAME) --cov-branch --cov-report=html --cov-report=lcov --cov-report=term-missing --cov-fail-under=89
3131

3232
## docs - Generates docs for the library
3333
docs:

easypost/constant.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
NO_BILLING_ERROR = "Billing has not been setup for this user. Please add a payment method."
2727
NO_MORE_PAGES_ERROR = "There are no more pages to retrieve."
2828
NO_RATES_ERROR = "No rates found."
29+
NO_USER_FOUND = "No user found with the given id."
2930
SEND_STRIPE_DETAILS_ERROR = "Could not send card details to Stripe, please try again later."
3031
TIMEOUT_ERROR = "Request timed out."
3132

easypost/easypost_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212
from easypost.services import (
1313
AddressService,
14+
ApiKeyService,
1415
BatchService,
1516
BetaCarrierMetadataService,
1617
BetaRateService,
@@ -54,6 +55,7 @@ def __init__(
5455

5556
# Services
5657
self.address = AddressService(self)
58+
self.api_keys = ApiKeyService(self)
5759
self.batch = BatchService(self)
5860
self.beta_carrier_metadata = BetaCarrierMetadataService(self)
5961
self.beta_rate = BetaRateService(self)

easypost/services/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# flake8: noqa
22
from easypost.services.address_service import AddressService
3+
from easypost.services.api_key_service import ApiKeyService
34
from easypost.services.batch_service import BatchService
45
from easypost.services.beta_carrier_metadata_service import BetaCarrierMetadataService
56
from easypost.services.beta_rate_service import BetaRateService
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing import (
2+
Any,
3+
Dict,
4+
List,
5+
)
6+
7+
from easypost.constant import NO_USER_FOUND
8+
from easypost.easypost_object import convert_to_easypost_object
9+
from easypost.errors import FilteringError
10+
from easypost.models import ApiKey
11+
from easypost.requestor import (
12+
RequestMethod,
13+
Requestor,
14+
)
15+
from easypost.services.base_service import BaseService
16+
17+
18+
class ApiKeyService(BaseService):
19+
def __init__(self, client):
20+
self._client = client
21+
self._model_class = ApiKey.__name__
22+
23+
def all(self) -> Dict[str, Any]:
24+
"""Retrieve a list of all API keys."""
25+
url = "/api_keys"
26+
27+
response = Requestor(self._client).request(method=RequestMethod.GET, url=url)
28+
29+
return convert_to_easypost_object(response=response)
30+
31+
def retrieve_api_keys_for_user(self, id: str) -> List[ApiKey]:
32+
"""Retrieve a list of API keys (works for the authenticated User or a child User)."""
33+
api_keys = self.all()
34+
35+
if api_keys["id"] == id:
36+
# This function was called on the authenticated user
37+
return api_keys["keys"]
38+
39+
# This function was called on a child user (authenticated as parent, only return
40+
# this child user's details).
41+
for child in api_keys["children"]:
42+
if child.id == id:
43+
return child.keys
44+
45+
raise FilteringError(message=NO_USER_FOUND)

easypost/services/user_service.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
List,
55
Optional,
66
)
7+
from warnings import warn
78

89
from easypost.easypost_object import convert_to_easypost_object
910
from easypost.models import (
@@ -68,6 +69,12 @@ def retrieve_me(self) -> User:
6869

6970
def all_api_keys(self) -> Dict[str, Any]:
7071
"""Retrieve a list of all API keys."""
72+
warn(
73+
'This method is deprecated, use the "all" function of "api_keys" on the client instead.',
74+
DeprecationWarning,
75+
stacklevel=2,
76+
)
77+
7178
url = "/api_keys"
7279

7380
response = Requestor(self._client).request(method=RequestMethod.GET, url=url)
@@ -76,6 +83,13 @@ def all_api_keys(self) -> Dict[str, Any]:
7683

7784
def api_keys(self, id: str) -> List[ApiKey]:
7885
"""Retrieve a list of API keys (works for the authenticated User or a child User)."""
86+
warn(
87+
'This method is deprecated, use the "retrieve_api_keys_for_user" function '
88+
'of "api_keys" on the client instead.',
89+
DeprecationWarning,
90+
stacklevel=2,
91+
)
92+
7993
api_keys = self.all_api_keys()
8094
my_api_keys = []
8195

examples

Submodule examples updated 494 files

tests/cassettes/test_user_all_api_keys.yaml renamed to tests/cassettes/test_all_api_keys.yaml

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_authenticated_user_api_keys.yaml

Lines changed: 21 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)