-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapi_tokens.py
More file actions
61 lines (43 loc) · 1.86 KB
/
api_tokens.py
File metadata and controls
61 lines (43 loc) · 1.86 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
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_TOKEN = "YOUR_API_TOKEN"
ACCOUNT_ID = "YOUR_ACCOUNT_ID"
client = mt.MailtrapClient(token=API_TOKEN)
api_tokens_api = client.general_api.api_tokens
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.
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 reset_api_token(account_id: int, api_token_id: int) -> ApiTokenWithToken:
# The reset response includes the new full token value once — store it securely.
return api_tokens_api.reset(account_id=account_id, api_token_id=api_token_id)
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)
fetched = get_api_token(ACCOUNT_ID, created.id)
print(fetched)
reset = reset_api_token(ACCOUNT_ID, created.id)
print(reset)
deleted = delete_api_token(ACCOUNT_ID, reset.id)
print(deleted)