mcsmapi 0.1.1__py3-none-any.whl → 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mcsmapi/common.py DELETED
@@ -1,34 +0,0 @@
1
- import requests
2
-
3
-
4
- class ApiClient:
5
- def __init__(self, url, apikey=None, timeout=None):
6
- self.url = url
7
- self.apikey = apikey
8
- self.timeout = timeout
9
- self.cookies = None
10
- self.token = None
11
-
12
- def send(self, method, endpoint, params={}, data=None):
13
- url = f"{self.url}/api/{endpoint}"
14
-
15
- if self.apikey is not None:
16
- params['apikey'] = self.apikey
17
- if self.token is not None:
18
- params['token'] = self.token
19
-
20
- response = requests.request(
21
- method, url, params=params, json=data, cookies=self.cookies, timeout=self.timeout)
22
- response.raise_for_status()
23
- return response
24
-
25
-
26
- def support_login(cls):
27
- def login(self, username, password):
28
- seq = self.client.send(
29
- "POST", "auth/login", data={"username": username, "password": password})
30
- self.client.cookies = seq.cookies
31
- self.client.token = seq.json()["data"]
32
-
33
- setattr(cls, 'login', login)
34
- return cls
mcsmapi/daemon.py DELETED
@@ -1,86 +0,0 @@
1
- from .common import support_login, ApiClient
2
- import requests
3
-
4
-
5
- @support_login
6
- class Daemon:
7
- def __init__(self, url: str, apikey: str = None):
8
- """
9
- Initialize a new Daemon instance.
10
-
11
- :param url: The URL of the daemon service.
12
- :param apikey: Optional API key for authentication.
13
- """
14
- self.client = ApiClient(url, apikey)
15
-
16
- def getList(self) -> list:
17
- """
18
- Get a list of remote services.
19
-
20
- :return: A list of remote services.
21
- """
22
- return self.client.send("GET", "overview").json()["data"]["remote"]
23
-
24
- def add(self, ip: str, port: int, remarks: str, apiKey: str, prefix: str = "") -> requests.Response:
25
- """
26
- Add a new remote service.
27
-
28
- :param ip: IP address of the remote service.
29
- :param port: Port number of the remote service.
30
- :param remarks: Remarks or description of the remote service.
31
- :param apiKey: API key for the remote service.
32
- :param prefix: Optional prefix for the remote service.
33
- :return: Response data from the server.
34
- """
35
- return self.client.send(
36
- "POST", "service/remote_service", data={
37
- "ip": ip,
38
- "port": port,
39
- "prefix": prefix,
40
- "remarks": remarks,
41
- "apiKey": apiKey
42
- })
43
-
44
- def delete(self, uuid: str) -> requests.Response:
45
- """
46
- Delete a remote service by its UUID.
47
-
48
- :param uuid: UUID of the remote service to delete.
49
- :return: Response data from the server.
50
- """
51
- return self.client.send(
52
- "DELETE", "service/remote_service", params={"uuid": uuid})
53
-
54
- def tryConnect(self, uuid: str) -> requests.Response:
55
- """
56
- Try to connect to a remote service by its UUID.
57
-
58
- :param uuid: UUID of the remote service to connect to.
59
- :return: Response data from the server.
60
- """
61
- return self.client.send(
62
- "GET", "service/link_remote_service", params={"uuid": uuid})
63
-
64
- def updateDaemonConnectConfig(self, uuid: str, ip: str, port: int, remarks: str, apiKey: str, available: bool = False, prefix: str = "") -> requests.Response:
65
- """
66
- Update the configuration of a remote service.
67
-
68
- :param uuid: UUID of the remote service to update.
69
- :param ip: New IP address of the remote service.
70
- :param port: New port number of the remote service.
71
- :param remarks: New remarks or description of the remote service.
72
- :param apiKey: New API key for the remote service.
73
- :param available: Whether the service is available.
74
- :param prefix: Optional prefix for the remote service.
75
- :return: Response data from the server.
76
- """
77
- return self.client.send(
78
- "PUT", "service/remote_service", data={
79
- "uuid": uuid,
80
- "ip": ip,
81
- "port": port,
82
- "prefix": prefix,
83
- "available": available,
84
- "remarks": remarks,
85
- "apiKey": apiKey
86
- })
mcsmapi/file.py DELETED
@@ -1,326 +0,0 @@
1
- from .common import support_login, ApiClient
2
- import aiohttp
3
- import requests
4
- import os
5
-
6
-
7
- @support_login
8
- class File:
9
- def __init__(self, url: str, apikey: str = None):
10
- """
11
- Initialize a new File instance.
12
-
13
- :param url: The URL of the file service.
14
- :param apikey: Optional API key for authentication.
15
- """
16
- self.client = ApiClient(url, apikey)
17
-
18
- def getList(self, daemonId: str, instanceUuid: str, target: str, page: int = 1, page_size: int = 10) -> requests.Response:
19
- """
20
- Get a list of files.
21
-
22
- :param daemonId: ID of the daemon.
23
- :param instanceUuid: UUID of the instance.
24
- :param target: Target directory path.
25
- :param page: Page number (default is 1).
26
- :param page_size: Number of items per page (default is 10).
27
- :return: Response data from the server.
28
- """
29
- return self.client.send("GET", "files/list", params={
30
- "daemonId": daemonId,
31
- "uuid": instanceUuid,
32
- "target": target,
33
- "page": page,
34
- "page_size": page_size
35
- })
36
-
37
- def getContents(self, daemonId: str, instanceUuid: str, target: str) -> requests.Response:
38
- """
39
- Get the contents of a file.
40
-
41
- :param daemonId: ID of the daemon.
42
- :param instanceUuid: UUID of the instance.
43
- :param target: Path to the file.
44
- :return: Response data from the server.
45
- """
46
- return self.client.send("PUT", "files", params={
47
- "daemonId": daemonId,
48
- "uuid": instanceUuid
49
- }, data={"target": target})
50
-
51
- def update(self, daemonId: str, instanceUuid: str, target: str, text: str) -> requests.Response:
52
- """
53
- Update the contents of a file.
54
-
55
- :param daemonId: ID of the daemon.
56
- :param instanceUuid: UUID of the instance.
57
- :param target: Path to the file.
58
- :param text: New content for the file.
59
- :return: Response data from the server.
60
- """
61
- return self.client.send("PUT", "files", params={
62
- "daemonId": daemonId,
63
- "uuid": instanceUuid
64
- }, data={"target": target, "text": text})
65
-
66
- def download(self, daemonId: str, instanceUuid: str, file_name: str, download_to_path: str) -> str:
67
- """
68
- Download a file from the server.
69
-
70
- :param daemonId: ID of the daemon.
71
- :param instanceUuid: UUID of the instance.
72
- :param file_name: Name of the file to download.
73
- :param download_to_path: Local path where the file will be saved.
74
- :return: Path to the downloaded file.
75
- """
76
- seq = self.client.send("POST", "files/download", params={
77
- "daemonId": daemonId,
78
- "uuid": instanceUuid,
79
- "file_name": file_name
80
- }).json()
81
- password = seq["password"]
82
- addr = seq["addr"]
83
-
84
- response = requests.get(
85
- f"http://{addr}/download/{password}/{file_name}", stream=True)
86
- if response.status_code == 200:
87
- file_path = os.path.join(download_to_path, file_name)
88
- with open(file_path, "wb") as f:
89
- for chunk in response.iter_content(chunk_size=1024):
90
- if chunk:
91
- f.write(chunk)
92
- return file_path
93
- else:
94
- raise Exception(
95
- f"Failed to download file. Status code: {response.status_code}")
96
-
97
- async def aiodownload(self, daemonId: str, instanceUuid: str, file_name: str, download_to_path: str) -> str:
98
- """
99
- Asynchronously download a file from the server.
100
-
101
- :param daemonId: ID of the daemon.
102
- :param instanceUuid: UUID of the instance.
103
- :param file_name: Name of the file to download.
104
- :param download_to_path: Local path where the file will be saved.
105
- :return: Path to the downloaded file.
106
- """
107
- seq = self.client.send("POST", "files/download", params={
108
- "daemonId": daemonId,
109
- "uuid": instanceUuid,
110
- "file_name": file_name
111
- }).json()
112
- password = seq["password"]
113
- addr = seq["addr"]
114
-
115
- async with aiohttp.ClientSession() as session:
116
- async with session.get(f"http://{addr}/download/{password}/{file_name}") as response:
117
- if response.status == 200:
118
- file_path = os.path.join(download_to_path, file_name)
119
- async with open(file_path, "wb") as f:
120
- await f.write(await response.read())
121
- return file_path
122
- else:
123
- raise Exception(
124
- f"Failed to download file. Status code: {response.status}")
125
-
126
- def upload(self, daemonId: str, instanceUuid: str, upload_dir: str, file_data: bytes) -> bool:
127
- """
128
- Upload a file to the server.
129
-
130
- :param daemonId: ID of the daemon.
131
- :param instanceUuid: UUID of the instance.
132
- :param upload_dir: Directory on the server where the file will be uploaded.
133
- :param file_data: Binary data of the file to upload.
134
- :return: True if the upload was successful.
135
- """
136
- seq = self.client.send("POST", "files/upload", params={
137
- "daemonId": daemonId,
138
- "uuid": instanceUuid,
139
- "upload_dir": upload_dir
140
- }).json()
141
- password = seq["password"]
142
- addr = seq["addr"]
143
-
144
- response = requests.post(
145
- f"http://{addr}/upload/{password}", files={"file": file_data})
146
-
147
- if response.status_code == 200:
148
- return True
149
- else:
150
- raise Exception(
151
- f"Failed to upload file. Status code: {response.status_code}")
152
-
153
- async def aioupload(self, daemonId: str, instanceUuid: str, upload_dir: str, file_data: bytes) -> bool:
154
- """
155
- Asynchronously upload a file to the server.
156
-
157
- :param daemonId: ID of the daemon.
158
- :param instanceUuid: UUID of the instance.
159
- :param upload_dir: Directory on the server where the file will be uploaded.
160
- :param file_data: Binary data of the file to upload.
161
- :return: True if the upload was successful.
162
- """
163
- seq = self.client.send("POST", "files/upload", params={
164
- "daemonId": daemonId,
165
- "uuid": instanceUuid,
166
- "upload_dir": upload_dir
167
- }).json()
168
- password = seq["password"]
169
- addr = seq["addr"]
170
- async with aiohttp.ClientSession() as session:
171
- async with session.post(f"http://{addr}/upload/{password}", data={"file": file_data}) as response:
172
- if response.status == 200:
173
- return True
174
- else:
175
- raise Exception(
176
- f"Failed to upload file. Status code: {response.status}")
177
-
178
- def copy(self, daemonId: str, instanceUuid: str, source_list: list, target_list: list) -> requests.Response:
179
- """
180
- Copy files from one location to another.
181
-
182
- :param daemonId: ID of the daemon.
183
- :param instanceUuid: UUID of the instance.
184
- :param source_list: List of source file paths.
185
- :param target_list: List of target file paths.
186
- :return: Response data from the server.
187
- """
188
- data = {
189
- "targets": [
190
- [source, target] for source, target in zip(source_list, target_list)
191
- ]
192
- }
193
-
194
- return self.client.send("POST", "files/copy", params={
195
- "daemonId": daemonId,
196
- "uuid": instanceUuid
197
- }, data=data)
198
-
199
- def moveOrRename(self, daemonId: str, instanceUuid: str, source_list: list, target_list: list) -> requests.Response:
200
- """
201
- Move or rename files.
202
-
203
- :param daemonId: ID of the daemon.
204
- :param instanceUuid: UUID of the instance.
205
- :param source_list: List of source file paths.
206
- :param target_list: List of target file paths.
207
- :return: Response data from the server.
208
- """
209
- data = {
210
- "targets": [
211
- [source, target] for source, target in zip(source_list, target_list)
212
- ]
213
- }
214
- return self.client.send("PUT", "files/move", params={
215
- "daemonId": daemonId,
216
- "uuid": instanceUuid
217
- }, data=data)
218
-
219
- def zip(self, daemonId: str, instanceUuid: str, zip_file_path: str, targets: list) -> requests.Response:
220
- """
221
- Compress files into a ZIP archive.
222
-
223
- :param daemonId: ID of the daemon.
224
- :param instanceUuid: UUID of the instance.
225
- :param zip_file_path: Path to the resulting ZIP file.
226
- :param targets: List of files to compress.
227
- :return: Response data from the server.
228
- """
229
- zip_file_path = os.path.join(zip_file_path)
230
-
231
- return self.client.send("POST", "files/compress", params={
232
- "daemonId": daemonId,
233
- "uuid": instanceUuid
234
- }, data={
235
- "type": 1,
236
- "code": "utf-8",
237
- "source": zip_file_path,
238
- "targets": targets
239
- })
240
-
241
- def unzip(self, daemonId: str, instanceUuid: str, zip_file_path: str, unzip_to: str, code: str = "utf-8") -> requests.Response:
242
- """
243
- Decompresses a ZIP file.
244
-
245
- Args:
246
- daemonId (str): ID of the daemon.
247
- instanceUuid (str): UUID of the instance.
248
- zip_file_path (str): Path to the ZIP file.
249
- unzip_to (str): Directory path to extract the files to.
250
- code (str, optional): Encoding type. Defaults to "utf-8". Must be one of "utf-8", "gbk", or "big5".
251
-
252
- Returns:
253
- requests.Response: Response data from the server.
254
-
255
- Raises:
256
- ValueError: If the provided encoding type is not valid.
257
- """
258
- if code not in ["utf-8", "gbk", "big5"]:
259
- raise ValueError(
260
- "code must be one of utf-8, gbk, big5")
261
- return self.client.send("POST", "files/compress", params={
262
- "daemonId": daemonId,
263
- "uuid": instanceUuid
264
- }, data={
265
- "type": 2,
266
- "code": code,
267
- "source": zip_file_path,
268
- "target": unzip_to
269
- })
270
-
271
- def delete(self, daemonId: str, instanceUuid: str, targets: list) -> requests.Response:
272
- """
273
- Deletes files or directories.
274
-
275
- Args:
276
- daemonId (str): ID of the daemon.
277
- instanceUuid (str): UUID of the instance.
278
- targets (list): List of paths to the files or directories to delete.
279
-
280
- Returns:
281
- requests.Response: Response data from the server.
282
- """
283
- return self.client.send("DELETE", "files", params={
284
- "daemonId": daemonId,
285
- "uuid": instanceUuid
286
- }, data={
287
- "targets": targets
288
- })
289
-
290
- def touch(self, daemonId: str, instanceUuid: str, target: str) -> requests.Response:
291
- """
292
- Creates an empty file.
293
-
294
- Args:
295
- daemonId (str): ID of the daemon.
296
- instanceUuid (str): UUID of the instance.
297
- target (str): Path to the file to create.
298
-
299
- Returns:
300
- requests.Response: Response data from the server.
301
- """
302
- return self.client.send("POST", "files/touch", params={
303
- "daemonId": daemonId,
304
- "uuid": instanceUuid
305
- }, data={
306
- "target": target
307
- })
308
-
309
- def createFolder(self, daemonId: str, instanceUuid: str, target: str) -> requests.Response:
310
- """
311
- Creates a directory.
312
-
313
- Args:
314
- daemonId (str): ID of the daemon.
315
- instanceUuid (str): UUID of the instance.
316
- target (str): Path to the directory to create.
317
-
318
- Returns:
319
- requests.Response: Response data from the server.
320
- """
321
- return self.client.send("POST", "files/mkdir", params={
322
- "daemonId": daemonId,
323
- "uuid": instanceUuid
324
- }, data={
325
- "target": target
326
- })
mcsmapi/image.py DELETED
@@ -1,104 +0,0 @@
1
- from .common import support_login, ApiClient
2
- import requests
3
-
4
-
5
- @support_login
6
- class Image:
7
- def __init__(self, url: str, apikey: str = None):
8
- """
9
- Initializes a new Image instance.
10
-
11
- Args:
12
- url (str): The URL of the image service.
13
- apikey (str, optional): Optional API key for authentication. Defaults to None.
14
- """
15
- self.client = ApiClient(url, apikey)
16
-
17
- def getImageList(self, daemonId: str) -> requests.Response:
18
- """
19
- Retrieves a list of Docker images.
20
-
21
- DockerImageList:
22
- https://docs.docker.com/engine/api/v1.37/#tag/Image/operation/ImageList
23
-
24
- Args:
25
- daemonId (str): ID of the Docker daemon.
26
-
27
- Returns:
28
- dict: Response data containing the list of Docker images.
29
- """
30
-
31
- return self.client.send("GET", "environment/image", params={
32
- "daemonId": daemonId
33
- })
34
-
35
- def getContainerList(self, daemonId: str) -> requests.Response:
36
- """
37
- Retrieves a list of Docker containers.
38
-
39
- DockerContainerList:
40
- https://docs.docker.com/engine/api/v1.37/#tag/Container/operation/ContainerList
41
-
42
- Args:
43
- daemonId (str): ID of the Docker daemon.
44
-
45
- Returns:
46
- dict: Response data containing the list of Docker containers.
47
- """
48
-
49
- return self.client.send("GET", "environment/containers", params={
50
- "daemonId": daemonId
51
- })
52
-
53
- def getNetworkModeList(self, daemonId: str) -> requests.Response:
54
- """
55
- Retrieves a list of Docker network modes.
56
-
57
- DockerNetworkModeList:
58
- https://docs.docker.com/engine/api/v1.37/#tag/Network/operation/NetworkList
59
-
60
- Args:
61
- daemonId (str): ID of the Docker daemon.
62
-
63
- Returns:
64
- dict: Response data containing the list of Docker network modes.
65
- """
66
-
67
- return self.client.send("GET", "environment/network", params={
68
- "daemonId": daemonId
69
- })
70
-
71
- def createImage(self, daemonId: str, dockerFileConfig: dict, imageName: str, tag: str) -> requests.Response:
72
- """
73
- Builds a Docker image from a Dockerfile configuration.
74
-
75
- Args:
76
- daemonId (str): ID of the Docker daemon.
77
- dockerFileConfig (dict): Configuration for the Dockerfile.
78
- imageName (str): Name of the Docker image.
79
- tag (str): Tag for the Docker image.
80
-
81
- Returns:
82
- dict: Response data indicating the status of the build process.
83
- """
84
- return self.client.send("POST", "environment/image", params={
85
- "daemonId": daemonId
86
- }, data={
87
- "dockerFile": dockerFileConfig,
88
- "imageName": imageName,
89
- "tag": tag
90
- })
91
-
92
- def buildProgress(self, daemonId: str) -> requests.Response:
93
- """
94
- Retrieves the progress of a Docker image build.
95
-
96
- Args:
97
- daemonId (str): ID of the Docker daemon.
98
-
99
- Returns:
100
- dict: Response data containing the build progress information.
101
- """
102
- return self.client.send("GET", "environment/image/progress", params={
103
- "daemonId": daemonId
104
- })