Repository navigation
Expand file tree
/
Copy pathapi_tokens.py
More file actions
119 lines (92 loc) · 3.8 KB
/
Copy pathapi_tokens.py
File metadata and controls
119 lines (92 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
from datetime import datetime
from datetime import timedelta
from datetime import timezone
import mailtrap as mt
from mailtrap.models.api_tokens import ApiToken
from mailtrap.models.api_tokens import ApiTokenWithToken
from mailtrap.models.common import DeletedObject
API_KEY = os.environ["MAILTRAP_API_KEY"]
ACCOUNT_ID = os.environ["MAILTRAP_ACCOUNT_ID"]
client = mt.MailtrapClient(token=API_KEY)
api_tokens_api = client.general_api.api_tokens
def one_year_from_now() -> str:
return (datetime.now(timezone.utc) + timedelta(days=365)).strftime(
"%Y-%m-%dT%H:%M:%SZ"
)
def list_api_tokens(account_id: int) -> list[ApiToken]:
return api_tokens_api.get_list(account_id=account_id)
def get_api_token(account_id: int, api_token_id: int) -> ApiToken:
return api_tokens_api.get_by_id(account_id=account_id, api_token_id=api_token_id)
def create_api_token(account_id: int) -> ApiTokenWithToken:
# The full token value is only returned once on the response — store it securely.
# Omit expires_at for the server default expiration.
return api_tokens_api.create(
account_id=account_id,
token_params=mt.CreateApiTokenParams(
name="My API Token",
resources=[
mt.ApiTokenResource(
resource_type="account",
resource_id=account_id,
access_level=100,
)
],
),
)
def create_api_token_with_expiration(account_id: int) -> ApiTokenWithToken:
# Pass an ISO 8601 date-time for an explicit expiry, or expires_at=None
# for a token that never expires.
return api_tokens_api.create(
account_id=account_id,
token_params=mt.CreateApiTokenParams(
name="My API Token With Expiration",
expires_at=one_year_from_now(),
resources=[
mt.ApiTokenResource(
resource_type="account",
resource_id=account_id,
access_level=100,
)
],
),
)
def reset_api_token(account_id: int, api_token_id: int) -> ApiTokenWithToken:
# The reset response includes the new full token value once — store it securely.
# Omit token_params for the server default expiration of the new token.
return api_tokens_api.reset(account_id=account_id, api_token_id=api_token_id)
def reset_api_token_with_expiration(
account_id: int, api_token_id: int
) -> ApiTokenWithToken:
# Pass an ISO 8601 date-time for an explicit expiry of the new token,
# or expires_at=None for a token that never expires.
return api_tokens_api.reset(
account_id=account_id,
api_token_id=api_token_id,
token_params=mt.ResetApiTokenParams(expires_at=one_year_from_now()),
)
def delete_api_token(account_id: int, api_token_id: int) -> DeletedObject:
return api_tokens_api.delete(account_id=account_id, api_token_id=api_token_id)
if __name__ == "__main__":
tokens = list_api_tokens(ACCOUNT_ID)
print(tokens)
created = create_api_token(ACCOUNT_ID)
print(created)
created_with_expiration = create_api_token_with_expiration(ACCOUNT_ID)
print(created_with_expiration)
fetched = get_api_token(ACCOUNT_ID, created.id)
print(fetched)
reset = reset_api_token(ACCOUNT_ID, created.id)
print(reset)
reset_with_expiration = reset_api_token_with_expiration(ACCOUNT_ID, reset.id)
print(reset_with_expiration)
# reset does not replace a token in place: it expires the requested one and
# returns a new token, so every id in the chain still has to be deleted.
for api_token_id in (
created.id,
created_with_expiration.id,
reset.id,
reset_with_expiration.id,
):
deleted = delete_api_token(ACCOUNT_ID, api_token_id)
print(deleted)