Skip to content

Commit 26cb2b4

Browse files
committed
Merge branch 'main' of github.com:molanp/mcsmapi
2 parents 793de25 + 285b636 commit 26cb2b4

File tree

10 files changed

+183
-108
lines changed

10 files changed

+183
-108
lines changed

mcsmapi/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import urllib.parse
2-
from mcsmapi.models.overview import OverviewModel
32
from mcsmapi.pool import ApiPool
43
from mcsmapi.apis.file import File
54
from mcsmapi.apis.user import User
@@ -11,12 +10,12 @@
1110

1211

1312
class MCSMAPI:
13+
1414
def __init__(self, url: str, timeout: int = 5) -> None:
1515
split_url = urllib.parse.urlsplit(url)
1616
Request.set_mcsm_url(
1717
urllib.parse.urljoin(f"{split_url.scheme}://{split_url.netloc}", "")
1818
)
19-
self.authentication = None
2019
Request.set_timeout(timeout)
2120

2221
def login(self, username: str, password: str) -> "MCSMAPI":
@@ -35,8 +34,8 @@ def login_with_apikey(self, apikey: str):
3534
self.authentication = "apikey"
3635
return self
3736

38-
def overview(self) -> OverviewModel:
39-
return Overview.init()
37+
def overview(self) -> Overview:
38+
return Overview()
4039

4140
def instance(self) -> Instance:
4241
return Instance()

mcsmapi/apis/file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from mcsmapi.pool import ApiPool
22
from mcsmapi.request import Request, send, upload
3-
from mcsmapi.models.file import CommonConfig, FileList
3+
from mcsmapi.models.file import FileDownloadConfig, FileList
44
import urllib.parse
55
import os
66

@@ -103,7 +103,7 @@ def download(daemonId: str, uuid: str, file_name: str) -> str:
103103
f"{ApiPool.FILE}/download",
104104
params={"daemonId": daemonId, "uuid": uuid, "file_name": file_name},
105105
)
106-
result = CommonConfig(**result)
106+
result = FileDownloadConfig(**result)
107107
protocol = Request.mcsm_url.split("://")[0]
108108
base_url = urllib.parse.urljoin(f"{protocol}://{result.addr}", "download")
109109
return urllib.parse.urljoin(base_url, f"{result.password}/{file_name}")
@@ -127,7 +127,7 @@ async def upload(daemonId: str, uuid: str, file: bytes, upload_dir: str) -> bool
127127
f"{ApiPool.FILE}/upload",
128128
params={"daemonId": daemonId, "uuid": uuid, "upload_dir": upload_dir},
129129
)
130-
result = CommonConfig(**result)
130+
result = FileDownloadConfig(**result)
131131
protocol = Request.mcsm_url.split("://")[0]
132132
base_url = urllib.parse.urljoin(f"{protocol}://{result.addr}", "upload")
133133
final_url = urllib.parse.urljoin(base_url, result.password)

mcsmapi/apis/overview.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
from mcsmapi.pool import ApiPool
22
from mcsmapi.request import send
3-
from mcsmapi.models.overview import OverviewModel
3+
from mcsmapi.models.overview import OverviewModel, LogDetail
44

55

66
class Overview:
77
@staticmethod
8-
def init():
8+
def overview():
99
"""
10-
初始化方法,用于获取API概览信息并构建概览模型。
11-
12-
本方法通过发送GET请求获取API概览信息,确保返回的数据类型为字典,
13-
然后使用这些数据来构建一个OverviewModel实例。
14-
15-
:return: 返回一个OverviewModel实例,该实例使用获取的API概览信息进行初始化。
10+
获取面板基本信息
1611
"""
1712
result = send("GET", ApiPool.OVERVIEW)
1813
return OverviewModel(**result)
14+
15+
@staticmethod
16+
def logs():
17+
"""
18+
获取面板操作日志
19+
"""
20+
result = send("GET", ApiPool.LOG)
21+
return [LogDetail(**item) for item in result]

mcsmapi/models/common.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44
class CpuMemChart(BaseModel):
55
"""节点资源使用率信息"""
66

7-
cpu: float = 0
7+
cpu: float
88
"""cpu使用率"""
9-
mem: float = 0
9+
mem: float
1010
"""内存使用率"""
1111

1212

1313
class ProcessInfo(BaseModel):
1414
"""节点进程详细信息"""
1515

16-
cpu: int = 0
16+
cpu: int
1717
"""远程节点使用的cpu资源(单位: byte)"""
18-
memory: int = 0
18+
memory: int
1919
"""远程节点使用的内存资源(单位: byte)"""
20-
cwd: str = ""
20+
cwd: str
2121
"""远程节点的工作路径"""
2222

2323

2424
class InstanceInfo(BaseModel):
2525
"""实例统计信息"""
2626

27-
running: int = 0
27+
running: int
2828
"""运行中实例数量"""
29-
total: int = 0
29+
total: int
3030
"""全部实例数量"""

mcsmapi/models/daemon.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,59 @@
77
class SystemInfo(BaseModel):
88
"""节点系统信息"""
99

10-
type: str = ""
10+
type: str
1111
"""系统类型"""
12-
hostname: str = ""
12+
hostname: str
1313
"""主机名"""
14-
platform: str = ""
14+
platform: str
1515
"""平台架构"""
16-
release: str = ""
16+
release: str
1717
"""系统版本"""
18-
uptime: float = 0
18+
uptime: float
1919
"""系统运行时间(单位: sec)"""
20-
cwd: str = ""
20+
cwd: str
2121
"""远程节点运行路径"""
22-
loadavg: list[float] = []
22+
loadavg: list[float]
2323
"""系统负载平均值(仅适用于 Linux 和 macOS),表示过去 **1 分钟、5 分钟、15 分钟** 内的 CPU 负载情况"""
24-
freemem: int = 0
24+
freemem: int
2525
"""可用内存(单位: byte)"""
26-
cpuUsage: float = 0
26+
cpuUsage: float
2727
"""cpu使用率"""
28-
memUsage: float = 0
28+
memUsage: float
2929
"""内存使用率"""
30-
totalmem: int = 0
30+
totalmem: int
3131
"""内存总量(单位: byte)"""
32-
processCpu: int = 0
32+
processCpu: int
3333
"""未知"""
34-
processMem: int = 0
34+
processMem: int
3535
"""未知"""
3636

3737

3838
class DaemonModel(BaseModel):
3939
"""节点详细信息"""
4040

41-
version: str = ""
41+
version: str
4242
"""远程节点版本"""
43-
process: ProcessInfo = ProcessInfo()
43+
process: ProcessInfo
4444
"""远程节点的基本信息"""
45-
instance: InstanceInfo = InstanceInfo()
45+
instance: InstanceInfo
4646
"""远程节点实例基本信息"""
47-
system: SystemInfo = SystemInfo()
47+
system: SystemInfo
4848
"""远程节点系统信息"""
49-
cpuMemChart: list[CpuMemChart] = []
49+
cpuMemChart: list[CpuMemChart]
5050
"""cpu和内存使用趋势"""
51-
uuid: str = ""
51+
uuid: str
5252
"""远程节点的uuid"""
53-
ip: str = ""
53+
ip: str
5454
"""远程节点的ip"""
55-
port: int = 24444
55+
port: int
5656
"""远程节点的端口"""
57-
prefix: str = ""
57+
prefix: str
5858
"""远程节点的路径前缀"""
59-
available: bool = False
59+
available: bool
6060
"""远程节点的可用状态"""
61-
remarks: str = ""
62-
"""远程节点的备注"""
61+
remarks: str
62+
"""远程节点的名称"""
6363

6464
def delete(self) -> bool:
6565
"""

mcsmapi/models/file.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ class FileType(IntEnum):
1111
class FileItem(BaseModel):
1212
"""文件信息"""
1313

14-
name: str = ""
14+
name: str
1515
"""文件名称"""
16-
size: int = 0
16+
size: int
1717
"""文件大小(单位: byte)"""
18-
time: str = ""
18+
time: str
1919
"""文件修改时间"""
20-
mode: int = 777
20+
mode: int
2121
"""文件操作权限(仅适用于Linux)"""
22-
type: FileType = FileType.FOLDER
22+
type: FileType
2323
"""文件类型"""
2424
daemonId: str = ""
2525
"""远程节点uuid"""
2626
uuid: str = ""
2727
"""实例的uiid"""
2828
target: str = ""
2929
"""文件所在路径"""
30-
file_name: str = ""
30+
file_name: str
3131
"""当前文件列表过滤条件"""
3232

3333
def rename(self, newName: str) -> bool:
@@ -160,19 +160,19 @@ class FileList(BaseModel):
160160

161161
items: list[FileItem]
162162
"""文件信息列表"""
163-
page: int = 0
163+
page: int
164164
"""当前页数"""
165-
pageSize: int = 100
165+
pageSize: int
166166
"""文件列表单页大小"""
167-
total: int = 0
167+
total: int
168168
"""总页数"""
169-
absolutePath: str = "\\"
169+
absolutePath: str
170170
"""当前路径在远程节点的绝对路径"""
171-
daemonId: str = ""
171+
daemonId: str
172172
"""远程节点uuid"""
173-
uuid: str = ""
173+
uuid: str
174174
"""实例uuid"""
175-
target: str = ""
175+
target: str
176176
"""文件(名称或目录)路径"""
177177

178178
def __init__(self, **data: str):
@@ -226,9 +226,9 @@ def createFolder(self, target: str) -> bool:
226226
return File.createFolder(self.daemonId, self.uuid, target)
227227

228228

229-
class CommonConfig(BaseModel):
229+
class FileDownloadConfig(BaseModel):
230230

231-
password: str = ""
231+
password: str
232232
"""文件下载密码"""
233-
addr: str = ""
233+
addr: str
234234
"""文件下载地址"""

mcsmapi/models/instance.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,27 +111,27 @@ class InstanceConfig(BaseModel):
111111
class ProcessInfo(BaseModel):
112112
"""进程信息"""
113113

114-
cpu: int = 0
114+
cpu: int
115115
"""CPU 使用率 (单位: %)"""
116-
memory: int = 0
116+
memory: int
117117
"""进程占用内存 (单位: KB)"""
118-
ppid: int = 0
118+
ppid: int
119119
"""父进程 ID"""
120-
pid: int = 0
120+
pid: int
121121
"""进程 ID"""
122-
ctime: int = 0
122+
ctime: int
123123
"""进程创建时间 (Unix 时间戳)"""
124-
elapsed: int = 0
124+
elapsed: int
125125
"""进程运行时长 (单位: 秒)"""
126-
timestamp: int = 0
126+
timestamp: int
127127
"""时间戳"""
128128

129129

130130
class InstanceInfo(BaseModel):
131131
"""实例运行状态信息(这些选项在新版中已不再支持设置,但仍在API中返回)"""
132132

133-
currentPlayers: int = -1
134-
"""当前玩家数量 (-1 表示未知)"""
133+
currentPlayers: int
134+
"""当前玩家数量"""
135135
fileLock: int = 0
136136
"""文件锁状态 (0: 无锁)"""
137137
maxPlayers: int = -1
@@ -147,21 +147,19 @@ class InstanceInfo(BaseModel):
147147
class InstanceDetail(BaseModel):
148148
"""实例详细信息"""
149149

150-
config: InstanceConfig = InstanceConfig()
150+
config: InstanceConfig
151151
"""实例的配置信息"""
152-
info: InstanceInfo = InstanceInfo()
152+
info: InstanceInfo
153153
"""实例的运行状态信息"""
154-
daemonId: str = ""
154+
daemonId: str
155155
"""所属的守护进程 (Daemon) ID"""
156-
instanceUuid: str = ""
156+
instanceUuid: str
157157
"""实例唯一标识符 (UUID)"""
158-
processInfo: ProcessInfo = ProcessInfo()
158+
processInfo: ProcessInfo
159159
"""实例的进程信息"""
160-
space: int = 0
161-
"""实例的存储空间大小(已弃用)"""
162-
started: int = 0
160+
started: int
163161
"""实例的启动次数"""
164-
status: Status = Status.STOP
162+
status: Status
165163
"""实例状态"""
166164

167165
def start(self) -> str | bool:

0 commit comments

Comments
 (0)