Skip to content

Commit 80bb604

Browse files
committed
MT-23076: show api token expiration in examples
1 parent 208ab30 commit 80bb604

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

examples/general/api_tokens.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ def get_api_token(account_id: int, api_token_id: int) -> ApiToken:
2020

2121
def create_api_token(account_id: int) -> ApiTokenWithToken:
2222
# The full token value is only returned once on the response — store it securely.
23+
# Omit expires_at for the server default expiration, pass an ISO 8601
24+
# date-time for an explicit expiry, or pass expires_at=None for a token
25+
# that never expires.
2326
return api_tokens_api.create(
2427
account_id=account_id,
2528
token_params=mt.CreateApiTokenParams(
2629
name="My API Token",
30+
expires_at="2027-06-01T00:00:00Z",
2731
resources=[
2832
mt.ApiTokenResource(
2933
resource_type="account",
@@ -37,9 +41,22 @@ def create_api_token(account_id: int) -> ApiTokenWithToken:
3741

3842
def reset_api_token(account_id: int, api_token_id: int) -> ApiTokenWithToken:
3943
# The reset response includes the new full token value once — store it securely.
44+
# Omit token_params for the server default expiration of the new token.
4045
return api_tokens_api.reset(account_id=account_id, api_token_id=api_token_id)
4146

4247

48+
def reset_api_token_with_expiration(
49+
account_id: int, api_token_id: int
50+
) -> ApiTokenWithToken:
51+
# Pass an ISO 8601 date-time for an explicit expiry of the new token,
52+
# or expires_at=None for a token that never expires.
53+
return api_tokens_api.reset(
54+
account_id=account_id,
55+
api_token_id=api_token_id,
56+
token_params=mt.ResetApiTokenParams(expires_at="2027-06-01T00:00:00Z"),
57+
)
58+
59+
4360
def delete_api_token(account_id: int, api_token_id: int) -> DeletedObject:
4461
return api_tokens_api.delete(account_id=account_id, api_token_id=api_token_id)
4562

@@ -57,5 +74,8 @@ def delete_api_token(account_id: int, api_token_id: int) -> DeletedObject:
5774
reset = reset_api_token(ACCOUNT_ID, created.id)
5875
print(reset)
5976

60-
deleted = delete_api_token(ACCOUNT_ID, reset.id)
77+
reset_with_expiration = reset_api_token_with_expiration(ACCOUNT_ID, reset.id)
78+
print(reset_with_expiration)
79+
80+
deleted = delete_api_token(ACCOUNT_ID, reset_with_expiration.id)
6181
print(deleted)

0 commit comments

Comments
 (0)