eduskit 0.1.2__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.
- eduskit-0.1.2/LICENSE +202 -0
- eduskit-0.1.2/PKG-INFO +146 -0
- eduskit-0.1.2/README.md +133 -0
- eduskit-0.1.2/eduskit/__init__.py +77 -0
- eduskit-0.1.2/eduskit/classroom.py +149 -0
- eduskit-0.1.2/eduskit/errors.py +21 -0
- eduskit-0.1.2/eduskit/http.py +116 -0
- eduskit-0.1.2/eduskit/token.py +44 -0
- eduskit-0.1.2/eduskit/whiteboard.py +104 -0
- eduskit-0.1.2/eduskit.egg-info/PKG-INFO +146 -0
- eduskit-0.1.2/eduskit.egg-info/SOURCES.txt +14 -0
- eduskit-0.1.2/eduskit.egg-info/dependency_links.txt +1 -0
- eduskit-0.1.2/eduskit.egg-info/top_level.txt +1 -0
- eduskit-0.1.2/pyproject.toml +18 -0
- eduskit-0.1.2/setup.cfg +4 -0
- eduskit-0.1.2/tests/test_eduskit.py +89 -0
eduskit-0.1.2/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Eduskit
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
eduskit-0.1.2/PKG-INFO
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: eduskit
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Eduskit B-end server SDK for classroom and whiteboard APIs
|
|
5
|
+
Author: Eduskit
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/eduskit/server-sdk-python
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Requires-Python: >=3.9
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# eduskit
|
|
15
|
+
|
|
16
|
+
Python B 端 Server SDK。`sdk.client` 调课堂 server-api,`sdk.whiteboard_client` 调白板 server-api。
|
|
17
|
+
|
|
18
|
+
方法名为 snake_case;**请求/响应 JSON 字段仍为 camelCase**(`originId`、`eduUserId`)。
|
|
19
|
+
|
|
20
|
+
概念与方法表见 [docs/overview.md](docs/overview.md)、[docs/api.md](docs/api.md)。
|
|
21
|
+
|
|
22
|
+
## 安装
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install -e .
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
要求 Python 3.9+,无第三方运行时依赖(标准库 `urllib`)。
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
PYTHONPATH=. python3 -m unittest tests.test_eduskit -v
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 初始化
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from eduskit import Eduskit, EduskitError
|
|
38
|
+
|
|
39
|
+
sdk = Eduskit(
|
|
40
|
+
timeout_ms=10_000,
|
|
41
|
+
lang="zh-CN",
|
|
42
|
+
client={
|
|
43
|
+
"base_url": "http://localhost:3112",
|
|
44
|
+
"app_id": "app_edu",
|
|
45
|
+
"app_key": "edu_key",
|
|
46
|
+
"app_secret": "edu_secret",
|
|
47
|
+
},
|
|
48
|
+
whiteboard_client={
|
|
49
|
+
"base_url": "http://localhost:3012",
|
|
50
|
+
"app_id": "app_wb",
|
|
51
|
+
"app_key": "wb_key",
|
|
52
|
+
"app_secret": "wb_secret",
|
|
53
|
+
},
|
|
54
|
+
)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
配置字典键是 snake_case(`base_url` / `app_id` / `app_key` / `app_secret`)。两侧均可选。
|
|
58
|
+
|
|
59
|
+
## 课堂 `sdk.client`
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
user = sdk.client.users.register(
|
|
63
|
+
originId="stu_001",
|
|
64
|
+
nickname="小明",
|
|
65
|
+
avatar="https://example.com/a.png",
|
|
66
|
+
)
|
|
67
|
+
token = sdk.client.auth.issue_token(eduUserId=user["eduUserId"], originId="stu_001")
|
|
68
|
+
classroom = sdk.client.classrooms.create(
|
|
69
|
+
name="一年级数学",
|
|
70
|
+
startsAt="2026-08-17T10:00:00.000Z",
|
|
71
|
+
endsAt="2026-08-17T11:00:00.000Z",
|
|
72
|
+
teacherEduUserId=user["eduUserId"],
|
|
73
|
+
)
|
|
74
|
+
sdk.client.classrooms.start(classroom["classroomId"])
|
|
75
|
+
sdk.client.classrooms.members.add(
|
|
76
|
+
classroom["classroomId"],
|
|
77
|
+
eduUserId=user["eduUserId"],
|
|
78
|
+
role="student",
|
|
79
|
+
)
|
|
80
|
+
sdk.client.classrooms.members.replace_students(
|
|
81
|
+
classroom["classroomId"],
|
|
82
|
+
eduUserIds=[user["eduUserId"]],
|
|
83
|
+
)
|
|
84
|
+
sdk.client.classrooms.permissions.set(
|
|
85
|
+
classroom["classroomId"],
|
|
86
|
+
user["eduUserId"],
|
|
87
|
+
permission="camera",
|
|
88
|
+
effect="grant",
|
|
89
|
+
operatorEduUserId=user["eduUserId"],
|
|
90
|
+
)
|
|
91
|
+
sdk.client.classrooms.coursewares.bind(
|
|
92
|
+
classroom["classroomId"],
|
|
93
|
+
coursewareIds=["cw_xxx"],
|
|
94
|
+
)
|
|
95
|
+
sdk.client.app.get_ui_config()
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
| 方法 | 对应 Node |
|
|
99
|
+
|------|-----------|
|
|
100
|
+
| `users.register(**input)` | `users.register` |
|
|
101
|
+
| `auth.issue_token(**input)` | `auth.issueToken` |
|
|
102
|
+
| `classrooms.create/start/end` | 同名 |
|
|
103
|
+
| `classrooms.members.add/list` | 同名 |
|
|
104
|
+
| `classrooms.members.replace_students` | `replaceStudents` |
|
|
105
|
+
| `classrooms.permissions.get/set/clear` | 同名 |
|
|
106
|
+
| `classrooms.coursewares.list/bind/unbind` | 同名 |
|
|
107
|
+
| `app.get_ui_config` / `set_ui_config` | `getUiConfig` / `setUiConfig` |
|
|
108
|
+
|
|
109
|
+
## 白板 `sdk.whiteboard_client`
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
credential = sdk.whiteboard_client.auth.issue_room_token(
|
|
113
|
+
roomId="room_1",
|
|
114
|
+
userId=user["eduUserId"],
|
|
115
|
+
role="host",
|
|
116
|
+
expiresIn=3600,
|
|
117
|
+
)
|
|
118
|
+
session = sdk.whiteboard_client.recordings.start("room_1", externalRef="lesson_001")
|
|
119
|
+
sdk.whiteboard_client.recordings.stop("room_1", recordingId=session["recordingId"])
|
|
120
|
+
sdk.whiteboard_client.recordings.enqueue_video_export(session["recordingId"], profile="hd")
|
|
121
|
+
sdk.whiteboard_client.files.convert(sourceUrl="https://cdn.example.com/lesson.pptx")
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
| 方法 | 对应 Node |
|
|
125
|
+
|------|-----------|
|
|
126
|
+
| `auth.issue_room_token` | `issueRoomToken` |
|
|
127
|
+
| `recordings.start/stop/list/get` | 同名 |
|
|
128
|
+
| `recordings.register_media_asset` / `delete_media_asset` | camelCase 版 |
|
|
129
|
+
| `recordings.enqueue_video_export` / `get_video_export` | camelCase 版 |
|
|
130
|
+
| `captures.create/list` | 同名 |
|
|
131
|
+
| `files.convert` / `get_convert_job` | `getConvertJob` |
|
|
132
|
+
|
|
133
|
+
## 错误
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
try:
|
|
137
|
+
sdk.client.auth.issue_token(eduUserId="")
|
|
138
|
+
except EduskitError as e:
|
|
139
|
+
print(e.error_code, e.status, e.trace_id, e.source)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`sdk.client.last_trace_id` / `sdk.whiteboard_client.last_trace_id`。
|
|
143
|
+
|
|
144
|
+
## 示例
|
|
145
|
+
|
|
146
|
+
- [examples/classroom_quickstart.py](examples/classroom_quickstart.py)
|
eduskit-0.1.2/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# eduskit
|
|
2
|
+
|
|
3
|
+
Python B 端 Server SDK。`sdk.client` 调课堂 server-api,`sdk.whiteboard_client` 调白板 server-api。
|
|
4
|
+
|
|
5
|
+
方法名为 snake_case;**请求/响应 JSON 字段仍为 camelCase**(`originId`、`eduUserId`)。
|
|
6
|
+
|
|
7
|
+
概念与方法表见 [docs/overview.md](docs/overview.md)、[docs/api.md](docs/api.md)。
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install -e .
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
要求 Python 3.9+,无第三方运行时依赖(标准库 `urllib`)。
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
PYTHONPATH=. python3 -m unittest tests.test_eduskit -v
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 初始化
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from eduskit import Eduskit, EduskitError
|
|
25
|
+
|
|
26
|
+
sdk = Eduskit(
|
|
27
|
+
timeout_ms=10_000,
|
|
28
|
+
lang="zh-CN",
|
|
29
|
+
client={
|
|
30
|
+
"base_url": "http://localhost:3112",
|
|
31
|
+
"app_id": "app_edu",
|
|
32
|
+
"app_key": "edu_key",
|
|
33
|
+
"app_secret": "edu_secret",
|
|
34
|
+
},
|
|
35
|
+
whiteboard_client={
|
|
36
|
+
"base_url": "http://localhost:3012",
|
|
37
|
+
"app_id": "app_wb",
|
|
38
|
+
"app_key": "wb_key",
|
|
39
|
+
"app_secret": "wb_secret",
|
|
40
|
+
},
|
|
41
|
+
)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
配置字典键是 snake_case(`base_url` / `app_id` / `app_key` / `app_secret`)。两侧均可选。
|
|
45
|
+
|
|
46
|
+
## 课堂 `sdk.client`
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
user = sdk.client.users.register(
|
|
50
|
+
originId="stu_001",
|
|
51
|
+
nickname="小明",
|
|
52
|
+
avatar="https://example.com/a.png",
|
|
53
|
+
)
|
|
54
|
+
token = sdk.client.auth.issue_token(eduUserId=user["eduUserId"], originId="stu_001")
|
|
55
|
+
classroom = sdk.client.classrooms.create(
|
|
56
|
+
name="一年级数学",
|
|
57
|
+
startsAt="2026-08-17T10:00:00.000Z",
|
|
58
|
+
endsAt="2026-08-17T11:00:00.000Z",
|
|
59
|
+
teacherEduUserId=user["eduUserId"],
|
|
60
|
+
)
|
|
61
|
+
sdk.client.classrooms.start(classroom["classroomId"])
|
|
62
|
+
sdk.client.classrooms.members.add(
|
|
63
|
+
classroom["classroomId"],
|
|
64
|
+
eduUserId=user["eduUserId"],
|
|
65
|
+
role="student",
|
|
66
|
+
)
|
|
67
|
+
sdk.client.classrooms.members.replace_students(
|
|
68
|
+
classroom["classroomId"],
|
|
69
|
+
eduUserIds=[user["eduUserId"]],
|
|
70
|
+
)
|
|
71
|
+
sdk.client.classrooms.permissions.set(
|
|
72
|
+
classroom["classroomId"],
|
|
73
|
+
user["eduUserId"],
|
|
74
|
+
permission="camera",
|
|
75
|
+
effect="grant",
|
|
76
|
+
operatorEduUserId=user["eduUserId"],
|
|
77
|
+
)
|
|
78
|
+
sdk.client.classrooms.coursewares.bind(
|
|
79
|
+
classroom["classroomId"],
|
|
80
|
+
coursewareIds=["cw_xxx"],
|
|
81
|
+
)
|
|
82
|
+
sdk.client.app.get_ui_config()
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
| 方法 | 对应 Node |
|
|
86
|
+
|------|-----------|
|
|
87
|
+
| `users.register(**input)` | `users.register` |
|
|
88
|
+
| `auth.issue_token(**input)` | `auth.issueToken` |
|
|
89
|
+
| `classrooms.create/start/end` | 同名 |
|
|
90
|
+
| `classrooms.members.add/list` | 同名 |
|
|
91
|
+
| `classrooms.members.replace_students` | `replaceStudents` |
|
|
92
|
+
| `classrooms.permissions.get/set/clear` | 同名 |
|
|
93
|
+
| `classrooms.coursewares.list/bind/unbind` | 同名 |
|
|
94
|
+
| `app.get_ui_config` / `set_ui_config` | `getUiConfig` / `setUiConfig` |
|
|
95
|
+
|
|
96
|
+
## 白板 `sdk.whiteboard_client`
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
credential = sdk.whiteboard_client.auth.issue_room_token(
|
|
100
|
+
roomId="room_1",
|
|
101
|
+
userId=user["eduUserId"],
|
|
102
|
+
role="host",
|
|
103
|
+
expiresIn=3600,
|
|
104
|
+
)
|
|
105
|
+
session = sdk.whiteboard_client.recordings.start("room_1", externalRef="lesson_001")
|
|
106
|
+
sdk.whiteboard_client.recordings.stop("room_1", recordingId=session["recordingId"])
|
|
107
|
+
sdk.whiteboard_client.recordings.enqueue_video_export(session["recordingId"], profile="hd")
|
|
108
|
+
sdk.whiteboard_client.files.convert(sourceUrl="https://cdn.example.com/lesson.pptx")
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
| 方法 | 对应 Node |
|
|
112
|
+
|------|-----------|
|
|
113
|
+
| `auth.issue_room_token` | `issueRoomToken` |
|
|
114
|
+
| `recordings.start/stop/list/get` | 同名 |
|
|
115
|
+
| `recordings.register_media_asset` / `delete_media_asset` | camelCase 版 |
|
|
116
|
+
| `recordings.enqueue_video_export` / `get_video_export` | camelCase 版 |
|
|
117
|
+
| `captures.create/list` | 同名 |
|
|
118
|
+
| `files.convert` / `get_convert_job` | `getConvertJob` |
|
|
119
|
+
|
|
120
|
+
## 错误
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
try:
|
|
124
|
+
sdk.client.auth.issue_token(eduUserId="")
|
|
125
|
+
except EduskitError as e:
|
|
126
|
+
print(e.error_code, e.status, e.trace_id, e.source)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`sdk.client.last_trace_id` / `sdk.whiteboard_client.last_trace_id`。
|
|
130
|
+
|
|
131
|
+
## 示例
|
|
132
|
+
|
|
133
|
+
- [examples/classroom_quickstart.py](examples/classroom_quickstart.py)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, Optional
|
|
4
|
+
|
|
5
|
+
from .classroom import ClassroomClient
|
|
6
|
+
from .errors import EduskitError
|
|
7
|
+
from .http import Fetcher, HttpTransport
|
|
8
|
+
from .whiteboard import WhiteboardClient
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"Eduskit",
|
|
12
|
+
"EduskitError",
|
|
13
|
+
"ClassroomClient",
|
|
14
|
+
"WhiteboardClient",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Eduskit:
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
*,
|
|
22
|
+
client: Optional[Dict[str, Any]] = None,
|
|
23
|
+
whiteboard_client: Optional[Dict[str, Any]] = None,
|
|
24
|
+
timeout_ms: int = 10_000,
|
|
25
|
+
lang: str = "zh-CN",
|
|
26
|
+
fetch: Optional[Fetcher] = None,
|
|
27
|
+
) -> None:
|
|
28
|
+
self._client: Optional[ClassroomClient] = None
|
|
29
|
+
self._whiteboard: Optional[WhiteboardClient] = None
|
|
30
|
+
if client:
|
|
31
|
+
self._client = ClassroomClient(
|
|
32
|
+
HttpTransport(
|
|
33
|
+
base_url=client["base_url"],
|
|
34
|
+
app_key=client["app_key"],
|
|
35
|
+
app_secret=client["app_secret"],
|
|
36
|
+
timeout_ms=timeout_ms,
|
|
37
|
+
lang=lang,
|
|
38
|
+
source="classroom",
|
|
39
|
+
fetch=fetch,
|
|
40
|
+
),
|
|
41
|
+
app_id=client["app_id"],
|
|
42
|
+
app_secret=client["app_secret"],
|
|
43
|
+
)
|
|
44
|
+
if whiteboard_client:
|
|
45
|
+
self._whiteboard = WhiteboardClient(
|
|
46
|
+
HttpTransport(
|
|
47
|
+
base_url=whiteboard_client["base_url"],
|
|
48
|
+
app_key=whiteboard_client["app_key"],
|
|
49
|
+
app_secret=whiteboard_client["app_secret"],
|
|
50
|
+
timeout_ms=timeout_ms,
|
|
51
|
+
lang=lang,
|
|
52
|
+
source="whiteboard",
|
|
53
|
+
fetch=fetch,
|
|
54
|
+
),
|
|
55
|
+
app_id=whiteboard_client["app_id"],
|
|
56
|
+
app_secret=whiteboard_client["app_secret"],
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def client(self) -> ClassroomClient:
|
|
61
|
+
if self._client is None:
|
|
62
|
+
raise EduskitError(
|
|
63
|
+
"classroom client is not configured",
|
|
64
|
+
error_code="SDK_CLIENT_NOT_CONFIGURED",
|
|
65
|
+
source="classroom",
|
|
66
|
+
)
|
|
67
|
+
return self._client
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def whiteboard_client(self) -> WhiteboardClient:
|
|
71
|
+
if self._whiteboard is None:
|
|
72
|
+
raise EduskitError(
|
|
73
|
+
"whiteboard client is not configured",
|
|
74
|
+
error_code="SDK_CLIENT_NOT_CONFIGURED",
|
|
75
|
+
source="whiteboard",
|
|
76
|
+
)
|
|
77
|
+
return self._whiteboard
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from .http import HttpTransport
|
|
6
|
+
from .token import iso_time, sign_hs256
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class _Users:
|
|
10
|
+
def __init__(self, http: HttpTransport) -> None:
|
|
11
|
+
self._http = http
|
|
12
|
+
|
|
13
|
+
def register(self, **input: Any) -> Any:
|
|
14
|
+
return self._http.request("POST", "/v1/users", input)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class _Auth:
|
|
18
|
+
def __init__(self, app_id: str, app_secret: str) -> None:
|
|
19
|
+
self._app_id = app_id
|
|
20
|
+
self._app_secret = app_secret
|
|
21
|
+
|
|
22
|
+
def issue_token(self, **input: Any) -> Any:
|
|
23
|
+
user_id = input.get("eduUserId")
|
|
24
|
+
if not isinstance(user_id, str) or not user_id:
|
|
25
|
+
from .errors import EduskitError
|
|
26
|
+
raise EduskitError("eduUserId is required", error_code="SDK_TOKEN_INPUT_INVALID", source="classroom")
|
|
27
|
+
expires_in = input.get("expiresIn", 86400)
|
|
28
|
+
claims = {"appId": self._app_id}
|
|
29
|
+
if input.get("originId"):
|
|
30
|
+
claims["originId"] = input["originId"]
|
|
31
|
+
if input.get("role"):
|
|
32
|
+
claims["role"] = input["role"]
|
|
33
|
+
token, expires_at = sign_hs256(self._app_id, self._app_secret, user_id,
|
|
34
|
+
"eduskit-edu-auth", "eduskit-edu", expires_in, claims, "classroom")
|
|
35
|
+
return {"accessToken": token, "expiresIn": expires_in, "expiresAt": iso_time(expires_at),
|
|
36
|
+
"tokenType": "Bearer", "appId": self._app_id, "eduUserId": user_id,
|
|
37
|
+
"originId": input.get("originId", ""), "role": input.get("role", "")}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class _Members:
|
|
41
|
+
def __init__(self, http: HttpTransport) -> None:
|
|
42
|
+
self._http = http
|
|
43
|
+
|
|
44
|
+
def add(self, classroom_id: str, **input: Any) -> Any:
|
|
45
|
+
return self._http.request("POST", f"/v1/classrooms/{classroom_id}/members", input)
|
|
46
|
+
|
|
47
|
+
def list(self, classroom_id: str) -> Any:
|
|
48
|
+
return self._http.request("GET", f"/v1/classrooms/{classroom_id}/members")
|
|
49
|
+
|
|
50
|
+
def replace_students(self, classroom_id: str, **input: Any) -> Any:
|
|
51
|
+
return self._http.request(
|
|
52
|
+
"PUT",
|
|
53
|
+
f"/v1/classrooms/{classroom_id}/members/students",
|
|
54
|
+
input,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class _Permissions:
|
|
59
|
+
def __init__(self, http: HttpTransport) -> None:
|
|
60
|
+
self._http = http
|
|
61
|
+
|
|
62
|
+
def get(self, classroom_id: str, edu_user_id: str) -> Any:
|
|
63
|
+
return self._http.request(
|
|
64
|
+
"GET",
|
|
65
|
+
f"/v1/classrooms/{classroom_id}/members/{edu_user_id}/permissions",
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
def set(self, classroom_id: str, edu_user_id: str, **input: Any) -> Any:
|
|
69
|
+
return self._http.request(
|
|
70
|
+
"POST",
|
|
71
|
+
f"/v1/classrooms/{classroom_id}/members/{edu_user_id}/permissions",
|
|
72
|
+
input,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
def clear(
|
|
76
|
+
self,
|
|
77
|
+
classroom_id: str,
|
|
78
|
+
edu_user_id: str,
|
|
79
|
+
permission: str,
|
|
80
|
+
**input: Any,
|
|
81
|
+
) -> Any:
|
|
82
|
+
return self._http.request(
|
|
83
|
+
"DELETE",
|
|
84
|
+
f"/v1/classrooms/{classroom_id}/members/{edu_user_id}/permissions/{permission}",
|
|
85
|
+
input,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class _Coursewares:
|
|
90
|
+
def __init__(self, http: HttpTransport) -> None:
|
|
91
|
+
self._http = http
|
|
92
|
+
|
|
93
|
+
def list(self, classroom_id: str) -> Any:
|
|
94
|
+
return self._http.request("GET", f"/v1/classrooms/{classroom_id}/coursewares")
|
|
95
|
+
|
|
96
|
+
def bind(self, classroom_id: str, **input: Any) -> Any:
|
|
97
|
+
return self._http.request(
|
|
98
|
+
"POST",
|
|
99
|
+
f"/v1/classrooms/{classroom_id}/coursewares",
|
|
100
|
+
input,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
def unbind(self, classroom_id: str, **input: Any) -> Any:
|
|
104
|
+
return self._http.request(
|
|
105
|
+
"DELETE",
|
|
106
|
+
f"/v1/classrooms/{classroom_id}/coursewares",
|
|
107
|
+
input,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class _Classrooms:
|
|
112
|
+
def __init__(self, http: HttpTransport) -> None:
|
|
113
|
+
self.members = _Members(http)
|
|
114
|
+
self.permissions = _Permissions(http)
|
|
115
|
+
self.coursewares = _Coursewares(http)
|
|
116
|
+
self._http = http
|
|
117
|
+
|
|
118
|
+
def create(self, **input: Any) -> Any:
|
|
119
|
+
return self._http.request("POST", "/v1/classrooms", input)
|
|
120
|
+
|
|
121
|
+
def start(self, classroom_id: str) -> Any:
|
|
122
|
+
return self._http.request("POST", f"/v1/classrooms/{classroom_id}/start")
|
|
123
|
+
|
|
124
|
+
def end(self, classroom_id: str) -> Any:
|
|
125
|
+
return self._http.request("POST", f"/v1/classrooms/{classroom_id}/end")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class _App:
|
|
129
|
+
def __init__(self, http: HttpTransport) -> None:
|
|
130
|
+
self._http = http
|
|
131
|
+
|
|
132
|
+
def get_ui_config(self) -> Any:
|
|
133
|
+
return self._http.request("GET", "/v1/app/ui-config")
|
|
134
|
+
|
|
135
|
+
def set_ui_config(self, **input: Any) -> Any:
|
|
136
|
+
return self._http.request("PUT", "/v1/app/ui-config", input)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class ClassroomClient:
|
|
140
|
+
def __init__(self, http: HttpTransport, *, app_id: str, app_secret: str) -> None:
|
|
141
|
+
self.users = _Users(http)
|
|
142
|
+
self.auth = _Auth(app_id, app_secret)
|
|
143
|
+
self.classrooms = _Classrooms(http)
|
|
144
|
+
self.app = _App(http)
|
|
145
|
+
self._http = http
|
|
146
|
+
|
|
147
|
+
@property
|
|
148
|
+
def last_trace_id(self) -> str:
|
|
149
|
+
return self._http.last_trace_id
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class EduskitError(Exception):
|
|
7
|
+
def __init__(
|
|
8
|
+
self,
|
|
9
|
+
message: str,
|
|
10
|
+
status: Optional[int] = None,
|
|
11
|
+
error_code: Optional[str] = None,
|
|
12
|
+
trace_id: Optional[str] = None,
|
|
13
|
+
path: Optional[str] = None,
|
|
14
|
+
source: Optional[str] = None,
|
|
15
|
+
) -> None:
|
|
16
|
+
super().__init__(message)
|
|
17
|
+
self.status = status
|
|
18
|
+
self.error_code = error_code
|
|
19
|
+
self.trace_id = trace_id
|
|
20
|
+
self.path = path
|
|
21
|
+
self.source = source
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import uuid
|
|
5
|
+
from typing import Any, Callable, Dict, Optional, Tuple
|
|
6
|
+
from urllib.error import HTTPError, URLError
|
|
7
|
+
from urllib.request import Request, urlopen
|
|
8
|
+
|
|
9
|
+
from .errors import EduskitError
|
|
10
|
+
|
|
11
|
+
Fetcher = Callable[[str, str, Dict[str, str], Optional[bytes], int], Tuple[int, Dict[str, str], bytes]]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _default_fetch(
|
|
15
|
+
url: str,
|
|
16
|
+
method: str,
|
|
17
|
+
headers: dict[str, str],
|
|
18
|
+
body: bytes | None,
|
|
19
|
+
timeout_ms: int,
|
|
20
|
+
) -> tuple[int, dict[str, str], bytes]:
|
|
21
|
+
request = Request(url, data=body, method=method, headers=headers)
|
|
22
|
+
try:
|
|
23
|
+
with urlopen(request, timeout=timeout_ms / 1000) as response:
|
|
24
|
+
return response.status, dict(response.headers.items()), response.read()
|
|
25
|
+
except HTTPError as error:
|
|
26
|
+
return error.code, dict(error.headers.items()), error.read()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class HttpTransport:
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
*,
|
|
33
|
+
base_url: str,
|
|
34
|
+
app_key: str,
|
|
35
|
+
app_secret: str,
|
|
36
|
+
timeout_ms: int = 10_000,
|
|
37
|
+
lang: str = "zh-CN",
|
|
38
|
+
source: str,
|
|
39
|
+
fetch: Optional[Fetcher] = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
self.base_url = base_url.rstrip("/")
|
|
42
|
+
self.app_key = app_key
|
|
43
|
+
self.app_secret = app_secret
|
|
44
|
+
self.timeout_ms = timeout_ms
|
|
45
|
+
self.lang = lang
|
|
46
|
+
self.source = source
|
|
47
|
+
self.fetch = fetch or _default_fetch
|
|
48
|
+
self.last_trace_id = ""
|
|
49
|
+
|
|
50
|
+
def request(self, method: str, path: str, body: Optional[Any] = None) -> Any:
|
|
51
|
+
trace_id = uuid.uuid4().hex[:16]
|
|
52
|
+
payload = None if body is None else json.dumps(body).encode("utf-8")
|
|
53
|
+
headers = {
|
|
54
|
+
"Content-Type": "application/json",
|
|
55
|
+
"Accept": "application/json",
|
|
56
|
+
"x-app-key": self.app_key,
|
|
57
|
+
"x-app-secret": self.app_secret,
|
|
58
|
+
"x-lang": self.lang,
|
|
59
|
+
"x-trace-id": trace_id,
|
|
60
|
+
}
|
|
61
|
+
try:
|
|
62
|
+
status, response_headers, raw = self.fetch(
|
|
63
|
+
f"{self.base_url}{path}",
|
|
64
|
+
method,
|
|
65
|
+
headers,
|
|
66
|
+
payload,
|
|
67
|
+
self.timeout_ms,
|
|
68
|
+
)
|
|
69
|
+
except URLError as error:
|
|
70
|
+
raise EduskitError(
|
|
71
|
+
str(error.reason),
|
|
72
|
+
error_code="SDK_NETWORK_ERROR",
|
|
73
|
+
path=path,
|
|
74
|
+
source=self.source,
|
|
75
|
+
trace_id=trace_id,
|
|
76
|
+
) from error
|
|
77
|
+
|
|
78
|
+
text = raw.decode("utf-8") if raw else ""
|
|
79
|
+
data: dict[str, Any] = {}
|
|
80
|
+
if text:
|
|
81
|
+
try:
|
|
82
|
+
parsed = json.loads(text)
|
|
83
|
+
except json.JSONDecodeError as error:
|
|
84
|
+
raise EduskitError(
|
|
85
|
+
text or f"invalid json (HTTP {status})",
|
|
86
|
+
status=status,
|
|
87
|
+
error_code="SDK_INVALID_JSON",
|
|
88
|
+
path=path,
|
|
89
|
+
source=self.source,
|
|
90
|
+
trace_id=trace_id,
|
|
91
|
+
) from error
|
|
92
|
+
if isinstance(parsed, dict):
|
|
93
|
+
data = parsed
|
|
94
|
+
else:
|
|
95
|
+
data = {"data": parsed}
|
|
96
|
+
|
|
97
|
+
header_trace = ""
|
|
98
|
+
for key, value in response_headers.items():
|
|
99
|
+
if key.lower() == "x-trace-id":
|
|
100
|
+
header_trace = value
|
|
101
|
+
break
|
|
102
|
+
self.last_trace_id = str(data.get("traceId") or header_trace or trace_id)
|
|
103
|
+
|
|
104
|
+
code = data.get("code")
|
|
105
|
+
if status >= 400 or (code is not None and code != 0):
|
|
106
|
+
raise EduskitError(
|
|
107
|
+
str(data.get("message") or f"HTTP {status}"),
|
|
108
|
+
status=status,
|
|
109
|
+
error_code=str(data.get("errorCode") or f"HTTP_{status}"),
|
|
110
|
+
path=str(data.get("path") or path),
|
|
111
|
+
source=self.source,
|
|
112
|
+
trace_id=self.last_trace_id,
|
|
113
|
+
)
|
|
114
|
+
if "data" in data:
|
|
115
|
+
return data["data"]
|
|
116
|
+
return data
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import hashlib
|
|
5
|
+
import hmac
|
|
6
|
+
import json
|
|
7
|
+
import time
|
|
8
|
+
from datetime import datetime, timezone
|
|
9
|
+
from typing import Any, Dict
|
|
10
|
+
|
|
11
|
+
from .errors import EduskitError
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def sign_hs256(app_id: str, app_secret: str, subject: str, issuer: str,
|
|
15
|
+
audience: str, expires_in: int, claims: Dict[str, Any],
|
|
16
|
+
source: str) -> tuple[str, int]:
|
|
17
|
+
if not app_id or not app_secret or not subject:
|
|
18
|
+
raise _error("app_id, app_secret and subject are required", source)
|
|
19
|
+
if not isinstance(expires_in, int) or expires_in < 60 or expires_in > 604800:
|
|
20
|
+
raise _error("expiresIn must be an integer between 60 and 604800", source)
|
|
21
|
+
issued_at = int(time.time())
|
|
22
|
+
expires_at = issued_at + expires_in
|
|
23
|
+
header = _encode({"alg": "HS256", "typ": "JWT"})
|
|
24
|
+
payload = _encode({**claims, "iss": issuer, "aud": audience,
|
|
25
|
+
"sub": subject, "iat": issued_at, "exp": expires_at})
|
|
26
|
+
content = f"{header}.{payload}"
|
|
27
|
+
signature = _b64(hmac.new(app_secret.encode(), content.encode(), hashlib.sha256).digest())
|
|
28
|
+
return f"{content}.{signature}", expires_at
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def iso_time(seconds: int) -> str:
|
|
32
|
+
return datetime.fromtimestamp(seconds, timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _encode(value: Any) -> str:
|
|
36
|
+
return _b64(json.dumps(value, separators=(",", ":"), ensure_ascii=False).encode())
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _b64(value: bytes) -> str:
|
|
40
|
+
return base64.urlsafe_b64encode(value).decode().rstrip("=")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _error(message: str, source: str) -> EduskitError:
|
|
44
|
+
return EduskitError(message, error_code="SDK_TOKEN_INPUT_INVALID", source=source)
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from .http import HttpTransport
|
|
6
|
+
from .errors import EduskitError
|
|
7
|
+
from .token import iso_time, sign_hs256
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class _Auth:
|
|
11
|
+
def __init__(self, app_id: str, app_secret: str) -> None:
|
|
12
|
+
self._app_id = app_id
|
|
13
|
+
self._app_secret = app_secret
|
|
14
|
+
|
|
15
|
+
def issue_room_token(self, **input: Any) -> Any:
|
|
16
|
+
room_id, user_id, role = input.get("roomId"), input.get("userId"), input.get("role")
|
|
17
|
+
if not room_id or not user_id or role not in ("host", "participant", "observer"):
|
|
18
|
+
raise EduskitError("roomId, userId and a valid role are required", error_code="SDK_TOKEN_INPUT_INVALID", source="whiteboard")
|
|
19
|
+
expires_in = input.get("expiresIn", 3600)
|
|
20
|
+
token, expires_at = sign_hs256(self._app_id, self._app_secret, user_id,
|
|
21
|
+
"eduskit", "eduskit-room", expires_in,
|
|
22
|
+
{"app_id": self._app_id, "room_id": room_id, "role": role, "source": "server_sdk"},
|
|
23
|
+
"whiteboard")
|
|
24
|
+
return {"token": token, "appId": self._app_id, "roomId": room_id, "userId": user_id,
|
|
25
|
+
"role": role, "expiresIn": expires_in, "expiresAt": iso_time(expires_at)}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class _Recordings:
|
|
29
|
+
def __init__(self, http: HttpTransport) -> None:
|
|
30
|
+
self._http = http
|
|
31
|
+
|
|
32
|
+
def start(self, room_id: str, **input: Any) -> Any:
|
|
33
|
+
return self._http.request("POST", f"/v1/rooms/{room_id}/recording/start", input)
|
|
34
|
+
|
|
35
|
+
def stop(self, room_id: str, **input: Any) -> Any:
|
|
36
|
+
return self._http.request("POST", f"/v1/rooms/{room_id}/recording/stop", input)
|
|
37
|
+
|
|
38
|
+
def list(self, room_id: str) -> Any:
|
|
39
|
+
return self._http.request("GET", f"/v1/rooms/{room_id}/recordings")
|
|
40
|
+
|
|
41
|
+
def get(self, recording_id: str) -> Any:
|
|
42
|
+
return self._http.request("GET", f"/v1/recordings/{recording_id}")
|
|
43
|
+
|
|
44
|
+
def register_media_asset(self, recording_id: str, **input: Any) -> Any:
|
|
45
|
+
return self._http.request(
|
|
46
|
+
"POST",
|
|
47
|
+
f"/v1/recordings/{recording_id}/media-assets",
|
|
48
|
+
input,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
def delete_media_asset(self, recording_id: str, asset_id: str) -> Any:
|
|
52
|
+
return self._http.request(
|
|
53
|
+
"DELETE",
|
|
54
|
+
f"/v1/recordings/{recording_id}/media-assets/{asset_id}",
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def enqueue_video_export(self, recording_id: str, **input: Any) -> Any:
|
|
58
|
+
return self._http.request(
|
|
59
|
+
"POST",
|
|
60
|
+
f"/v1/recordings/{recording_id}/video-exports",
|
|
61
|
+
input,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
def get_video_export(self, recording_id: str, job_id: str) -> Any:
|
|
65
|
+
return self._http.request(
|
|
66
|
+
"GET",
|
|
67
|
+
f"/v1/recordings/{recording_id}/video-exports/{job_id}",
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class _Captures:
|
|
72
|
+
def __init__(self, http: HttpTransport) -> None:
|
|
73
|
+
self._http = http
|
|
74
|
+
|
|
75
|
+
def create(self, room_id: str, **input: Any) -> Any:
|
|
76
|
+
body = {"roomId": room_id, **input}
|
|
77
|
+
return self._http.request("POST", f"/v1/rooms/{room_id}/captures", body)
|
|
78
|
+
|
|
79
|
+
def list(self, room_id: str) -> Any:
|
|
80
|
+
return self._http.request("GET", f"/v1/rooms/{room_id}/captures")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class _Files:
|
|
84
|
+
def __init__(self, http: HttpTransport) -> None:
|
|
85
|
+
self._http = http
|
|
86
|
+
|
|
87
|
+
def convert(self, **input: Any) -> Any:
|
|
88
|
+
return self._http.request("POST", "/v1/files/convert", input)
|
|
89
|
+
|
|
90
|
+
def get_convert_job(self, job_id: str) -> Any:
|
|
91
|
+
return self._http.request("GET", f"/v1/files/convert/{job_id}")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class WhiteboardClient:
|
|
95
|
+
def __init__(self, http: HttpTransport, *, app_id: str, app_secret: str) -> None:
|
|
96
|
+
self.auth = _Auth(app_id, app_secret)
|
|
97
|
+
self.recordings = _Recordings(http)
|
|
98
|
+
self.captures = _Captures(http)
|
|
99
|
+
self.files = _Files(http)
|
|
100
|
+
self._http = http
|
|
101
|
+
|
|
102
|
+
@property
|
|
103
|
+
def last_trace_id(self) -> str:
|
|
104
|
+
return self._http.last_trace_id
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: eduskit
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Eduskit B-end server SDK for classroom and whiteboard APIs
|
|
5
|
+
Author: Eduskit
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/eduskit/server-sdk-python
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Requires-Python: >=3.9
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# eduskit
|
|
15
|
+
|
|
16
|
+
Python B 端 Server SDK。`sdk.client` 调课堂 server-api,`sdk.whiteboard_client` 调白板 server-api。
|
|
17
|
+
|
|
18
|
+
方法名为 snake_case;**请求/响应 JSON 字段仍为 camelCase**(`originId`、`eduUserId`)。
|
|
19
|
+
|
|
20
|
+
概念与方法表见 [docs/overview.md](docs/overview.md)、[docs/api.md](docs/api.md)。
|
|
21
|
+
|
|
22
|
+
## 安装
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install -e .
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
要求 Python 3.9+,无第三方运行时依赖(标准库 `urllib`)。
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
PYTHONPATH=. python3 -m unittest tests.test_eduskit -v
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 初始化
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from eduskit import Eduskit, EduskitError
|
|
38
|
+
|
|
39
|
+
sdk = Eduskit(
|
|
40
|
+
timeout_ms=10_000,
|
|
41
|
+
lang="zh-CN",
|
|
42
|
+
client={
|
|
43
|
+
"base_url": "http://localhost:3112",
|
|
44
|
+
"app_id": "app_edu",
|
|
45
|
+
"app_key": "edu_key",
|
|
46
|
+
"app_secret": "edu_secret",
|
|
47
|
+
},
|
|
48
|
+
whiteboard_client={
|
|
49
|
+
"base_url": "http://localhost:3012",
|
|
50
|
+
"app_id": "app_wb",
|
|
51
|
+
"app_key": "wb_key",
|
|
52
|
+
"app_secret": "wb_secret",
|
|
53
|
+
},
|
|
54
|
+
)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
配置字典键是 snake_case(`base_url` / `app_id` / `app_key` / `app_secret`)。两侧均可选。
|
|
58
|
+
|
|
59
|
+
## 课堂 `sdk.client`
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
user = sdk.client.users.register(
|
|
63
|
+
originId="stu_001",
|
|
64
|
+
nickname="小明",
|
|
65
|
+
avatar="https://example.com/a.png",
|
|
66
|
+
)
|
|
67
|
+
token = sdk.client.auth.issue_token(eduUserId=user["eduUserId"], originId="stu_001")
|
|
68
|
+
classroom = sdk.client.classrooms.create(
|
|
69
|
+
name="一年级数学",
|
|
70
|
+
startsAt="2026-08-17T10:00:00.000Z",
|
|
71
|
+
endsAt="2026-08-17T11:00:00.000Z",
|
|
72
|
+
teacherEduUserId=user["eduUserId"],
|
|
73
|
+
)
|
|
74
|
+
sdk.client.classrooms.start(classroom["classroomId"])
|
|
75
|
+
sdk.client.classrooms.members.add(
|
|
76
|
+
classroom["classroomId"],
|
|
77
|
+
eduUserId=user["eduUserId"],
|
|
78
|
+
role="student",
|
|
79
|
+
)
|
|
80
|
+
sdk.client.classrooms.members.replace_students(
|
|
81
|
+
classroom["classroomId"],
|
|
82
|
+
eduUserIds=[user["eduUserId"]],
|
|
83
|
+
)
|
|
84
|
+
sdk.client.classrooms.permissions.set(
|
|
85
|
+
classroom["classroomId"],
|
|
86
|
+
user["eduUserId"],
|
|
87
|
+
permission="camera",
|
|
88
|
+
effect="grant",
|
|
89
|
+
operatorEduUserId=user["eduUserId"],
|
|
90
|
+
)
|
|
91
|
+
sdk.client.classrooms.coursewares.bind(
|
|
92
|
+
classroom["classroomId"],
|
|
93
|
+
coursewareIds=["cw_xxx"],
|
|
94
|
+
)
|
|
95
|
+
sdk.client.app.get_ui_config()
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
| 方法 | 对应 Node |
|
|
99
|
+
|------|-----------|
|
|
100
|
+
| `users.register(**input)` | `users.register` |
|
|
101
|
+
| `auth.issue_token(**input)` | `auth.issueToken` |
|
|
102
|
+
| `classrooms.create/start/end` | 同名 |
|
|
103
|
+
| `classrooms.members.add/list` | 同名 |
|
|
104
|
+
| `classrooms.members.replace_students` | `replaceStudents` |
|
|
105
|
+
| `classrooms.permissions.get/set/clear` | 同名 |
|
|
106
|
+
| `classrooms.coursewares.list/bind/unbind` | 同名 |
|
|
107
|
+
| `app.get_ui_config` / `set_ui_config` | `getUiConfig` / `setUiConfig` |
|
|
108
|
+
|
|
109
|
+
## 白板 `sdk.whiteboard_client`
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
credential = sdk.whiteboard_client.auth.issue_room_token(
|
|
113
|
+
roomId="room_1",
|
|
114
|
+
userId=user["eduUserId"],
|
|
115
|
+
role="host",
|
|
116
|
+
expiresIn=3600,
|
|
117
|
+
)
|
|
118
|
+
session = sdk.whiteboard_client.recordings.start("room_1", externalRef="lesson_001")
|
|
119
|
+
sdk.whiteboard_client.recordings.stop("room_1", recordingId=session["recordingId"])
|
|
120
|
+
sdk.whiteboard_client.recordings.enqueue_video_export(session["recordingId"], profile="hd")
|
|
121
|
+
sdk.whiteboard_client.files.convert(sourceUrl="https://cdn.example.com/lesson.pptx")
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
| 方法 | 对应 Node |
|
|
125
|
+
|------|-----------|
|
|
126
|
+
| `auth.issue_room_token` | `issueRoomToken` |
|
|
127
|
+
| `recordings.start/stop/list/get` | 同名 |
|
|
128
|
+
| `recordings.register_media_asset` / `delete_media_asset` | camelCase 版 |
|
|
129
|
+
| `recordings.enqueue_video_export` / `get_video_export` | camelCase 版 |
|
|
130
|
+
| `captures.create/list` | 同名 |
|
|
131
|
+
| `files.convert` / `get_convert_job` | `getConvertJob` |
|
|
132
|
+
|
|
133
|
+
## 错误
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
try:
|
|
137
|
+
sdk.client.auth.issue_token(eduUserId="")
|
|
138
|
+
except EduskitError as e:
|
|
139
|
+
print(e.error_code, e.status, e.trace_id, e.source)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`sdk.client.last_trace_id` / `sdk.whiteboard_client.last_trace_id`。
|
|
143
|
+
|
|
144
|
+
## 示例
|
|
145
|
+
|
|
146
|
+
- [examples/classroom_quickstart.py](examples/classroom_quickstart.py)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
eduskit/__init__.py
|
|
5
|
+
eduskit/classroom.py
|
|
6
|
+
eduskit/errors.py
|
|
7
|
+
eduskit/http.py
|
|
8
|
+
eduskit/token.py
|
|
9
|
+
eduskit/whiteboard.py
|
|
10
|
+
eduskit.egg-info/PKG-INFO
|
|
11
|
+
eduskit.egg-info/SOURCES.txt
|
|
12
|
+
eduskit.egg-info/dependency_links.txt
|
|
13
|
+
eduskit.egg-info/top_level.txt
|
|
14
|
+
tests/test_eduskit.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
eduskit
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "eduskit"
|
|
3
|
+
version = "0.1.2"
|
|
4
|
+
description = "Eduskit B-end server SDK for classroom and whiteboard APIs"
|
|
5
|
+
requires-python = ">=3.9"
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
authors = [{ name = "Eduskit" }]
|
|
10
|
+
classifiers = ["Programming Language :: Python :: 3"]
|
|
11
|
+
urls = { Repository = "https://github.com/eduskit/server-sdk-python" }
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
requires = ["setuptools>=77"]
|
|
15
|
+
build-backend = "setuptools.build_meta"
|
|
16
|
+
|
|
17
|
+
[tool.setuptools.packages.find]
|
|
18
|
+
include = ["eduskit*"]
|
eduskit-0.1.2/setup.cfg
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from eduskit import Eduskit, EduskitError
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def mock_fetch(body, status=200, headers=None):
|
|
8
|
+
calls = []
|
|
9
|
+
|
|
10
|
+
def fetch(url, method, req_headers, payload, timeout_ms):
|
|
11
|
+
calls.append(
|
|
12
|
+
{
|
|
13
|
+
"url": url,
|
|
14
|
+
"method": method,
|
|
15
|
+
"headers": req_headers,
|
|
16
|
+
"body": json.loads(payload.decode()) if payload else None,
|
|
17
|
+
}
|
|
18
|
+
)
|
|
19
|
+
return status, headers or {"x-trace-id": "wb-trace"}, json.dumps(body).encode()
|
|
20
|
+
|
|
21
|
+
return fetch, calls
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class EduskitTest(unittest.TestCase):
|
|
25
|
+
def test_classroom_register(self):
|
|
26
|
+
fetch, calls = mock_fetch(
|
|
27
|
+
{
|
|
28
|
+
"code": 0,
|
|
29
|
+
"data": {"eduUserId": "eu_1", "nickname": "n"},
|
|
30
|
+
"traceId": "body-trace",
|
|
31
|
+
}
|
|
32
|
+
)
|
|
33
|
+
sdk = Eduskit(
|
|
34
|
+
client={"base_url": "http://edu.test", "app_id": "app_edu", "app_key": "k", "app_secret": "secret-edu"},
|
|
35
|
+
fetch=fetch,
|
|
36
|
+
)
|
|
37
|
+
user = sdk.client.users.register(nickname="n", avatar="https://a")
|
|
38
|
+
self.assertEqual(user["eduUserId"], "eu_1")
|
|
39
|
+
self.assertEqual(sdk.client.last_trace_id, "body-trace")
|
|
40
|
+
self.assertEqual(calls[0]["url"], "http://edu.test/v1/users")
|
|
41
|
+
self.assertEqual(calls[0]["headers"]["x-app-key"], "k")
|
|
42
|
+
|
|
43
|
+
def test_whiteboard_issue_room_token(self):
|
|
44
|
+
fetch, calls = mock_fetch({"code": 0, "data": {"token": "rt"}})
|
|
45
|
+
sdk = Eduskit(
|
|
46
|
+
whiteboard_client={
|
|
47
|
+
"base_url": "http://wb.test",
|
|
48
|
+
"app_id": "app_wb",
|
|
49
|
+
"app_key": "wk",
|
|
50
|
+
"app_secret": "ws",
|
|
51
|
+
},
|
|
52
|
+
fetch=fetch,
|
|
53
|
+
)
|
|
54
|
+
token = sdk.whiteboard_client.auth.issue_room_token(
|
|
55
|
+
roomId="r1", userId="u1", role="host"
|
|
56
|
+
)
|
|
57
|
+
self.assertEqual(token["appId"], "app_wb")
|
|
58
|
+
self.assertEqual(len(token["token"].split(".")), 3)
|
|
59
|
+
self.assertEqual(calls, [])
|
|
60
|
+
|
|
61
|
+
def test_error_envelope(self):
|
|
62
|
+
fetch, _ = mock_fetch(
|
|
63
|
+
{
|
|
64
|
+
"code": 404,
|
|
65
|
+
"errorCode": "EDU_USER_NOT_FOUND",
|
|
66
|
+
"message": "missing",
|
|
67
|
+
"traceId": "err",
|
|
68
|
+
},
|
|
69
|
+
status=404,
|
|
70
|
+
)
|
|
71
|
+
sdk = Eduskit(
|
|
72
|
+
client={"base_url": "http://edu.test", "app_id": "app_edu", "app_key": "k", "app_secret": "secret-edu"},
|
|
73
|
+
fetch=fetch,
|
|
74
|
+
)
|
|
75
|
+
with self.assertRaises(EduskitError) as ctx:
|
|
76
|
+
sdk.client.auth.issue_token(eduUserId="")
|
|
77
|
+
self.assertEqual(ctx.exception.error_code, "SDK_TOKEN_INPUT_INVALID")
|
|
78
|
+
|
|
79
|
+
def test_unconfigured(self):
|
|
80
|
+
sdk = Eduskit(
|
|
81
|
+
client={"base_url": "http://edu.test", "app_id": "app_edu", "app_key": "k", "app_secret": "secret-edu"}
|
|
82
|
+
)
|
|
83
|
+
with self.assertRaises(EduskitError) as ctx:
|
|
84
|
+
_ = sdk.whiteboard_client
|
|
85
|
+
self.assertEqual(ctx.exception.error_code, "SDK_CLIENT_NOT_CONFIGURED")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
if __name__ == "__main__":
|
|
89
|
+
unittest.main()
|