mcsm-sdk-python 1.0.6__tar.gz

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.
@@ -0,0 +1,285 @@
1
+ Metadata-Version: 2.4
2
+ Name: mcsm_sdk_python
3
+ Version: 1.0.6
4
+ Summary: An unoffical SDK for the MCSManager
5
+ Requires-Python: >=3.9
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: build>=1.4.0
8
+ Requires-Dist: httpx>=0.28.1
9
+ Requires-Dist: ruff>=0.13.3
10
+ Requires-Dist: twine>=6.2.0
11
+
12
+ # MCSM SDK Python
13
+
14
+ 一个用于 MCSManager 的非官方 Python SDK,提供简单易用的 API 接口来管理 Minecraft 服务器实例。
15
+
16
+ ## 特性
17
+
18
+ - 🚀 异步支持,基于 `httpx` 构建
19
+ - 📊 完整的仪表板信息获取
20
+ - 👥 用户管理功能(创建、查询、更新、删除)
21
+ - 🎮 实例管理功能(创建、启动、停止、重启等)
22
+ - 📝 类型提示支持
23
+ - 🔧 简单易用的 API 设计
24
+
25
+ ## 安装
26
+
27
+ ### 使用 pip 安装
28
+
29
+ ```bash
30
+ pip install mcsm-sdk-python
31
+ ```
32
+
33
+ ### 从源码安装
34
+
35
+ ```bash
36
+ git clone https://github.com/laobinghu/mcsm-sdk-python.git
37
+ cd mcsm-sdk-python
38
+ pip install -e .
39
+ ```
40
+
41
+ ## 快速开始
42
+
43
+ ```python
44
+ import asyncio
45
+ from httpx import AsyncClient
46
+ from mcsm_sdk_python import McsmSdk
47
+
48
+ async def main():
49
+ # 创建 HTTP 客户端
50
+ http_client = AsyncClient()
51
+
52
+ # 初始化 MCSM SDK
53
+ mcsm = McsmSdk(
54
+ client=http_client,
55
+ endpoint="http://127.0.0.1:2333", # 你的 MCSManager 地址
56
+ apikey="your_api_key_here" # 你的 API 密钥
57
+ )
58
+
59
+ try:
60
+ # 获取系统概览信息
61
+ overview = await mcsm.get_overview()
62
+ print("系统概览:", overview)
63
+
64
+ # 获取用户列表
65
+ users = await mcsm.get_user_list(page=1, page_size=10)
66
+ print("用户列表:", users)
67
+
68
+ finally:
69
+ # 关闭 HTTP 客户端
70
+ await http_client.aclose()
71
+
72
+ # 运行示例
73
+ asyncio.run(main())
74
+ ```
75
+
76
+ ## API 文档
77
+
78
+ ### 初始化
79
+
80
+ ```python
81
+ from httpx import AsyncClient
82
+ from mcsm_sdk_python import McsmSdk
83
+
84
+ client = AsyncClient()
85
+ mcsm = McsmSdk(
86
+ client=client,
87
+ endpoint="http://your-mcsm-server:2333",
88
+ apikey="your_api_key"
89
+ )
90
+ ```
91
+
92
+ ### 仪表板 API
93
+
94
+ #### 获取系统概览
95
+
96
+ ```python
97
+ overview = await mcsm.get_overview()
98
+ ```
99
+
100
+ 返回系统的基本信息,包括服务器状态、实例数量等。
101
+
102
+ ### 用户管理 API
103
+
104
+ #### 获取用户列表
105
+
106
+ ```python
107
+ users = await mcsm.get_user_list(
108
+ page=1, # 页码
109
+ page_size=10, # 每页数量
110
+ userName="", # 可选:用户名筛选
111
+ role=1 # 可选:角色筛选
112
+ )
113
+ ```
114
+
115
+ #### 创建用户
116
+
117
+ ```python
118
+ result = await mcsm.create_user(
119
+ username="new_user",
120
+ password="password123",
121
+ permission=1 # 权限级别
122
+ )
123
+ ```
124
+
125
+ #### 更新用户信息
126
+
127
+ ```python
128
+ result = await mcsm.update_user({
129
+ "uuid": "user_uuid",
130
+ "username": "updated_name",
131
+ "permission": 2
132
+ })
133
+ ```
134
+
135
+ #### 删除用户
136
+
137
+ ```python
138
+ result = await mcsm.delete_user(["user_uuid_1", "user_uuid_2"])
139
+ ```
140
+
141
+ ### 实例管理 API
142
+
143
+ #### 获取实例列表
144
+
145
+ ```python
146
+ instances = await mcsm.get_instances_list(
147
+ daemonId="daemon_id",
148
+ page=1,
149
+ page_size=10,
150
+ status="running", # 状态筛选
151
+ instance_name="" # 可选:实例名称筛选
152
+ )
153
+ ```
154
+
155
+ #### 获取实例详细信息
156
+
157
+ ```python
158
+ instance_info = await mcsm.get_instances_info(
159
+ daemonId="daemon_id",
160
+ page=1,
161
+ page_size=10,
162
+ status="running",
163
+ instance_id="instance_uuid"
164
+ )
165
+ ```
166
+
167
+ #### 创建实例
168
+
169
+ ```python
170
+ result = await mcsm.create_instance(
171
+ daemonId="daemon_id",
172
+ data={
173
+ "nickname": "我的服务器",
174
+ "startCommand": "java -jar server.jar",
175
+ "stopCommand": "stop",
176
+ "cwd": "/path/to/server",
177
+ "ie": "utf-8",
178
+ "oe": "utf-8"
179
+ }
180
+ )
181
+ ```
182
+
183
+ #### 实例操作
184
+
185
+ 支持单个实例和批量实例操作:
186
+
187
+ ```python
188
+ # 单个实例操作
189
+ result = await mcsm.instances_operate(
190
+ method="start", # 操作类型:start, stop, restart, kill
191
+ daemonId="daemon_id",
192
+ instance_id="instance_uuid"
193
+ )
194
+
195
+ # 批量实例操作
196
+ result = await mcsm.instances_operate(
197
+ method="start",
198
+ isBatch=True,
199
+ instance_list=[
200
+ {
201
+ "daemonId": "daemon-123",
202
+ "instanceUuids": ["uuid-1", "uuid-2", "uuid-3"]
203
+ },
204
+ {
205
+ "daemonId": "daemon-456",
206
+ "instanceUuids": ["uuid-4", "uuid-5"]
207
+ }
208
+ ]
209
+ )
210
+ ```
211
+
212
+ 支持的操作类型:
213
+ - `start` - 启动实例
214
+ - `stop` - 停止实例
215
+ - `restart` - 重启实例
216
+ - `kill` - 强制终止实例
217
+
218
+ #### 更新实例
219
+
220
+ ```python
221
+ result = await mcsm.update_instance(
222
+ daemonId="daemon_id",
223
+ uuid="instance_uuid"
224
+ )
225
+ ```
226
+
227
+ ## 错误处理
228
+
229
+ SDK 使用 `httpx` 的异常处理机制:
230
+
231
+ ```python
232
+ import httpx
233
+
234
+ try:
235
+ result = await mcsm.get_overview()
236
+ except httpx.HTTPStatusError as e:
237
+ print(f"HTTP 错误: {e.response.status_code}")
238
+ print(f"响应内容: {e.response.text}")
239
+ except httpx.RequestError as e:
240
+ print(f"请求错误: {e}")
241
+ ```
242
+
243
+ ## 配置要求
244
+
245
+ - Python 3.9+
246
+ - httpx >= 0.28.1
247
+
248
+ ## 项目结构
249
+
250
+ ```
251
+ mcsm_sdk_python/
252
+ ├── __init__.py # 主入口,导出 McsmSdk 类
253
+ ├── base.py # 基础类,提供通用功能
254
+ ├── dashboard.py # 仪表板相关 API
255
+ ├── users.py # 用户管理 API
256
+ ├── instance.py # 实例管理 API
257
+ ├── daemon.py # 守护进程 API(待实现)
258
+ ├── fileManager.py # 文件管理 API(待实现)
259
+ └── imageManager.py # 镜像管理 API(待实现)
260
+ ```
261
+
262
+ ## 开发计划
263
+
264
+ - [ ] 守护进程管理 API
265
+ - [ ] 文件管理 API
266
+ - [ ] 镜像管理 API
267
+ - [ ] 单元测试
268
+ - [ ] 文档完善
269
+
270
+ ## 贡献
271
+
272
+ 欢迎提交 Issue 和 Pull Request!
273
+
274
+ ## 许可证
275
+
276
+ MIT License
277
+
278
+ ## 相关链接
279
+
280
+ - [MCSManager 官方网站](https://mcsmanager.com/)
281
+ - [MCSManager GitHub](https://github.com/MCSManager/MCSManager)
282
+
283
+ ---
284
+
285
+ **注意**: 这是一个非官方的 SDK,与 MCSManager 官方团队无关。使用前请确保你的 MCSManager 版本支持相应的 API。
@@ -0,0 +1,274 @@
1
+ # MCSM SDK Python
2
+
3
+ 一个用于 MCSManager 的非官方 Python SDK,提供简单易用的 API 接口来管理 Minecraft 服务器实例。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 异步支持,基于 `httpx` 构建
8
+ - 📊 完整的仪表板信息获取
9
+ - 👥 用户管理功能(创建、查询、更新、删除)
10
+ - 🎮 实例管理功能(创建、启动、停止、重启等)
11
+ - 📝 类型提示支持
12
+ - 🔧 简单易用的 API 设计
13
+
14
+ ## 安装
15
+
16
+ ### 使用 pip 安装
17
+
18
+ ```bash
19
+ pip install mcsm-sdk-python
20
+ ```
21
+
22
+ ### 从源码安装
23
+
24
+ ```bash
25
+ git clone https://github.com/laobinghu/mcsm-sdk-python.git
26
+ cd mcsm-sdk-python
27
+ pip install -e .
28
+ ```
29
+
30
+ ## 快速开始
31
+
32
+ ```python
33
+ import asyncio
34
+ from httpx import AsyncClient
35
+ from mcsm_sdk_python import McsmSdk
36
+
37
+ async def main():
38
+ # 创建 HTTP 客户端
39
+ http_client = AsyncClient()
40
+
41
+ # 初始化 MCSM SDK
42
+ mcsm = McsmSdk(
43
+ client=http_client,
44
+ endpoint="http://127.0.0.1:2333", # 你的 MCSManager 地址
45
+ apikey="your_api_key_here" # 你的 API 密钥
46
+ )
47
+
48
+ try:
49
+ # 获取系统概览信息
50
+ overview = await mcsm.get_overview()
51
+ print("系统概览:", overview)
52
+
53
+ # 获取用户列表
54
+ users = await mcsm.get_user_list(page=1, page_size=10)
55
+ print("用户列表:", users)
56
+
57
+ finally:
58
+ # 关闭 HTTP 客户端
59
+ await http_client.aclose()
60
+
61
+ # 运行示例
62
+ asyncio.run(main())
63
+ ```
64
+
65
+ ## API 文档
66
+
67
+ ### 初始化
68
+
69
+ ```python
70
+ from httpx import AsyncClient
71
+ from mcsm_sdk_python import McsmSdk
72
+
73
+ client = AsyncClient()
74
+ mcsm = McsmSdk(
75
+ client=client,
76
+ endpoint="http://your-mcsm-server:2333",
77
+ apikey="your_api_key"
78
+ )
79
+ ```
80
+
81
+ ### 仪表板 API
82
+
83
+ #### 获取系统概览
84
+
85
+ ```python
86
+ overview = await mcsm.get_overview()
87
+ ```
88
+
89
+ 返回系统的基本信息,包括服务器状态、实例数量等。
90
+
91
+ ### 用户管理 API
92
+
93
+ #### 获取用户列表
94
+
95
+ ```python
96
+ users = await mcsm.get_user_list(
97
+ page=1, # 页码
98
+ page_size=10, # 每页数量
99
+ userName="", # 可选:用户名筛选
100
+ role=1 # 可选:角色筛选
101
+ )
102
+ ```
103
+
104
+ #### 创建用户
105
+
106
+ ```python
107
+ result = await mcsm.create_user(
108
+ username="new_user",
109
+ password="password123",
110
+ permission=1 # 权限级别
111
+ )
112
+ ```
113
+
114
+ #### 更新用户信息
115
+
116
+ ```python
117
+ result = await mcsm.update_user({
118
+ "uuid": "user_uuid",
119
+ "username": "updated_name",
120
+ "permission": 2
121
+ })
122
+ ```
123
+
124
+ #### 删除用户
125
+
126
+ ```python
127
+ result = await mcsm.delete_user(["user_uuid_1", "user_uuid_2"])
128
+ ```
129
+
130
+ ### 实例管理 API
131
+
132
+ #### 获取实例列表
133
+
134
+ ```python
135
+ instances = await mcsm.get_instances_list(
136
+ daemonId="daemon_id",
137
+ page=1,
138
+ page_size=10,
139
+ status="running", # 状态筛选
140
+ instance_name="" # 可选:实例名称筛选
141
+ )
142
+ ```
143
+
144
+ #### 获取实例详细信息
145
+
146
+ ```python
147
+ instance_info = await mcsm.get_instances_info(
148
+ daemonId="daemon_id",
149
+ page=1,
150
+ page_size=10,
151
+ status="running",
152
+ instance_id="instance_uuid"
153
+ )
154
+ ```
155
+
156
+ #### 创建实例
157
+
158
+ ```python
159
+ result = await mcsm.create_instance(
160
+ daemonId="daemon_id",
161
+ data={
162
+ "nickname": "我的服务器",
163
+ "startCommand": "java -jar server.jar",
164
+ "stopCommand": "stop",
165
+ "cwd": "/path/to/server",
166
+ "ie": "utf-8",
167
+ "oe": "utf-8"
168
+ }
169
+ )
170
+ ```
171
+
172
+ #### 实例操作
173
+
174
+ 支持单个实例和批量实例操作:
175
+
176
+ ```python
177
+ # 单个实例操作
178
+ result = await mcsm.instances_operate(
179
+ method="start", # 操作类型:start, stop, restart, kill
180
+ daemonId="daemon_id",
181
+ instance_id="instance_uuid"
182
+ )
183
+
184
+ # 批量实例操作
185
+ result = await mcsm.instances_operate(
186
+ method="start",
187
+ isBatch=True,
188
+ instance_list=[
189
+ {
190
+ "daemonId": "daemon-123",
191
+ "instanceUuids": ["uuid-1", "uuid-2", "uuid-3"]
192
+ },
193
+ {
194
+ "daemonId": "daemon-456",
195
+ "instanceUuids": ["uuid-4", "uuid-5"]
196
+ }
197
+ ]
198
+ )
199
+ ```
200
+
201
+ 支持的操作类型:
202
+ - `start` - 启动实例
203
+ - `stop` - 停止实例
204
+ - `restart` - 重启实例
205
+ - `kill` - 强制终止实例
206
+
207
+ #### 更新实例
208
+
209
+ ```python
210
+ result = await mcsm.update_instance(
211
+ daemonId="daemon_id",
212
+ uuid="instance_uuid"
213
+ )
214
+ ```
215
+
216
+ ## 错误处理
217
+
218
+ SDK 使用 `httpx` 的异常处理机制:
219
+
220
+ ```python
221
+ import httpx
222
+
223
+ try:
224
+ result = await mcsm.get_overview()
225
+ except httpx.HTTPStatusError as e:
226
+ print(f"HTTP 错误: {e.response.status_code}")
227
+ print(f"响应内容: {e.response.text}")
228
+ except httpx.RequestError as e:
229
+ print(f"请求错误: {e}")
230
+ ```
231
+
232
+ ## 配置要求
233
+
234
+ - Python 3.9+
235
+ - httpx >= 0.28.1
236
+
237
+ ## 项目结构
238
+
239
+ ```
240
+ mcsm_sdk_python/
241
+ ├── __init__.py # 主入口,导出 McsmSdk 类
242
+ ├── base.py # 基础类,提供通用功能
243
+ ├── dashboard.py # 仪表板相关 API
244
+ ├── users.py # 用户管理 API
245
+ ├── instance.py # 实例管理 API
246
+ ├── daemon.py # 守护进程 API(待实现)
247
+ ├── fileManager.py # 文件管理 API(待实现)
248
+ └── imageManager.py # 镜像管理 API(待实现)
249
+ ```
250
+
251
+ ## 开发计划
252
+
253
+ - [ ] 守护进程管理 API
254
+ - [ ] 文件管理 API
255
+ - [ ] 镜像管理 API
256
+ - [ ] 单元测试
257
+ - [ ] 文档完善
258
+
259
+ ## 贡献
260
+
261
+ 欢迎提交 Issue 和 Pull Request!
262
+
263
+ ## 许可证
264
+
265
+ MIT License
266
+
267
+ ## 相关链接
268
+
269
+ - [MCSManager 官方网站](https://mcsmanager.com/)
270
+ - [MCSManager GitHub](https://github.com/MCSManager/MCSManager)
271
+
272
+ ---
273
+
274
+ **注意**: 这是一个非官方的 SDK,与 MCSManager 官方团队无关。使用前请确保你的 MCSManager 版本支持相应的 API。
@@ -0,0 +1,15 @@
1
+ from httpx import AsyncClient
2
+
3
+ from .dashboard import Dashboard
4
+ from .users import Users
5
+ from .instance import Instance
6
+
7
+
8
+ __all__ = ["McsmSdk"]
9
+
10
+
11
+ class McsmSdk(Dashboard, Users, Instance):
12
+ def __init__(self, client: AsyncClient, endpoint: str, apikey: str = ""):
13
+ super().__init__(client, endpoint, apikey)
14
+
15
+ pass
@@ -0,0 +1,57 @@
1
+ from abc import ABC
2
+ from httpx import AsyncClient, Request, Response
3
+ from urllib.parse import urljoin
4
+ from typing import Union
5
+
6
+
7
+ class Base(ABC):
8
+ def __init__(self, client: AsyncClient, endpoint: str, apikey: str, **kwargs):
9
+ self.client = client
10
+ self.endpoint = endpoint
11
+ self.apikey = apikey
12
+ # 可以在这里处理其他通用参数
13
+ self.extra_kwargs = kwargs
14
+
15
+ def _get_headers(self) -> dict:
16
+ """构建基础请求头,包含认证信息和内容类型"""
17
+ headers = {
18
+ "Content-Type": "application/json",
19
+ "X-Requested-With": "XMLHttpRequest",
20
+ "charset": "utf-8",
21
+ }
22
+ return headers
23
+
24
+ def _get_full_url(self, path: str) -> str:
25
+ """拼接基础地址和路径,生成完整URL"""
26
+ return urljoin(self.endpoint, path)
27
+
28
+ async def delete_with_body(
29
+ self,
30
+ url: str,
31
+ params: dict,
32
+ data: Union[list, dict],
33
+ ) -> Response:
34
+ """
35
+ 发送带有body的DELETE请求
36
+
37
+ 参数:
38
+ url: API路径(相对于endpoint)
39
+ params: 请求参数
40
+ payload: 要发送的请求体数据(列表???)
41
+
42
+ 返回:
43
+ httpx.Response对象
44
+ """
45
+ # 准备请求数据
46
+ full_url = self._get_full_url(url)
47
+ headers = self._get_headers()
48
+
49
+ # 构建请求对象
50
+ request = Request(
51
+ method="DELETE", url=full_url, content=data, headers=headers, params=params
52
+ )
53
+
54
+ # 发送请求
55
+ response = await self.client.send(request)
56
+ response.raise_for_status() # 自动处理HTTP错误状态码
57
+ return response
File without changes
@@ -0,0 +1,15 @@
1
+ from httpx import AsyncClient
2
+ from urllib.parse import urljoin
3
+
4
+ from .base import Base
5
+
6
+
7
+ class Dashboard(Base):
8
+ def __init__(self, client: AsyncClient, endpoint: str, apikey: str, **kwargs):
9
+ super().__init__(client, endpoint, apikey, **kwargs)
10
+
11
+ async def get_overview(self) -> dict:
12
+ final_url = urljoin(self.endpoint, f"/api/overview?apikey={self.apikey}")
13
+ response = await self.client.get(final_url)
14
+ response.raise_for_status()
15
+ return response.json()
File without changes
File without changes
@@ -0,0 +1,219 @@
1
+ from httpx import AsyncClient
2
+ from urllib.parse import urljoin
3
+
4
+ from .base import Base
5
+
6
+
7
+ class Instance(Base):
8
+ def __init__(self, client: AsyncClient, endpoint: str, apikey: str, **kwargs):
9
+ super().__init__(client, endpoint, apikey, **kwargs)
10
+
11
+ async def get_instances_list(
12
+ self,
13
+ daemonId: str,
14
+ page: int,
15
+ page_size: int,
16
+ status: str,
17
+ instance_name: str = "",
18
+ ) -> dict:
19
+ params = {
20
+ "apikey": self.apikey,
21
+ "page": page,
22
+ "page_size": page_size,
23
+ "daemonId": daemonId,
24
+ "status": status,
25
+ **({"instance_name": instance_name} if instance_name != "" else {}),
26
+ }
27
+
28
+ final_url = urljoin(
29
+ self.endpoint,
30
+ "/api/service/remote_service_instances",
31
+ )
32
+ response = await self.client.get(final_url, params=params)
33
+ response.raise_for_status()
34
+ return response.json()
35
+
36
+ async def get_instances_info(
37
+ self,
38
+ daemonId: str,
39
+ page: int,
40
+ page_size: int,
41
+ status: str,
42
+ instance_id: str = "",
43
+ ) -> dict:
44
+ params = {
45
+ "apikey": self.apikey,
46
+ "daemonId": daemonId,
47
+ "uuid": instance_id,
48
+ }
49
+
50
+ final_url = urljoin(
51
+ self.endpoint,
52
+ "/api/instance",
53
+ )
54
+ response = await self.client.get(final_url, params=params)
55
+ response.raise_for_status()
56
+ return response.json()
57
+
58
+ async def create_instance(self, daemonId: str, data: dict) -> dict:
59
+ params = {"apikey": self.apikey, "daemonId": daemonId}
60
+ final_url = urljoin(
61
+ self.endpoint,
62
+ "/api/instance",
63
+ )
64
+ response = await self.client.post(final_url, params=params, data=data)
65
+ response.raise_for_status()
66
+ return response.json()
67
+
68
+ async def update_instance_config(self, daemonId: str, data: dict) -> dict:
69
+ params = {"apikey": self.apikey, "daemonId": daemonId}
70
+ final_url = urljoin(
71
+ self.endpoint,
72
+ "/api/instance",
73
+ )
74
+ response = await self.client.post(final_url, params=params, data=data)
75
+ response.raise_for_status()
76
+ return response.json()
77
+
78
+ async def delete_user(
79
+ self, daemonId: str, uuids: list[str], deleteFile: bool = False
80
+ ) -> dict:
81
+ data = {"uuids": uuids, "deleteFile": deleteFile}
82
+ params = {"apikey": self.apikey, "daemonId": daemonId}
83
+ final_url = urljoin(
84
+ self.endpoint,
85
+ "/api/auth",
86
+ )
87
+ response = await self.delete_with_body(final_url, params=params, data=data)
88
+ response.raise_for_status()
89
+ return response.json()
90
+
91
+ async def instances_operate(
92
+ self,
93
+ method: str,
94
+ isBatch: bool = False,
95
+ daemonId: str = "",
96
+ instance_id: str = "",
97
+ instance_list: dict[str, str] = {},
98
+ ) -> dict:
99
+ """执行实例操作,支持单个实例和批量实例操作
100
+
101
+ Args:
102
+ method (str): 操作方法名称,如 'start', 'stop', 'restart', 'kill'
103
+ isBatch (bool, optional): 是否为批量操作. 默认为 False
104
+ daemonId (str, optional): 守护进程ID. 默认为空字符串
105
+ instance_id (str, optional): 实例ID,单个操作时使用. 默认为空字符串
106
+ instance_list (dict[str, str], optional): 实例列表,批量操作时使用. 默认为空字典,示例:
107
+ [
108
+ {
109
+ "daemonId": "daemon-123",
110
+ "instanceUuids": ["uuid-1", "uuid-2", "uuid-3"]
111
+ },
112
+ {
113
+ "daemonId": "daemon-456",
114
+ "instanceUuids": ["uuid-4", "uuid-5"]
115
+ }
116
+ ]
117
+
118
+ Returns: dict: API响应结果,包含操作状态和相关信息
119
+
120
+ Raises: httpx.HTTPStatusError: 当HTTP请求返回错误状态码时抛出
121
+ """
122
+ params = {
123
+ "apikey": self.apikey,
124
+ "daemonId": daemonId,
125
+ **({"uuid": instance_id} if not isBatch else {}),
126
+ **({"daemonId": daemonId} if not isBatch else {}),
127
+ }
128
+ final_url = urljoin(
129
+ self.endpoint,
130
+ f"/api/protected_instance/{method}"
131
+ if not isBatch
132
+ else f"/api/instance/multi_{method}",
133
+ )
134
+ response = await self.client.get(final_url, params=params)
135
+ response.raise_for_status()
136
+ return response.json()
137
+
138
+ async def update_instance(self, daemonId: str, uuid: str) -> dict:
139
+ params = {
140
+ "apikey": self.apikey,
141
+ "daemonId": daemonId,
142
+ "uuid": uuid,
143
+ "task_name": "update",
144
+ }
145
+ final_url = urljoin(
146
+ self.endpoint,
147
+ "/api/protected_instance/asynchronous",
148
+ )
149
+ response = await self.client.post(final_url, params=params)
150
+ response.raise_for_status()
151
+ return response.json()
152
+
153
+ async def send_command(
154
+ self,
155
+ daemonId: str,
156
+ instance_id: str,
157
+ command: str,
158
+ ) -> dict:
159
+ params = {
160
+ "apikey": self.apikey,
161
+ "daemonId": daemonId,
162
+ "uuid": instance_id,
163
+ "command": command,
164
+ }
165
+
166
+ final_url = urljoin(
167
+ self.endpoint,
168
+ "/api/protected_instance/command",
169
+ )
170
+ response = await self.client.get(final_url, params=params)
171
+ response.raise_for_status()
172
+ return response.json()
173
+
174
+ async def get_instance_log(
175
+ self,
176
+ daemonId: str,
177
+ instance_id: str,
178
+ size: int = 1,
179
+ ) -> dict:
180
+ params = {
181
+ "apikey": self.apikey,
182
+ "daemonId": daemonId,
183
+ "uuid": instance_id,
184
+ **({"size": size} if size is not None else {}),
185
+ }
186
+
187
+ final_url = urljoin(
188
+ self.endpoint,
189
+ "/api/protected_instance/outputlog",
190
+ )
191
+ response = await self.client.get(final_url, params=params)
192
+ response.raise_for_status()
193
+ return response.json()
194
+
195
+ async def install_instance(
196
+ self,
197
+ daemonId: str,
198
+ instance_id: str,
199
+ targetUrl: str,
200
+ title: str,
201
+ description: str,
202
+ ) -> dict:
203
+ params = {
204
+ "apikey": self.apikey,
205
+ "daemonId": daemonId,
206
+ "uuid": instance_id,
207
+ }
208
+ data = {
209
+ "targetUrl": targetUrl,
210
+ "title": title,
211
+ "description": description,
212
+ }
213
+ final_url = urljoin(
214
+ self.endpoint,
215
+ "/api/protected_instance/install_instance",
216
+ )
217
+ response = await self.client.post(final_url, params=params, data=data)
218
+ response.raise_for_status()
219
+ return response.json()
@@ -0,0 +1,72 @@
1
+ from httpx import AsyncClient
2
+ from urllib.parse import urljoin
3
+
4
+ from .base import Base
5
+
6
+
7
+ class Users(Base):
8
+ def __init__(self, client: AsyncClient, endpoint: str, apikey: str, **kwargs):
9
+ super().__init__(client, endpoint, apikey, **kwargs)
10
+
11
+ async def get_user_list(
12
+ self, page: int, page_size: int, userName: str = "", role: int = 1
13
+ ) -> dict:
14
+ params = {
15
+ "apikey": self.apikey,
16
+ "page": page,
17
+ "page_size": page_size,
18
+ **({"userName": userName} if userName != "" else {}),
19
+ **({"role": role} if role is not None else {}),
20
+ }
21
+
22
+ final_url = urljoin(
23
+ self.endpoint,
24
+ "/api/auth/search",
25
+ )
26
+ response = await self.client.get(final_url, params=params)
27
+ response.raise_for_status()
28
+ return response.json()
29
+
30
+ async def create_user(
31
+ self, username: str, password: str, permission: int = 1
32
+ ) -> dict:
33
+ params = {
34
+ "apikey": self.apikey,
35
+ }
36
+ data = {
37
+ "username": username,
38
+ "password": password,
39
+ "permission": permission,
40
+ }
41
+ final_url = urljoin(
42
+ self.endpoint,
43
+ "/api/auth",
44
+ )
45
+ response = await self.client.post(final_url, params=params, data=data)
46
+ response.raise_for_status()
47
+ return response.json()
48
+
49
+ async def update_user(self, data: dict) -> dict:
50
+ params = {
51
+ "apikey": self.apikey,
52
+ }
53
+ final_url = urljoin(
54
+ self.endpoint,
55
+ "/api/auth",
56
+ )
57
+ response = await self.client.put(final_url, params=params, data=data)
58
+ response.raise_for_status()
59
+ return response.json()
60
+
61
+ async def delete_user(self, user_uuid: list[str]) -> dict:
62
+ params = {
63
+ "apikey": self.apikey,
64
+ }
65
+
66
+ final_url = urljoin(
67
+ self.endpoint,
68
+ "/api/auth",
69
+ )
70
+ response = await self.delete_with_body(final_url, params=params, data=user_uuid)
71
+ response.raise_for_status()
72
+ return response.json()
@@ -0,0 +1,285 @@
1
+ Metadata-Version: 2.4
2
+ Name: mcsm_sdk_python
3
+ Version: 1.0.6
4
+ Summary: An unoffical SDK for the MCSManager
5
+ Requires-Python: >=3.9
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: build>=1.4.0
8
+ Requires-Dist: httpx>=0.28.1
9
+ Requires-Dist: ruff>=0.13.3
10
+ Requires-Dist: twine>=6.2.0
11
+
12
+ # MCSM SDK Python
13
+
14
+ 一个用于 MCSManager 的非官方 Python SDK,提供简单易用的 API 接口来管理 Minecraft 服务器实例。
15
+
16
+ ## 特性
17
+
18
+ - 🚀 异步支持,基于 `httpx` 构建
19
+ - 📊 完整的仪表板信息获取
20
+ - 👥 用户管理功能(创建、查询、更新、删除)
21
+ - 🎮 实例管理功能(创建、启动、停止、重启等)
22
+ - 📝 类型提示支持
23
+ - 🔧 简单易用的 API 设计
24
+
25
+ ## 安装
26
+
27
+ ### 使用 pip 安装
28
+
29
+ ```bash
30
+ pip install mcsm-sdk-python
31
+ ```
32
+
33
+ ### 从源码安装
34
+
35
+ ```bash
36
+ git clone https://github.com/laobinghu/mcsm-sdk-python.git
37
+ cd mcsm-sdk-python
38
+ pip install -e .
39
+ ```
40
+
41
+ ## 快速开始
42
+
43
+ ```python
44
+ import asyncio
45
+ from httpx import AsyncClient
46
+ from mcsm_sdk_python import McsmSdk
47
+
48
+ async def main():
49
+ # 创建 HTTP 客户端
50
+ http_client = AsyncClient()
51
+
52
+ # 初始化 MCSM SDK
53
+ mcsm = McsmSdk(
54
+ client=http_client,
55
+ endpoint="http://127.0.0.1:2333", # 你的 MCSManager 地址
56
+ apikey="your_api_key_here" # 你的 API 密钥
57
+ )
58
+
59
+ try:
60
+ # 获取系统概览信息
61
+ overview = await mcsm.get_overview()
62
+ print("系统概览:", overview)
63
+
64
+ # 获取用户列表
65
+ users = await mcsm.get_user_list(page=1, page_size=10)
66
+ print("用户列表:", users)
67
+
68
+ finally:
69
+ # 关闭 HTTP 客户端
70
+ await http_client.aclose()
71
+
72
+ # 运行示例
73
+ asyncio.run(main())
74
+ ```
75
+
76
+ ## API 文档
77
+
78
+ ### 初始化
79
+
80
+ ```python
81
+ from httpx import AsyncClient
82
+ from mcsm_sdk_python import McsmSdk
83
+
84
+ client = AsyncClient()
85
+ mcsm = McsmSdk(
86
+ client=client,
87
+ endpoint="http://your-mcsm-server:2333",
88
+ apikey="your_api_key"
89
+ )
90
+ ```
91
+
92
+ ### 仪表板 API
93
+
94
+ #### 获取系统概览
95
+
96
+ ```python
97
+ overview = await mcsm.get_overview()
98
+ ```
99
+
100
+ 返回系统的基本信息,包括服务器状态、实例数量等。
101
+
102
+ ### 用户管理 API
103
+
104
+ #### 获取用户列表
105
+
106
+ ```python
107
+ users = await mcsm.get_user_list(
108
+ page=1, # 页码
109
+ page_size=10, # 每页数量
110
+ userName="", # 可选:用户名筛选
111
+ role=1 # 可选:角色筛选
112
+ )
113
+ ```
114
+
115
+ #### 创建用户
116
+
117
+ ```python
118
+ result = await mcsm.create_user(
119
+ username="new_user",
120
+ password="password123",
121
+ permission=1 # 权限级别
122
+ )
123
+ ```
124
+
125
+ #### 更新用户信息
126
+
127
+ ```python
128
+ result = await mcsm.update_user({
129
+ "uuid": "user_uuid",
130
+ "username": "updated_name",
131
+ "permission": 2
132
+ })
133
+ ```
134
+
135
+ #### 删除用户
136
+
137
+ ```python
138
+ result = await mcsm.delete_user(["user_uuid_1", "user_uuid_2"])
139
+ ```
140
+
141
+ ### 实例管理 API
142
+
143
+ #### 获取实例列表
144
+
145
+ ```python
146
+ instances = await mcsm.get_instances_list(
147
+ daemonId="daemon_id",
148
+ page=1,
149
+ page_size=10,
150
+ status="running", # 状态筛选
151
+ instance_name="" # 可选:实例名称筛选
152
+ )
153
+ ```
154
+
155
+ #### 获取实例详细信息
156
+
157
+ ```python
158
+ instance_info = await mcsm.get_instances_info(
159
+ daemonId="daemon_id",
160
+ page=1,
161
+ page_size=10,
162
+ status="running",
163
+ instance_id="instance_uuid"
164
+ )
165
+ ```
166
+
167
+ #### 创建实例
168
+
169
+ ```python
170
+ result = await mcsm.create_instance(
171
+ daemonId="daemon_id",
172
+ data={
173
+ "nickname": "我的服务器",
174
+ "startCommand": "java -jar server.jar",
175
+ "stopCommand": "stop",
176
+ "cwd": "/path/to/server",
177
+ "ie": "utf-8",
178
+ "oe": "utf-8"
179
+ }
180
+ )
181
+ ```
182
+
183
+ #### 实例操作
184
+
185
+ 支持单个实例和批量实例操作:
186
+
187
+ ```python
188
+ # 单个实例操作
189
+ result = await mcsm.instances_operate(
190
+ method="start", # 操作类型:start, stop, restart, kill
191
+ daemonId="daemon_id",
192
+ instance_id="instance_uuid"
193
+ )
194
+
195
+ # 批量实例操作
196
+ result = await mcsm.instances_operate(
197
+ method="start",
198
+ isBatch=True,
199
+ instance_list=[
200
+ {
201
+ "daemonId": "daemon-123",
202
+ "instanceUuids": ["uuid-1", "uuid-2", "uuid-3"]
203
+ },
204
+ {
205
+ "daemonId": "daemon-456",
206
+ "instanceUuids": ["uuid-4", "uuid-5"]
207
+ }
208
+ ]
209
+ )
210
+ ```
211
+
212
+ 支持的操作类型:
213
+ - `start` - 启动实例
214
+ - `stop` - 停止实例
215
+ - `restart` - 重启实例
216
+ - `kill` - 强制终止实例
217
+
218
+ #### 更新实例
219
+
220
+ ```python
221
+ result = await mcsm.update_instance(
222
+ daemonId="daemon_id",
223
+ uuid="instance_uuid"
224
+ )
225
+ ```
226
+
227
+ ## 错误处理
228
+
229
+ SDK 使用 `httpx` 的异常处理机制:
230
+
231
+ ```python
232
+ import httpx
233
+
234
+ try:
235
+ result = await mcsm.get_overview()
236
+ except httpx.HTTPStatusError as e:
237
+ print(f"HTTP 错误: {e.response.status_code}")
238
+ print(f"响应内容: {e.response.text}")
239
+ except httpx.RequestError as e:
240
+ print(f"请求错误: {e}")
241
+ ```
242
+
243
+ ## 配置要求
244
+
245
+ - Python 3.9+
246
+ - httpx >= 0.28.1
247
+
248
+ ## 项目结构
249
+
250
+ ```
251
+ mcsm_sdk_python/
252
+ ├── __init__.py # 主入口,导出 McsmSdk 类
253
+ ├── base.py # 基础类,提供通用功能
254
+ ├── dashboard.py # 仪表板相关 API
255
+ ├── users.py # 用户管理 API
256
+ ├── instance.py # 实例管理 API
257
+ ├── daemon.py # 守护进程 API(待实现)
258
+ ├── fileManager.py # 文件管理 API(待实现)
259
+ └── imageManager.py # 镜像管理 API(待实现)
260
+ ```
261
+
262
+ ## 开发计划
263
+
264
+ - [ ] 守护进程管理 API
265
+ - [ ] 文件管理 API
266
+ - [ ] 镜像管理 API
267
+ - [ ] 单元测试
268
+ - [ ] 文档完善
269
+
270
+ ## 贡献
271
+
272
+ 欢迎提交 Issue 和 Pull Request!
273
+
274
+ ## 许可证
275
+
276
+ MIT License
277
+
278
+ ## 相关链接
279
+
280
+ - [MCSManager 官方网站](https://mcsmanager.com/)
281
+ - [MCSManager GitHub](https://github.com/MCSManager/MCSManager)
282
+
283
+ ---
284
+
285
+ **注意**: 这是一个非官方的 SDK,与 MCSManager 官方团队无关。使用前请确保你的 MCSManager 版本支持相应的 API。
@@ -0,0 +1,15 @@
1
+ README.md
2
+ pyproject.toml
3
+ mcsm_sdk_python/__init__.py
4
+ mcsm_sdk_python/base.py
5
+ mcsm_sdk_python/daemon.py
6
+ mcsm_sdk_python/dashboard.py
7
+ mcsm_sdk_python/fileManager.py
8
+ mcsm_sdk_python/imageManager.py
9
+ mcsm_sdk_python/instance.py
10
+ mcsm_sdk_python/users.py
11
+ mcsm_sdk_python.egg-info/PKG-INFO
12
+ mcsm_sdk_python.egg-info/SOURCES.txt
13
+ mcsm_sdk_python.egg-info/dependency_links.txt
14
+ mcsm_sdk_python.egg-info/requires.txt
15
+ mcsm_sdk_python.egg-info/top_level.txt
@@ -0,0 +1,4 @@
1
+ build>=1.4.0
2
+ httpx>=0.28.1
3
+ ruff>=0.13.3
4
+ twine>=6.2.0
@@ -0,0 +1 @@
1
+ mcsm_sdk_python
@@ -0,0 +1,12 @@
1
+ [project]
2
+ name = "mcsm_sdk_python"
3
+ version = "1.0.6"
4
+ description = "An unoffical SDK for the MCSManager"
5
+ readme = "README.md"
6
+ requires-python = ">=3.9"
7
+ dependencies = [
8
+ "build>=1.4.0",
9
+ "httpx>=0.28.1",
10
+ "ruff>=0.13.3",
11
+ "twine>=6.2.0",
12
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+