Skip to content

Commit 3ff4880

Browse files
committed
refactor(mcsmapi): 优化代码结构和文档
- 移除函数返回类型注解 - 统一代码格式 - 修改部分函数描述,使其更加准确 - 在 InstanceDetail 类中添加 command 和 get_output 方法
1 parent 88a88ff commit 3ff4880

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

mcsmapi/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
class MCSMAPI:
1313

14-
def __init__(self, url: str, timeout: int = 5) -> None:
14+
def __init__(self, url: str, timeout: int = 5):
1515
split_url = urllib.parse.urlsplit(url)
1616
Request.set_mcsm_url(
1717
urllib.parse.urljoin(f"{split_url.scheme}://{split_url.netloc}", "")
1818
)
1919
Request.set_timeout(timeout)
2020

21-
def login(self, username: str, password: str) -> "MCSMAPI":
21+
def login(self, username: str, password: str):
2222
Request.set_token(
2323
Request.send(
2424
"POST",
@@ -34,20 +34,20 @@ def login_with_apikey(self, apikey: str):
3434
self.authentication = "apikey"
3535
return self
3636

37-
def overview(self) -> Overview:
37+
def overview(self):
3838
return Overview()
3939

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

43-
def user(self) -> User:
43+
def user(self) :
4444
return User()
4545

46-
def daemon(self) -> Daemon:
46+
def daemon(self):
4747
return Daemon()
4848

49-
def file(self) -> File:
49+
def file(self):
5050
return File()
5151

52-
def image(self) -> Image:
52+
def image(self):
5353
return Image()

mcsmapi/apis/instance.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def updateConfig(daemonId: str, uuid: str, config: dict[str, Any]) -> str:
9494
:params uuid: 实例的UUID
9595
:params config: 新的实例配置,以字典形式提供,缺失内容由InstanceConfig模型补全
9696
97-
:returns: 更新成功后返回更新的实例UUID
97+
:returns: 被更新配置的实例UUID
9898
"""
9999
result = send(
100100
"PUT",
@@ -109,7 +109,7 @@ def delete(daemonId: str, uuids: list[str], deleteFile: bool = False) -> list[st
109109
"""
110110
删除实例
111111
112-
:params daemonId: 节点的标识符
112+
:params daemonId: 节点的UUID
113113
:params uuids: 要删除的实例UUID列表
114114
:params deleteFile: 是否删除关联的文件
115115
@@ -130,7 +130,7 @@ def start(daemonId: str, uuid: str) -> str:
130130
:params daemonId: 节点的UUID
131131
:params uuid: 实例的UUID
132132
133-
:returns: 返回被操作的实例的UUID
133+
:returns: 被启动的实例的UUID
134134
"""
135135
result = send(
136136
"GET",
@@ -147,7 +147,7 @@ def stop(daemonId: str, uuid: str) -> str:
147147
:params daemonId: 节点的UUID
148148
:params uuid: 实例的UUID
149149
150-
:returns: 返回被操作的实例的UUID
150+
:returns: 被关闭的实例的UUID
151151
"""
152152
result = send(
153153
"GET",
@@ -164,7 +164,7 @@ def restart(daemonId: str, uuid: str) -> str:
164164
:params daemonId: 节点的UUID
165165
:params uuid: 实例的UUID
166166
167-
:returns: 返回被操作的实例的UUID
167+
:returns: 被重启的实例的UUID
168168
"""
169169
result = send(
170170
"GET",
@@ -181,7 +181,7 @@ def kill(daemonId: str, uuid: str) -> str:
181181
:params daemonId: 节点的UUID
182182
:params uuid: 实例的UUID
183183
184-
:returns: 返回被操作的实例的UUID
184+
:returns: 被强制关闭的实例的UUID
185185
"""
186186
result = send(
187187
"GET",
@@ -230,7 +230,7 @@ def command(daemonId: str, uuid: str, command: str) -> str:
230230
:params uuid: 实例的UUID
231231
:params command: 要发送的命令
232232
233-
:params: 返回被操作的实例的UUID
233+
:returns: 被操作的实例的UUID
234234
"""
235235
result = send(
236236
"GET",

mcsmapi/models/instance.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def start(self):
179179
"""
180180
启动该实例
181181
182-
:returns: 返回被操作的实例的UUID
182+
:returns: 被启动的实例的UUID
183183
"""
184184
from mcsmapi.apis.instance import Instance
185185

@@ -189,7 +189,7 @@ def stop(self):
189189
"""
190190
停止该实例
191191
192-
:returns: 返回被操作的实例的UUID
192+
:returns: 被停止的实例的UUID
193193
"""
194194
from mcsmapi.apis.instance import Instance
195195

@@ -199,7 +199,7 @@ def restart(self):
199199
"""
200200
重启该实例
201201
202-
:returns: 返回被操作的实例的UUID
202+
:returns: 被重启的实例的UUID
203203
"""
204204
from mcsmapi.apis.instance import Instance
205205

@@ -209,7 +209,7 @@ def kill(self):
209209
"""
210210
强制关闭该实例
211211
212-
:returns: 返回被操作的实例的UUID
212+
:returns: 被强制关闭的实例的UUID
213213
"""
214214
from mcsmapi.apis.instance import Instance
215215

@@ -268,6 +268,30 @@ def reinstall(self, targetUrl: str, title: str = "", description: str = ""):
268268
self.daemonId, self.instanceUuid, targetUrl, title, description
269269
)
270270

271+
def command(self, command: str) -> str:
272+
"""
273+
发送命令给实例。
274+
275+
:params command: 要发送的命令
276+
277+
:returns: 被操作的实例的UUID
278+
"""
279+
from mcsmapi.apis.instance import Instance
280+
281+
return Instance.command(self.daemonId, self.instanceUuid, command)
282+
283+
def get_output(self, size: int | None = None) -> str:
284+
"""
285+
获取实例的输出。
286+
287+
:params size: 要获取的输出大小
288+
289+
:returns: 输出结果
290+
"""
291+
from mcsmapi.apis.instance import Instance
292+
293+
return Instance.get_output(self.daemonId, self.instanceUuid, size)
294+
271295
def files(
272296
self, target: str = "", page: int = 0, page_size: int = 100, file_name: str = ""
273297
):

0 commit comments

Comments
 (0)