This repository was archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworkspaces.py
More file actions
141 lines (119 loc) · 3.55 KB
/
workspaces.py
File metadata and controls
141 lines (119 loc) · 3.55 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from seamapi.types import (
AbstractSeam as Seam,
AbstractWorkspaces,
Workspace,
WorkspaceId,
)
from typing import Optional, List, Union
import requests
from seamapi.utils.convert_to_id import to_workspace_id
class Workspaces(AbstractWorkspaces):
"""
A class used to retreive workspace data
through interaction with Seam API
...
Attributes
----------
seam : Seam
Initial seam class
Methods
-------
list(workspace=None)
Gets a list of workspaces
get(workspace=None)
Gets a workspace
reset_sandbox()
Resets workspace sandbox
"""
seam: Seam
def __init__(self, seam: Seam):
"""
Parameters
----------
seam : Seam
Intial seam class
"""
self.seam = seam
def list(
self,
workspace: Optional[Union[WorkspaceId, Workspace]] = None,
) -> List[Workspace]:
"""Gets a list of workspaces.
Parameters
----------
workspace : WorkspaceId or Workspace, optional
Workspace id or Workspace to get latest version of
Raises
------
Exception
If workspaces weren't found.
Exception
If the API request wasn't successful.
Returns
------
Workspace
"""
workspace_id = None if workspace is None else to_workspace_id(workspace)
res = requests.get(
f"{self.seam.api_url}/workspaces/list",
params={"workspace_id": workspace_id},
headers={"Authorization": f"Bearer {self.seam.api_key}"},
)
if res.status_code == 404:
raise Exception("workspaces not found") # TODO custom exception
if res.status_code != 200:
raise Exception(res.text)
res_json = res.json()
return res_json["workspaces"]
def get(
self,
workspace: Optional[Union[WorkspaceId, Workspace]] = None,
) -> Workspace:
"""Gets a workspace.
Parameters
----------
workspace : WorkspaceId or Workspace, optional
Workspace id or Workspace to get latest version of
Raises
------
Exception
If the workspace wasn't found.
Exception
If the API request wasn't successful.
Returns
------
Workspace
"""
workspace_id = None if workspace is None else to_workspace_id(workspace)
res = requests.get(
f"{self.seam.api_url}/workspaces/get",
params={"workspace_id": workspace_id},
headers={"Authorization": f"Bearer {self.seam.api_key}"},
)
if res.status_code == 404:
raise Exception("workspace not found") # TODO custom exception
if res.status_code != 200:
raise Exception(res.text)
res_json = res.json()
return Workspace(
workspace_id=res_json["workspace"]["workspace_id"],
name=res_json["workspace"]["name"],
is_sandbox=res_json["workspace"]["is_sandbox"],
)
def reset_sandbox(self) -> None:
"""Resets workspace sandbox.
Raises
------
Exception
If the API request wasn't successful.
Returns
------
None
"""
res = requests.post(
f"{self.seam.api_url}/workspaces/reset_sandbox",
headers={"Authorization": f"Bearer {self.seam.api_key}"},
)
if not res.ok:
raise Exception(res.text)
return None