Skip to content

Commit c345fc8

Browse files
authored
refactor(mcsmapi): 重构 API 类方法并优化模型定义 (#14)
- 将 API 类中的实例方法改为静态方法,简化使用 - 优化模型定义,提高代码可读性和维护性 - 新增 common 模型模块,用于定义通用的模型结构 - 调整文件列表和 Docker 相关模型结构,增强数据表达能力
1 parent ec9e894 commit c345fc8

File tree

15 files changed

+470
-409
lines changed

15 files changed

+470
-409
lines changed

mcsmapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def login_with_apikey(self, apikey: str):
3636
return self
3737

3838
def overview(self) -> OverviewModel:
39-
return Overview().init()
39+
return Overview.init()
4040

4141
def instance(self) -> Instance:
4242
return Instance()

mcsmapi/apis/daemon.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
from mcsmapi.pool import ApiPool
33
from mcsmapi.request import send
44
from mcsmapi.models.daemon import DaemonConfig, DaemonModel
5-
from mcsmapi.models.daemon.instance import InstanceDetail
65

76

87
class Daemon:
9-
def show(self) -> list[DaemonConfig]:
8+
@staticmethod
9+
def show() -> list[DaemonConfig]:
1010
"""
1111
获取全部节点配置信息
1212
@@ -18,11 +18,12 @@ def show(self) -> list[DaemonConfig]:
1818
f"{ApiPool.SERVICE}/remote_services_list",
1919
)
2020
return [DaemonConfig(**daemon) for daemon in daemons]
21-
22-
def system(self) -> list[DaemonModel]:
21+
22+
@staticmethod
23+
def system() -> list[DaemonModel]:
2324
"""
2425
获取全部节点的系统信息
25-
26+
2627
返回:
2728
- List[DaemonModel]: 节点系统信息列表
2829
"""
@@ -32,7 +33,8 @@ def system(self) -> list[DaemonModel]:
3233
)
3334
return [DaemonModel(**daemon) for daemon in daemons]
3435

35-
def add(self, config: dict[str, Any]) -> str:
36+
@staticmethod
37+
def add(config: dict[str, Any]) -> str:
3638
"""
3739
新增一个节点。
3840
@@ -47,8 +49,8 @@ def add(self, config: dict[str, Any]) -> str:
4749
f"{ApiPool.SERVICE}/remote_service",
4850
data=DaemonConfig(**config).dict(),
4951
)
50-
51-
def delete(self, daemonId: str) -> bool:
52+
@staticmethod
53+
def delete(daemonId: str) -> bool:
5254
"""
5355
删除一个节点。
5456
@@ -61,8 +63,8 @@ def delete(self, daemonId: str) -> bool:
6163
return send(
6264
"DELETE", f"{ApiPool.SERVICE}/remote_service", params={"uuid": daemonId}
6365
)
64-
65-
def link(self, daemonId: str) -> bool:
66+
@staticmethod
67+
def link(daemonId: str) -> bool:
6668
"""
6769
连接一个节点。
6870
@@ -75,11 +77,11 @@ def link(self, daemonId: str) -> bool:
7577
return send(
7678
"GET", f"{ApiPool.SERVICE}/link_remote_service", params={"uuid": daemonId}
7779
)
78-
79-
def update(self, daemonId: str, config: dict[str, Any]) -> bool:
80+
@staticmethod
81+
def update(daemonId: str, config: dict[str, Any]) -> bool:
8082
"""
8183
更新一个节点的配置。
82-
84+
8385
**不建议直接使用此函数,建议调用overview()后在remote属性内使用updateConfig方法按需更新**
8486
8587
参数:

mcsmapi/apis/file.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77

88
class File:
9+
@staticmethod
910
def show(
10-
self,
1111
daemonId: str,
1212
uuid: str,
1313
target: str = "",
1414
page: int = 0,
1515
page_size: int = 100,
16-
file_name: str = ""
16+
file_name: str = "",
1717
) -> FileList:
1818
"""
1919
获取文件列表
@@ -43,7 +43,8 @@ def show(
4343
)
4444
return FileList(**result, daemonId=daemonId, uuid=uuid)
4545

46-
def content(self, daemonId: str, uuid: str, target: str) -> str | bytes:
46+
@staticmethod
47+
def content(daemonId: str, uuid: str, target: str) -> str | bytes:
4748
"""
4849
获取文件内容
4950
@@ -62,7 +63,8 @@ def content(self, daemonId: str, uuid: str, target: str) -> str | bytes:
6263
data={"target": target},
6364
)
6465

65-
def update(self, daemonId: str, uuid: str, target: str, text: str) -> bool:
66+
@staticmethod
67+
def update(daemonId: str, uuid: str, target: str, text: str) -> bool:
6668
"""
6769
更新文件内容
6870
@@ -82,7 +84,8 @@ def update(self, daemonId: str, uuid: str, target: str, text: str) -> bool:
8284
data={"target": target, "text": text},
8385
)
8486

85-
def download(self, daemonId: str, uuid: str, file_name: str) -> str:
87+
@staticmethod
88+
def download(daemonId: str, uuid: str, file_name: str) -> str:
8689
"""
8790
下载文件
8891
@@ -105,9 +108,8 @@ def download(self, daemonId: str, uuid: str, file_name: str) -> str:
105108
base_url = urllib.parse.urljoin(f"{protocol}://{result.addr}", "download")
106109
return urllib.parse.urljoin(base_url, f"{result.password}/{file_name}")
107110

108-
async def upload(
109-
self, daemonId: str, uuid: str, file: bytes, upload_dir: str
110-
) -> bool:
111+
@staticmethod
112+
async def upload(daemonId: str, uuid: str, file: bytes, upload_dir: str) -> bool:
111113
"""
112114
上传文件
113115
@@ -132,7 +134,8 @@ async def upload(
132134
await upload(final_url, file)
133135
return True
134136

135-
def copy(self, daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
137+
@staticmethod
138+
def copy(daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
136139
"""
137140
复制多个文件夹或文件到指定位置。
138141
@@ -152,7 +155,8 @@ def copy(self, daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
152155
data={"targets": targets},
153156
)
154157

155-
def copyOne(self, daemonId: str, uuid: str, source: str, target: str) -> bool:
158+
@staticmethod
159+
def copyOne(daemonId: str, uuid: str, source: str, target: str) -> bool:
156160
"""
157161
复制单个文件或文件夹到指定位置。
158162
@@ -165,9 +169,10 @@ def copyOne(self, daemonId: str, uuid: str, source: str, target: str) -> bool:
165169
**返回:**
166170
- bool: 移动成功后返回True。
167171
"""
168-
return self.copy(daemonId, uuid, {source: target})
172+
return File.copy(daemonId, uuid, {source: target})
169173

170-
def move(self, daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
174+
@staticmethod
175+
def move(daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
171176
"""
172177
移动多个文件或文件夹到指定位置。
173178
@@ -187,7 +192,8 @@ def move(self, daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
187192
data={"targets": targets},
188193
)
189194

190-
def moveOne(self, daemonId: str, uuid: str, source: str, target: str) -> bool:
195+
@staticmethod
196+
def moveOne(daemonId: str, uuid: str, source: str, target: str) -> bool:
191197
"""
192198
从源路径移动单个文件或文件夹到目标路径。
193199
@@ -200,9 +206,10 @@ def moveOne(self, daemonId: str, uuid: str, source: str, target: str) -> bool:
200206
返回:
201207
- bool: 移动成功后返回True。
202208
"""
203-
return self.move(daemonId, uuid, {source: target})
209+
return File.move(daemonId, uuid, {source: target})
204210

205-
def rename(self, daemonId: str, uuid: str, source: str, new_name: str) -> bool:
211+
@staticmethod
212+
def rename(daemonId: str, uuid: str, source: str, new_name: str) -> bool:
206213
"""
207214
重命名单个文件或文件夹。
208215
@@ -217,9 +224,10 @@ def rename(self, daemonId: str, uuid: str, source: str, new_name: str) -> bool:
217224
"""
218225
directory = os.path.dirname(source)
219226
target = os.path.join(directory, new_name)
220-
return self.moveOne(daemonId, uuid, source, target)
227+
return File.moveOne(daemonId, uuid, source, target)
221228

222-
def zip(self, daemonId: str, uuid: str, source: str, targets: list[str]) -> bool:
229+
@staticmethod
230+
def zip(daemonId: str, uuid: str, source: str, targets: list[str]) -> bool:
223231
"""
224232
压缩多个文件或文件夹到指定位置。
225233
@@ -239,8 +247,9 @@ def zip(self, daemonId: str, uuid: str, source: str, targets: list[str]) -> bool
239247
data={"type": 1, "code": "utf-8", "source": source, "targets": targets},
240248
)
241249

250+
@staticmethod
242251
def unzip(
243-
self, daemonId: str, uuid: str, source: str, target: str, code: str = "utf-8"
252+
daemonId: str, uuid: str, source: str, target: str, code: str = "utf-8"
244253
) -> bool:
245254
"""
246255
解压缩指定的zip文件到目标位置。
@@ -263,7 +272,8 @@ def unzip(
263272
data={"type": 2, "code": code, "source": source, "targets": target},
264273
)
265274

266-
def delete(self, daemonId: str, uuid: str, targets: list[str]) -> bool:
275+
@staticmethod
276+
def delete(daemonId: str, uuid: str, targets: list[str]) -> bool:
267277
"""
268278
删除多个文件或文件夹。
269279
@@ -282,7 +292,8 @@ def delete(self, daemonId: str, uuid: str, targets: list[str]) -> bool:
282292
data={"targets": targets},
283293
)
284294

285-
def createFile(self, daemonId: str, uuid: str, target: str) -> bool:
295+
@staticmethod
296+
def createFile(daemonId: str, uuid: str, target: str) -> bool:
286297
"""
287298
创建文件。
288299
@@ -301,7 +312,8 @@ def createFile(self, daemonId: str, uuid: str, target: str) -> bool:
301312
data={"target": target},
302313
)
303314

304-
def createFloder(self, daemonId: str, uuid: str, target: str) -> bool:
315+
@staticmethod
316+
def createFloder(daemonId: str, uuid: str, target: str) -> bool:
305317
"""
306318
创建文件夹
307319

mcsmapi/apis/image.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55

66
class Image:
7-
def images(self, daemonId: str) -> list[DockerImageItem]:
7+
@staticmethod
8+
def images(daemonId: str) -> list[DockerImageItem]:
89
"""
910
获取镜像列表
1011
@@ -24,7 +25,8 @@ def images(self, daemonId: str) -> list[DockerImageItem]:
2425

2526
return [DockerImageItem(**item) for item in result]
2627

27-
def containers(self, daemonId: str) -> list[DockerContainerItem]:
28+
@staticmethod
29+
def containers(daemonId: str) -> list[DockerContainerItem]:
2830
"""
2931
获取容器列表
3032
@@ -44,7 +46,8 @@ def containers(self, daemonId: str) -> list[DockerContainerItem]:
4446

4547
return [DockerContainerItem(**item) for item in result]
4648

47-
def network(self, daemonId: str) -> list[DockerNetworkItem]:
49+
@staticmethod
50+
def network(daemonId: str) -> list[DockerNetworkItem]:
4851
"""
4952
获取网络接口列表
5053
@@ -63,7 +66,8 @@ def network(self, daemonId: str) -> list[DockerNetworkItem]:
6366
)
6467
return [DockerNetworkItem(**item) for item in result]
6568

66-
def add(self, daemonId: str, dockerFile: str, name: str, tag: str) -> bool:
69+
@staticmethod
70+
def add(daemonId: str, dockerFile: str, name: str, tag: str) -> bool:
6771
"""
6872
新增一个镜像
6973
@@ -83,7 +87,8 @@ def add(self, daemonId: str, dockerFile: str, name: str, tag: str) -> bool:
8387
data={"dockerFile": dockerFile, "name": name, "tag": tag},
8488
)
8589

86-
def progress(self, daemonId: str) -> dict[str, int]:
90+
@staticmethod
91+
def progress(daemonId: str) -> dict[str, int]:
8792
"""
8893
获取镜像构建进度
8994

0 commit comments

Comments
 (0)