-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
126 lines (105 loc) · 4.12 KB
/
__init__.py
File metadata and controls
126 lines (105 loc) · 4.12 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
120
121
122
123
124
125
126
import logging
import json
log = logging.getLogger("socketdev")
class ApiTokens:
def __init__(self, api):
self.api = api
def create(self, org_slug: str, **kwargs) -> dict:
"""
Create a new API token.
Args:
org_slug: Organization slug
**kwargs: Token configuration parameters
Returns:
dict: API response containing the created token details
"""
path = f"orgs/{org_slug}/api-tokens"
payload = json.dumps(kwargs) if kwargs else "{}"
response = self.api.do_request(path=path, method="POST", payload=payload)
if response.status_code == 201:
return response.json()
log.error(f"Error creating API token: {response.status_code}")
log.error(response.text)
return {}
def list(self, org_slug: str, **kwargs) -> dict:
"""
List API tokens for an organization.
Args:
org_slug: Organization slug
**kwargs: Query parameters
Returns:
dict: API response containing list of tokens
"""
path = f"orgs/{org_slug}/api-tokens"
query_params = {}
if kwargs:
query_params.update(kwargs)
if query_params:
from urllib.parse import urlencode
path += "?" + urlencode(query_params)
response = self.api.do_request(path=path, method="GET")
if response.status_code == 200:
return response.json()
log.error(f"Error listing API tokens: {response.status_code}")
log.error(response.text)
return {}
def update(self, org_slug: str, token_id: str = None, **kwargs) -> dict:
"""
Update an API token.
Args:
org_slug: Organization slug
token_id: Token ID to update (optional, can be in kwargs)
**kwargs: Token update parameters
Returns:
dict: API response containing the updated token details
"""
# Extract token_id from kwargs if not provided as parameter
if token_id is None and 'token_id' in kwargs:
token_id = kwargs.pop('token_id')
if token_id:
path = f"orgs/{org_slug}/api-tokens/{token_id}"
method = "PUT"
else:
path = f"orgs/{org_slug}/api-tokens/update"
method = "POST"
payload = json.dumps(kwargs) if kwargs else "{}"
response = self.api.do_request(path=path, method=method, payload=payload)
if response.status_code == 200:
return response.json()
log.error(f"Error updating API token: {response.status_code}")
log.error(response.text)
return {}
def rotate(self, org_slug: str, **kwargs) -> dict:
"""
Rotate an API token.
Args:
org_slug: Organization slug
**kwargs: Token rotation parameters
Returns:
dict: API response containing the rotated token details
"""
path = f"orgs/{org_slug}/api-tokens/rotate"
payload = json.dumps(kwargs) if kwargs else "{}"
response = self.api.do_request(path=path, method="POST", payload=payload)
if response.status_code == 200:
return response.json()
log.error(f"Error rotating API token: {response.status_code}")
log.error(response.text)
return {}
def revoke(self, org_slug: str, **kwargs) -> dict:
"""
Revoke an API token.
Args:
org_slug: Organization slug
**kwargs: Token revocation parameters
Returns:
dict: API response confirming token revocation
"""
path = f"orgs/{org_slug}/api-tokens/revoke"
payload = json.dumps(kwargs) if kwargs else "{}"
response = self.api.do_request(path=path, method="POST", payload=payload)
if response.status_code == 200:
return response.json()
log.error(f"Error revoking API token: {response.status_code}")
log.error(response.text)
return {}