-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path_startup_scripts.py
More file actions
110 lines (86 loc) · 3.06 KB
/
_startup_scripts.py
File metadata and controls
110 lines (86 loc) · 3.06 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
STARTUP_SCRIPTS_ENDPOINT = '/scripts'
class StartupScript:
"""A startup script model class."""
def __init__(self, id: str, name: str, script: str) -> None:
"""Initialize a new startup script object.
:param id: startup script id
:type id: str
:param name: startup script name
:type name: str
:param script: the actual script
:type script: str
"""
self._id = id
self._name = name
self._script = script
@property
def id(self) -> str:
"""Get the startup script id.
:return: startup script id
:rtype: str
"""
return self._id
@property
def name(self) -> str:
"""Get the startup script name.
:return: startup script name
:rtype: str
"""
return self._name
@property
def script(self) -> str:
"""Get the actual startup script code.
:return: startup script text
:rtype: str
"""
return self._script
class StartupScriptsService:
"""A service for interacting with the startup scripts endpoint."""
def __init__(self, http_client) -> None:
self._http_client = http_client
def get(self) -> list[StartupScript]:
"""Get all of the client's startup scripts.
:return: list of startup script objects
:rtype: list[StartupScript]
"""
scripts = self._http_client.get(STARTUP_SCRIPTS_ENDPOINT).json()
scripts_objects = [
StartupScript(script['id'], script['name'], script['script']) for script in scripts
]
return scripts_objects
def get_by_id(self, id) -> StartupScript:
"""Get a specific startup script by id.
:param id: startup script id
:type id: str
:return: startup script object
:rtype: StartupScript
"""
script = self._http_client.get(STARTUP_SCRIPTS_ENDPOINT + f'/{id}').json()[0]
return StartupScript(script['id'], script['name'], script['script'])
def delete(self, id_list: list[str]) -> None:
"""Delete multiple startup scripts by id.
:param id_list: list of startup scripts ids
:type id_list: list[str]
"""
payload = {'scripts': id_list}
self._http_client.delete(STARTUP_SCRIPTS_ENDPOINT, json=payload)
return
def delete_by_id(self, id: str) -> None:
"""Delete a single startup script by id.
:param id: startup script id
:type id: str
"""
self._http_client.delete(STARTUP_SCRIPTS_ENDPOINT + f'/{id}')
return
def create(self, name: str, script: str) -> StartupScript:
"""Create a new startup script.
:param name: startup script name
:type name: str
:param script: startup script value
:type script: str
:return: the new startup script's id
:rtype: str
"""
payload = {'name': name, 'script': script}
id = self._http_client.post(STARTUP_SCRIPTS_ENDPOINT, json=payload).text
return StartupScript(id, name, script)