Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions mcsmapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Literal
import urllib.parse
from mcsmapi.models.overview import OverviewModel
from mcsmapi.pool import ApiPool
from mcsmapi.apis.file import File
from mcsmapi.apis.user import User
Expand All @@ -11,12 +11,14 @@


class MCSMAPI:
authentication: Literal["apikey", "account"]
"""认证类型"""

def __init__(self, url: str, timeout: int = 5) -> None:
split_url = urllib.parse.urlsplit(url)
Request.set_mcsm_url(
urllib.parse.urljoin(f"{split_url.scheme}://{split_url.netloc}", "")
)
self.authentication = None
Request.set_timeout(timeout)

def login(self, username: str, password: str) -> "MCSMAPI":
Expand All @@ -35,8 +37,8 @@ def login_with_apikey(self, apikey: str):
self.authentication = "apikey"
return self

def overview(self) -> OverviewModel:
return Overview.init()
def overview(self) -> Overview:
return Overview()

def instance(self) -> Instance:
return Instance()
Expand Down
6 changes: 3 additions & 3 deletions mcsmapi/apis/file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from mcsmapi.pool import ApiPool
from mcsmapi.request import Request, send, upload
from mcsmapi.models.file import CommonConfig, FileList
from mcsmapi.models.file import FileDownloadConfig, FileList
import urllib.parse
import os

Expand Down Expand Up @@ -103,7 +103,7 @@ def download(daemonId: str, uuid: str, file_name: str) -> str:
f"{ApiPool.FILE}/download",
params={"daemonId": daemonId, "uuid": uuid, "file_name": file_name},
)
result = CommonConfig(**result)
result = FileDownloadConfig(**result)
protocol = Request.mcsm_url.split("://")[0]
base_url = urllib.parse.urljoin(f"{protocol}://{result.addr}", "download")
return urllib.parse.urljoin(base_url, f"{result.password}/{file_name}")
Expand All @@ -127,7 +127,7 @@ async def upload(daemonId: str, uuid: str, file: bytes, upload_dir: str) -> bool
f"{ApiPool.FILE}/upload",
params={"daemonId": daemonId, "uuid": uuid, "upload_dir": upload_dir},
)
result = CommonConfig(**result)
result = FileDownloadConfig(**result)
protocol = Request.mcsm_url.split("://")[0]
base_url = urllib.parse.urljoin(f"{protocol}://{result.addr}", "upload")
final_url = urllib.parse.urljoin(base_url, result.password)
Expand Down
19 changes: 11 additions & 8 deletions mcsmapi/apis/overview.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from mcsmapi.pool import ApiPool
from mcsmapi.request import send
from mcsmapi.models.overview import OverviewModel
from mcsmapi.models.overview import OverviewModel, LogDetail


class Overview:
@staticmethod
def init():
def overview():
"""
初始化方法,用于获取API概览信息并构建概览模型。

本方法通过发送GET请求获取API概览信息,确保返回的数据类型为字典,
然后使用这些数据来构建一个OverviewModel实例。

:return: 返回一个OverviewModel实例,该实例使用获取的API概览信息进行初始化。
获取面板基本信息
"""
result = send("GET", ApiPool.OVERVIEW)
return OverviewModel(**result)

@staticmethod
def logs():
"""
获取面板操作日志
"""
result = send("GET", ApiPool.LOG)
return [LogDetail(**item) for item in result]
14 changes: 7 additions & 7 deletions mcsmapi/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
class CpuMemChart(BaseModel):
"""节点资源使用率信息"""

cpu: float = 0
cpu: float
"""cpu使用率"""
mem: float = 0
mem: float
"""内存使用率"""


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

cpu: int = 0
cpu: int
"""远程节点使用的cpu资源(单位: byte)"""
memory: int = 0
memory: int
"""远程节点使用的内存资源(单位: byte)"""
cwd: str = ""
cwd: str
"""远程节点的工作路径"""


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

running: int = 0
running: int
"""运行中实例数量"""
total: int = 0
total: int
"""全部实例数量"""
50 changes: 25 additions & 25 deletions mcsmapi/models/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,59 @@
class SystemInfo(BaseModel):
"""节点系统信息"""

type: str = ""
type: str
"""系统类型"""
hostname: str = ""
hostname: str
"""主机名"""
platform: str = ""
platform: str
"""平台架构"""
release: str = ""
release: str
"""系统版本"""
uptime: float = 0
uptime: float
"""系统运行时间(单位: sec)"""
cwd: str = ""
cwd: str
"""远程节点运行路径"""
loadavg: list[float] = []
loadavg: list[float]
"""系统负载平均值(仅适用于 Linux 和 macOS),表示过去 **1 分钟、5 分钟、15 分钟** 内的 CPU 负载情况"""
freemem: int = 0
freemem: int
"""可用内存(单位: byte)"""
cpuUsage: float = 0
cpuUsage: float
"""cpu使用率"""
memUsage: float = 0
memUsage: float
"""内存使用率"""
totalmem: int = 0
totalmem: int
"""内存总量(单位: byte)"""
processCpu: int = 0
processCpu: int
"""未知"""
processMem: int = 0
processMem: int
"""未知"""


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

version: str = ""
version: str
"""远程节点版本"""
process: ProcessInfo = ProcessInfo()
process: ProcessInfo
"""远程节点的基本信息"""
instance: InstanceInfo = InstanceInfo()
instance: InstanceInfo
"""远程节点实例基本信息"""
system: SystemInfo = SystemInfo()
system: SystemInfo
"""远程节点系统信息"""
cpuMemChart: list[CpuMemChart] = []
cpuMemChart: list[CpuMemChart]
"""cpu和内存使用趋势"""
uuid: str = ""
uuid: str
"""远程节点的uuid"""
ip: str = ""
ip: str
"""远程节点的ip"""
port: int = 24444
port: int
"""远程节点的端口"""
prefix: str = ""
prefix: str
"""远程节点的路径前缀"""
available: bool = False
available: bool
"""远程节点的可用状态"""
remarks: str = ""
"""远程节点的备注"""
remarks: str
"""远程节点的名称"""

def delete(self) -> bool:
"""
Expand Down
32 changes: 16 additions & 16 deletions mcsmapi/models/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ class FileType(IntEnum):
class FileItem(BaseModel):
"""文件信息"""

name: str = ""
name: str
"""文件名称"""
size: int = 0
size: int
"""文件大小(单位: byte)"""
time: str = ""
time: str
"""文件修改时间"""
mode: int = 777
mode: int
"""文件操作权限(仅适用于Linux)"""
type: FileType = FileType.FOLDER
type: FileType
"""文件类型"""
daemonId: str = ""
"""远程节点uuid"""
uuid: str = ""
"""实例的uiid"""
target: str = ""
"""文件所在路径"""
file_name: str = ""
file_name: str
"""当前文件列表过滤条件"""

def rename(self, newName: str) -> bool:
Expand Down Expand Up @@ -160,19 +160,19 @@ class FileList(BaseModel):

items: list[FileItem]
"""文件信息列表"""
page: int = 0
page: int
"""当前页数"""
pageSize: int = 100
pageSize: int
"""文件列表单页大小"""
total: int = 0
total: int
"""总页数"""
absolutePath: str = "\\"
absolutePath: str
"""当前路径在远程节点的绝对路径"""
daemonId: str = ""
daemonId: str
"""远程节点uuid"""
uuid: str = ""
uuid: str
"""实例uuid"""
target: str = ""
target: str
"""文件(名称或目录)路径"""

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


class CommonConfig(BaseModel):
class FileDownloadConfig(BaseModel):

password: str = ""
password: str
"""文件下载密码"""
addr: str = ""
addr: str
"""文件下载地址"""
34 changes: 16 additions & 18 deletions mcsmapi/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,27 @@ class InstanceConfig(BaseModel):
class ProcessInfo(BaseModel):
"""进程信息"""

cpu: int = 0
cpu: int
"""CPU 使用率 (单位: %)"""
memory: int = 0
memory: int
"""进程占用内存 (单位: KB)"""
ppid: int = 0
ppid: int
"""父进程 ID"""
pid: int = 0
pid: int
"""进程 ID"""
ctime: int = 0
ctime: int
"""进程创建时间 (Unix 时间戳)"""
elapsed: int = 0
elapsed: int
"""进程运行时长 (单位: 秒)"""
timestamp: int = 0
timestamp: int
"""时间戳"""


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

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

config: InstanceConfig = InstanceConfig()
config: InstanceConfig
"""实例的配置信息"""
info: InstanceInfo = InstanceInfo()
info: InstanceInfo
"""实例的运行状态信息"""
daemonId: str = ""
daemonId: str
"""所属的守护进程 (Daemon) ID"""
instanceUuid: str = ""
instanceUuid: str
"""实例唯一标识符 (UUID)"""
processInfo: ProcessInfo = ProcessInfo()
processInfo: ProcessInfo
"""实例的进程信息"""
space: int = 0
"""实例的存储空间大小(已弃用)"""
started: int = 0
started: int
"""实例的启动次数"""
status: Status = Status.STOP
status: Status
"""实例状态"""

def start(self) -> str | bool:
Expand Down
Loading