magic-campus-cli 0.1.0__py3-none-any.whl
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.
- magic_campus_cli-0.1.0.dist-info/METADATA +51 -0
- magic_campus_cli-0.1.0.dist-info/RECORD +53 -0
- magic_campus_cli-0.1.0.dist-info/WHEEL +5 -0
- magic_campus_cli-0.1.0.dist-info/entry_points.txt +2 -0
- magic_campus_cli-0.1.0.dist-info/top_level.txt +2 -0
- magiccampus_cli/__init__.py +1 -0
- magiccampus_cli/__main__.py +9 -0
- magiccampus_cli/accounts.py +144 -0
- magiccampus_cli/cli.py +176 -0
- magiccampus_cli/client.py +54 -0
- magiccampus_cli/commands/auth.py +32 -0
- magiccampus_cli/commands/comment.py +38 -0
- magiccampus_cli/commands/config.py +95 -0
- magiccampus_cli/commands/feed.py +26 -0
- magiccampus_cli/commands/group.py +346 -0
- magiccampus_cli/commands/im.py +47 -0
- magiccampus_cli/commands/notify.py +81 -0
- magiccampus_cli/commands/playgroup.py +49 -0
- magiccampus_cli/commands/reaction.py +50 -0
- magiccampus_cli/commands/space.py +134 -0
- magiccampus_cli/commands/user.py +311 -0
- magiccampus_cli/config_store.py +61 -0
- magiccampus_cli/notify.py +493 -0
- magiccampus_cli/output.py +104 -0
- magiccampus_cli/prompts.py +58 -0
- magiccampus_cli/targets.py +26 -0
- magiccampus_cli/update.py +149 -0
- magiccampus_cli/utils.py +67 -0
- magiccampus_sdk/__init__.py +43 -0
- magiccampus_sdk/auth.py +111 -0
- magiccampus_sdk/comment_client.py +107 -0
- magiccampus_sdk/feed_api.py +61 -0
- magiccampus_sdk/feed_client.py +30 -0
- magiccampus_sdk/group_api.py +139 -0
- magiccampus_sdk/group_client.py +242 -0
- magiccampus_sdk/http/__init__.py +1 -0
- magiccampus_sdk/http/group_info_api.py +47 -0
- magiccampus_sdk/http/http_api.py +58 -0
- magiccampus_sdk/http/http_send.py +758 -0
- magiccampus_sdk/http/media.py +839 -0
- magiccampus_sdk/http/models.py +645 -0
- magiccampus_sdk/http/self_user_info_api.py +182 -0
- magiccampus_sdk/http/text.py +75 -0
- magiccampus_sdk/models.py +279 -0
- magiccampus_sdk/platform_client.py +137 -0
- magiccampus_sdk/platform_http.py +71 -0
- magiccampus_sdk/playgroup_api.py +104 -0
- magiccampus_sdk/playgroup_client.py +45 -0
- magiccampus_sdk/reaction_api.py +110 -0
- magiccampus_sdk/space_api.py +170 -0
- magiccampus_sdk/space_client.py +93 -0
- magiccampus_sdk/user_client.py +211 -0
- magiccampus_sdk/user_info_api.py +593 -0
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from datetime import date
|
|
5
|
+
import re
|
|
6
|
+
from typing import Any, Callable
|
|
7
|
+
from urllib.parse import quote
|
|
8
|
+
|
|
9
|
+
import httpx
|
|
10
|
+
|
|
11
|
+
from .auth import normalize_apiurl
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
ACADEMY_CODES = (
|
|
15
|
+
"Material",
|
|
16
|
+
"Digital",
|
|
17
|
+
"Sound",
|
|
18
|
+
"Movement",
|
|
19
|
+
"Nature",
|
|
20
|
+
"Healing",
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def normalize_academy_code(value: object) -> str:
|
|
25
|
+
raw = str(value or "").strip()
|
|
26
|
+
if not raw:
|
|
27
|
+
raise ValueError("academy_code is required")
|
|
28
|
+
academy_code = {code.lower(): code for code in ACADEMY_CODES}.get(raw.lower())
|
|
29
|
+
if academy_code is None:
|
|
30
|
+
raise ValueError(f"academy_code must be one of: {', '.join(ACADEMY_CODES)}")
|
|
31
|
+
return academy_code
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def initialize_growth_profile(
|
|
35
|
+
*,
|
|
36
|
+
apiurl: str,
|
|
37
|
+
apikey: str,
|
|
38
|
+
user_id: str | int,
|
|
39
|
+
reset: bool = False,
|
|
40
|
+
timeout_seconds: float,
|
|
41
|
+
make_http_error: Callable[[int, str], Exception],
|
|
42
|
+
make_sdk_error: Callable[[int, str, str], Exception],
|
|
43
|
+
) -> dict[str, object]:
|
|
44
|
+
normalized_apiurl = normalize_apiurl(apiurl)
|
|
45
|
+
normalized_apikey = str(apikey or "").strip()
|
|
46
|
+
if not normalized_apikey:
|
|
47
|
+
raise ValueError("magiccampus apikey is required")
|
|
48
|
+
if isinstance(user_id, bool) or not str(user_id or "").strip():
|
|
49
|
+
raise ValueError("user_id is required")
|
|
50
|
+
normalized_user_id = _normalize_integer(user_id, name="user_id")
|
|
51
|
+
if not isinstance(reset, bool):
|
|
52
|
+
raise ValueError("reset must be a boolean")
|
|
53
|
+
|
|
54
|
+
response = httpx.post(
|
|
55
|
+
f"{normalized_apiurl}/api/v1/growth/profile/init",
|
|
56
|
+
json={"userId": normalized_user_id, "reset": reset},
|
|
57
|
+
headers={
|
|
58
|
+
"Authorization": f"Bearer {normalized_apikey}",
|
|
59
|
+
"Content-Type": "application/json",
|
|
60
|
+
},
|
|
61
|
+
timeout=timeout_seconds,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
response.raise_for_status()
|
|
66
|
+
except httpx.HTTPStatusError as exc:
|
|
67
|
+
raise make_http_error(exc.response.status_code, exc.response.text) from exc
|
|
68
|
+
|
|
69
|
+
payload = response.json()
|
|
70
|
+
if not isinstance(payload, dict):
|
|
71
|
+
raise ValueError("magiccampus initialize growth profile response must be a JSON object")
|
|
72
|
+
|
|
73
|
+
code = int(payload.get("code", 0) or 0)
|
|
74
|
+
message = str(payload.get("msg", "") or "")
|
|
75
|
+
if code != 200:
|
|
76
|
+
raise make_sdk_error(code, message, str(payload.get("detail", "") or ""))
|
|
77
|
+
return {"code": code, "msg": message}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def set_user_academy(
|
|
81
|
+
*,
|
|
82
|
+
apiurl: str,
|
|
83
|
+
apikey: str,
|
|
84
|
+
user_id: str | int,
|
|
85
|
+
academy_code: str,
|
|
86
|
+
timeout_seconds: float,
|
|
87
|
+
make_http_error: Callable[[int, str], Exception],
|
|
88
|
+
make_sdk_error: Callable[[int, str, str], Exception],
|
|
89
|
+
) -> dict[str, object]:
|
|
90
|
+
normalized_apiurl = normalize_apiurl(apiurl)
|
|
91
|
+
normalized_apikey = str(apikey or "").strip()
|
|
92
|
+
if not normalized_apikey:
|
|
93
|
+
raise ValueError("magiccampus apikey is required")
|
|
94
|
+
if isinstance(user_id, bool) or not str(user_id or "").strip():
|
|
95
|
+
raise ValueError("user_id is required")
|
|
96
|
+
normalized_user_id = _normalize_integer(user_id, name="user_id")
|
|
97
|
+
normalized_academy_code = normalize_academy_code(academy_code)
|
|
98
|
+
|
|
99
|
+
response = httpx.post(
|
|
100
|
+
f"{normalized_apiurl}/api/v1/me/academy",
|
|
101
|
+
json={
|
|
102
|
+
"academyCode": normalized_academy_code,
|
|
103
|
+
},
|
|
104
|
+
headers={
|
|
105
|
+
"Authorization": f"Bearer {normalized_apikey}",
|
|
106
|
+
"Content-Type": "application/json",
|
|
107
|
+
},
|
|
108
|
+
timeout=timeout_seconds,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
try:
|
|
112
|
+
response.raise_for_status()
|
|
113
|
+
except httpx.HTTPStatusError as exc:
|
|
114
|
+
raise make_http_error(exc.response.status_code, exc.response.text) from exc
|
|
115
|
+
|
|
116
|
+
payload = response.json()
|
|
117
|
+
if not isinstance(payload, dict):
|
|
118
|
+
raise ValueError("magiccampus set academy response must be a JSON object")
|
|
119
|
+
|
|
120
|
+
code = int(payload.get("code", 0) or 0)
|
|
121
|
+
message = str(payload.get("msg", "") or "")
|
|
122
|
+
if code != 200:
|
|
123
|
+
raise make_sdk_error(code, message, str(payload.get("detail", "") or ""))
|
|
124
|
+
return {"code": code, "msg": message}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def fetch_growth_profile(
|
|
128
|
+
*,
|
|
129
|
+
apiurl: str,
|
|
130
|
+
apikey: str,
|
|
131
|
+
user_id: str | int,
|
|
132
|
+
timeout_seconds: float,
|
|
133
|
+
make_http_error: Callable[[int, str], Exception],
|
|
134
|
+
make_sdk_error: Callable[[int, str, str], Exception],
|
|
135
|
+
make_not_found_error: Callable[[str], Exception],
|
|
136
|
+
) -> dict[str, Any]:
|
|
137
|
+
normalized_apiurl = normalize_apiurl(apiurl)
|
|
138
|
+
normalized_apikey = str(apikey or "").strip()
|
|
139
|
+
if not normalized_apikey:
|
|
140
|
+
raise ValueError("magiccampus apikey is required")
|
|
141
|
+
if isinstance(user_id, bool) or not str(user_id or "").strip():
|
|
142
|
+
raise ValueError("user_id is required")
|
|
143
|
+
normalized_user_id = _normalize_integer(user_id, name="user_id")
|
|
144
|
+
|
|
145
|
+
response = httpx.get(
|
|
146
|
+
f"{normalized_apiurl}/api/v1/growth/profile",
|
|
147
|
+
params={"userId": normalized_user_id},
|
|
148
|
+
headers={
|
|
149
|
+
"Authorization": f"Bearer {normalized_apikey}",
|
|
150
|
+
"Content-Type": "application/json",
|
|
151
|
+
},
|
|
152
|
+
timeout=timeout_seconds,
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
try:
|
|
156
|
+
response.raise_for_status()
|
|
157
|
+
except httpx.HTTPStatusError as exc:
|
|
158
|
+
raise make_http_error(exc.response.status_code, exc.response.text) from exc
|
|
159
|
+
|
|
160
|
+
payload = response.json()
|
|
161
|
+
if not isinstance(payload, dict):
|
|
162
|
+
raise ValueError("magiccampus growth profile response must be a JSON object")
|
|
163
|
+
|
|
164
|
+
code = int(payload.get("code", 0) or 0)
|
|
165
|
+
if code != 200:
|
|
166
|
+
raise make_sdk_error(
|
|
167
|
+
code,
|
|
168
|
+
str(payload.get("msg", "") or ""),
|
|
169
|
+
str(payload.get("detail", "") or ""),
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
data = payload.get("data")
|
|
173
|
+
if data is None:
|
|
174
|
+
raise make_not_found_error(str(normalized_user_id))
|
|
175
|
+
if not isinstance(data, dict):
|
|
176
|
+
raise ValueError("magiccampus growth profile response data must be a JSON object")
|
|
177
|
+
return data
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def adjust_user_elements(
|
|
181
|
+
*,
|
|
182
|
+
apiurl: str,
|
|
183
|
+
apikey: str,
|
|
184
|
+
user_id: str | int,
|
|
185
|
+
metal_delta: int | None = None,
|
|
186
|
+
wood_delta: int | None = None,
|
|
187
|
+
water_delta: int | None = None,
|
|
188
|
+
fire_delta: int | None = None,
|
|
189
|
+
earth_delta: int | None = None,
|
|
190
|
+
timeout_seconds: float,
|
|
191
|
+
make_http_error: Callable[[int, str], Exception],
|
|
192
|
+
make_sdk_error: Callable[[int, str, str], Exception],
|
|
193
|
+
) -> dict[str, object]:
|
|
194
|
+
normalized_apiurl = normalize_apiurl(apiurl)
|
|
195
|
+
normalized_apikey = str(apikey or "").strip()
|
|
196
|
+
if not normalized_apikey:
|
|
197
|
+
raise ValueError("magiccampus apikey is required")
|
|
198
|
+
if isinstance(user_id, bool) or not str(user_id or "").strip():
|
|
199
|
+
raise ValueError("user_id is required")
|
|
200
|
+
normalized_user_id = _normalize_integer(user_id, name="user_id")
|
|
201
|
+
|
|
202
|
+
deltas = (
|
|
203
|
+
("metalDelta", "metal_delta", metal_delta),
|
|
204
|
+
("woodDelta", "wood_delta", wood_delta),
|
|
205
|
+
("waterDelta", "water_delta", water_delta),
|
|
206
|
+
("fireDelta", "fire_delta", fire_delta),
|
|
207
|
+
("earthDelta", "earth_delta", earth_delta),
|
|
208
|
+
)
|
|
209
|
+
if all(value is None for _, _, value in deltas):
|
|
210
|
+
raise ValueError("at least one element delta is required")
|
|
211
|
+
|
|
212
|
+
body = {"userId": normalized_user_id}
|
|
213
|
+
for field, argument_name, value in deltas:
|
|
214
|
+
if value is not None:
|
|
215
|
+
body[field] = _normalize_integer(value, name=argument_name)
|
|
216
|
+
|
|
217
|
+
response = httpx.post(
|
|
218
|
+
f"{normalized_apiurl}/api/v1/growth/profile/elements/adjust",
|
|
219
|
+
json=body,
|
|
220
|
+
headers={
|
|
221
|
+
"Authorization": f"Bearer {normalized_apikey}",
|
|
222
|
+
"Content-Type": "application/json",
|
|
223
|
+
},
|
|
224
|
+
timeout=timeout_seconds,
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
try:
|
|
228
|
+
response.raise_for_status()
|
|
229
|
+
except httpx.HTTPStatusError as exc:
|
|
230
|
+
raise make_http_error(exc.response.status_code, exc.response.text) from exc
|
|
231
|
+
|
|
232
|
+
payload = response.json()
|
|
233
|
+
if not isinstance(payload, dict):
|
|
234
|
+
raise ValueError("magiccampus adjust elements response must be a JSON object")
|
|
235
|
+
|
|
236
|
+
code = int(payload.get("code", 0) or 0)
|
|
237
|
+
message = str(payload.get("msg", "") or "")
|
|
238
|
+
if code != 200:
|
|
239
|
+
raise make_sdk_error(code, message, str(payload.get("detail", "") or ""))
|
|
240
|
+
|
|
241
|
+
return {"code": code, "msg": message}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _normalize_integer(value: object, *, name: str) -> int:
|
|
245
|
+
if isinstance(value, bool):
|
|
246
|
+
raise ValueError(f"{name} must be an integer")
|
|
247
|
+
try:
|
|
248
|
+
return int(str(value).strip())
|
|
249
|
+
except (TypeError, ValueError) as exc:
|
|
250
|
+
raise ValueError(f"{name} must be an integer") from exc
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
USER_INFO_FIELDS = {"nickname", "avatarUrl", "introduction", "birth", "gender", "tags"}
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def validate_user_info(info: Mapping[str, Any]) -> dict[str, Any]:
|
|
257
|
+
"""Validate the writable fields; response/control fields and target IDs are unsupported."""
|
|
258
|
+
if not isinstance(info, Mapping) or not info or info.keys() - USER_INFO_FIELDS:
|
|
259
|
+
raise ValueError("user info must contain only nickname, avatarUrl, introduction, birth, gender or tags")
|
|
260
|
+
body = {key: value for key, value in info.items() if value is not None}
|
|
261
|
+
if not body:
|
|
262
|
+
raise ValueError("at least one non-null user info field is required")
|
|
263
|
+
for key, limit in (("nickname", 30), ("avatarUrl", 512), ("introduction", 512), ("birth", 10)):
|
|
264
|
+
if key not in body:
|
|
265
|
+
continue
|
|
266
|
+
value = body[key]
|
|
267
|
+
if not isinstance(value, str):
|
|
268
|
+
raise ValueError(f"{key} must be a string")
|
|
269
|
+
if key in {"nickname", "avatarUrl"}:
|
|
270
|
+
value = value.strip()
|
|
271
|
+
if len(value.encode("utf-16-le", errors="surrogatepass")) // 2 > limit:
|
|
272
|
+
raise ValueError(f"{key} must be at most {limit} UTF-16 code units")
|
|
273
|
+
if key == "nickname" and not value:
|
|
274
|
+
raise ValueError("nickname must not be blank")
|
|
275
|
+
body[key] = value
|
|
276
|
+
if "birth" in body and body["birth"].strip():
|
|
277
|
+
if not re.fullmatch(r"[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}", body["birth"]):
|
|
278
|
+
raise ValueError("birth must be a valid date in yyyy-MM-dd format")
|
|
279
|
+
try:
|
|
280
|
+
body["birth"] = date(*map(int, body["birth"].split("-"))).isoformat()
|
|
281
|
+
except ValueError as exc:
|
|
282
|
+
raise ValueError("birth must be a valid date in yyyy-MM-dd format") from exc
|
|
283
|
+
if "gender" in body and (not isinstance(body["gender"], str) or body["gender"] not in {"MALE", "FEMALE", "OTHER", "UNKNOWN"}):
|
|
284
|
+
raise ValueError("gender must be MALE, FEMALE, OTHER or UNKNOWN")
|
|
285
|
+
if "tags" in body:
|
|
286
|
+
tags = body["tags"]
|
|
287
|
+
if not isinstance(tags, list) or len(tags) > 10:
|
|
288
|
+
raise ValueError("tags must be a list with at most 10 strings")
|
|
289
|
+
if any(not isinstance(tag, str) or not tag.strip() or "," in tag
|
|
290
|
+
or len(tag.encode("utf-16-le", errors="surrogatepass")) // 2 > 20 for tag in tags):
|
|
291
|
+
raise ValueError("tags must be non-blank strings of at most 20 UTF-16 code units without commas")
|
|
292
|
+
return body
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def update_user_info(
|
|
296
|
+
*, apiurl: str, apikey: str, info: Mapping[str, Any], timeout_seconds: float,
|
|
297
|
+
make_http_error: Callable[[int, str], Exception],
|
|
298
|
+
make_sdk_error: Callable[[int, str, str], Exception],
|
|
299
|
+
) -> dict[str, Any]:
|
|
300
|
+
body = validate_user_info(info)
|
|
301
|
+
normalized_apiurl = normalize_apiurl(apiurl)
|
|
302
|
+
normalized_apikey = str(apikey or "").strip()
|
|
303
|
+
if not normalized_apikey:
|
|
304
|
+
raise ValueError("magiccampus apikey is required")
|
|
305
|
+
response = httpx.post(
|
|
306
|
+
f"{normalized_apiurl}/api/v1/accounts/updateUserInfo", json=body,
|
|
307
|
+
headers={"Authorization": f"Bearer {normalized_apikey}", "Content-Type": "application/json"},
|
|
308
|
+
timeout=timeout_seconds,
|
|
309
|
+
)
|
|
310
|
+
try:
|
|
311
|
+
response.raise_for_status()
|
|
312
|
+
except httpx.HTTPStatusError as exc:
|
|
313
|
+
raise make_http_error(exc.response.status_code, exc.response.text) from exc
|
|
314
|
+
payload = response.json()
|
|
315
|
+
if not isinstance(payload, dict):
|
|
316
|
+
raise ValueError("magiccampus update user info response must be a JSON object")
|
|
317
|
+
code = int(payload.get("code", 0) or 0)
|
|
318
|
+
if code != 200:
|
|
319
|
+
raise make_sdk_error(code, str(payload.get("msg", "") or ""), str(payload.get("detail", "") or ""))
|
|
320
|
+
data = payload.get("data")
|
|
321
|
+
if not isinstance(data, dict):
|
|
322
|
+
raise ValueError("magiccampus update user info response data must be a JSON object")
|
|
323
|
+
return data
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def update_user_profile(
|
|
327
|
+
*,
|
|
328
|
+
apiurl: str,
|
|
329
|
+
apikey: str,
|
|
330
|
+
user_id: str,
|
|
331
|
+
nickname: str | None,
|
|
332
|
+
avatar_url: str | None,
|
|
333
|
+
timeout_seconds: float,
|
|
334
|
+
make_http_error: Callable[[int, str], Exception],
|
|
335
|
+
make_sdk_error: Callable[[int, str, str], Exception],
|
|
336
|
+
) -> Any:
|
|
337
|
+
normalized_apiurl = normalize_apiurl(apiurl)
|
|
338
|
+
normalized_apikey = str(apikey or "").strip()
|
|
339
|
+
normalized_user_id = str(user_id or "").strip()
|
|
340
|
+
if not normalized_apikey:
|
|
341
|
+
raise ValueError("magiccampus apikey is required")
|
|
342
|
+
if not normalized_user_id:
|
|
343
|
+
raise ValueError("user_id is required")
|
|
344
|
+
if nickname is None and avatar_url is None:
|
|
345
|
+
raise ValueError("nickname or avatar_url is required")
|
|
346
|
+
|
|
347
|
+
body: dict[str, str] = {}
|
|
348
|
+
if nickname is not None:
|
|
349
|
+
normalized_nickname = str(nickname).strip()
|
|
350
|
+
if len(normalized_nickname) > 30:
|
|
351
|
+
raise ValueError("nickname must be at most 30 characters")
|
|
352
|
+
body["nickname"] = normalized_nickname
|
|
353
|
+
if avatar_url is not None:
|
|
354
|
+
normalized_avatar_url = str(avatar_url).strip()
|
|
355
|
+
if len(normalized_avatar_url) > 512:
|
|
356
|
+
raise ValueError("avatar_url must be at most 512 characters")
|
|
357
|
+
body["avatarUrl"] = normalized_avatar_url
|
|
358
|
+
|
|
359
|
+
response = httpx.patch(
|
|
360
|
+
f"{normalized_apiurl}/api/v1/accounts/{quote(normalized_user_id, safe='')}/profile",
|
|
361
|
+
json=body,
|
|
362
|
+
headers={
|
|
363
|
+
"Authorization": f"Bearer {normalized_apikey}",
|
|
364
|
+
"Content-Type": "application/json",
|
|
365
|
+
},
|
|
366
|
+
timeout=timeout_seconds,
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
try:
|
|
370
|
+
response.raise_for_status()
|
|
371
|
+
except httpx.HTTPStatusError as exc:
|
|
372
|
+
raise make_http_error(exc.response.status_code, exc.response.text) from exc
|
|
373
|
+
|
|
374
|
+
payload = response.json()
|
|
375
|
+
if not isinstance(payload, dict):
|
|
376
|
+
raise ValueError("magiccampus update profile response must be a JSON object")
|
|
377
|
+
|
|
378
|
+
code = int(payload.get("code", 0) or 0)
|
|
379
|
+
if code != 200:
|
|
380
|
+
raise make_sdk_error(
|
|
381
|
+
code,
|
|
382
|
+
str(payload.get("msg", "") or ""),
|
|
383
|
+
str(payload.get("detail", "") or ""),
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
return payload.get("data")
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def validate_user_ext(ext: Mapping[str, str]) -> dict[str, str]:
|
|
390
|
+
if not isinstance(ext, Mapping) or not 1 <= len(ext) <= 100:
|
|
391
|
+
raise ValueError("ext must be a non-empty mapping with at most 100 entries")
|
|
392
|
+
for key, value in ext.items():
|
|
393
|
+
# Java String.length() counts UTF-16 code units, including surrogate pairs.
|
|
394
|
+
if (
|
|
395
|
+
not isinstance(key, str) or not key.strip()
|
|
396
|
+
or len(key.encode("utf-16-le", errors="surrogatepass")) // 2 > 64
|
|
397
|
+
):
|
|
398
|
+
raise ValueError("ext keys must be non-blank strings of at most 64 UTF-16 code units")
|
|
399
|
+
if key in {"initial_feed_recommended", "source"}:
|
|
400
|
+
raise ValueError("ext contains a reserved key")
|
|
401
|
+
if (
|
|
402
|
+
not isinstance(value, str)
|
|
403
|
+
or len(value.encode("utf-16-le", errors="surrogatepass")) // 2 > 4096
|
|
404
|
+
):
|
|
405
|
+
raise ValueError("ext values must be strings of at most 4096 UTF-16 code units")
|
|
406
|
+
return dict(ext)
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def update_user_ext(
|
|
410
|
+
*,
|
|
411
|
+
apiurl: str,
|
|
412
|
+
apikey: str,
|
|
413
|
+
ext: Mapping[str, str],
|
|
414
|
+
timeout_seconds: float,
|
|
415
|
+
make_http_error: Callable[[int, str], Exception],
|
|
416
|
+
make_sdk_error: Callable[[int, str, str], Exception],
|
|
417
|
+
) -> dict[str, object]:
|
|
418
|
+
normalized_apiurl = normalize_apiurl(apiurl)
|
|
419
|
+
normalized_apikey = str(apikey or "").strip()
|
|
420
|
+
if not normalized_apikey:
|
|
421
|
+
raise ValueError("magiccampus apikey is required")
|
|
422
|
+
body = validate_user_ext(ext)
|
|
423
|
+
response = httpx.post(
|
|
424
|
+
f"{normalized_apiurl}/api/v1/accounts/ext",
|
|
425
|
+
json=body,
|
|
426
|
+
headers={
|
|
427
|
+
"Authorization": f"Bearer {normalized_apikey}",
|
|
428
|
+
"Content-Type": "application/json",
|
|
429
|
+
},
|
|
430
|
+
timeout=timeout_seconds,
|
|
431
|
+
)
|
|
432
|
+
try:
|
|
433
|
+
response.raise_for_status()
|
|
434
|
+
except httpx.HTTPStatusError as exc:
|
|
435
|
+
raise make_http_error(exc.response.status_code, exc.response.text) from exc
|
|
436
|
+
|
|
437
|
+
payload = response.json()
|
|
438
|
+
if not isinstance(payload, dict):
|
|
439
|
+
raise ValueError("magiccampus update ext response must be a JSON object")
|
|
440
|
+
code = int(payload.get("code", 0) or 0)
|
|
441
|
+
message = str(payload.get("msg", "") or "")
|
|
442
|
+
if code != 200:
|
|
443
|
+
raise make_sdk_error(code, message, str(payload.get("detail", "") or ""))
|
|
444
|
+
return {"code": code, "msg": message}
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def fetch_user_info(
|
|
448
|
+
*,
|
|
449
|
+
apiurl: str,
|
|
450
|
+
apikey: str,
|
|
451
|
+
user_id: str,
|
|
452
|
+
timeout_seconds: float,
|
|
453
|
+
make_http_error: Callable[[int, str], Exception],
|
|
454
|
+
make_sdk_error: Callable[[int, str, str], Exception],
|
|
455
|
+
make_not_found_error: Callable[[str], Exception],
|
|
456
|
+
) -> dict[str, Any]:
|
|
457
|
+
normalized_apiurl = normalize_apiurl(apiurl)
|
|
458
|
+
normalized_apikey = str(apikey or "").strip()
|
|
459
|
+
normalized_user_id = str(user_id or "").strip()
|
|
460
|
+
if not normalized_apikey:
|
|
461
|
+
raise ValueError("magiccampus apikey is required")
|
|
462
|
+
if not normalized_user_id:
|
|
463
|
+
raise ValueError("user_id is required")
|
|
464
|
+
|
|
465
|
+
response = httpx.get(
|
|
466
|
+
f"{normalized_apiurl}/api/v1/accounts/getUserInfo",
|
|
467
|
+
params={"userId": normalized_user_id},
|
|
468
|
+
headers={"Authorization": f"Bearer {normalized_apikey}"},
|
|
469
|
+
timeout=timeout_seconds,
|
|
470
|
+
)
|
|
471
|
+
|
|
472
|
+
try:
|
|
473
|
+
response.raise_for_status()
|
|
474
|
+
except httpx.HTTPStatusError as exc:
|
|
475
|
+
raise make_http_error(exc.response.status_code, exc.response.text) from exc
|
|
476
|
+
|
|
477
|
+
payload = response.json()
|
|
478
|
+
if not isinstance(payload, dict):
|
|
479
|
+
raise ValueError("magiccampus getUserInfo response must be a JSON object")
|
|
480
|
+
|
|
481
|
+
code = int(payload.get("code", 0) or 0)
|
|
482
|
+
if code != 200:
|
|
483
|
+
raise make_sdk_error(code, str(payload.get("msg", "") or ""), "")
|
|
484
|
+
|
|
485
|
+
data = payload.get("data")
|
|
486
|
+
if data is None:
|
|
487
|
+
raise make_not_found_error(normalized_user_id)
|
|
488
|
+
if not isinstance(data, dict):
|
|
489
|
+
raise ValueError("magiccampus getUserInfo response data must be a JSON object or null")
|
|
490
|
+
|
|
491
|
+
return data
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
def fetch_all_users_page(
|
|
495
|
+
*,
|
|
496
|
+
apiurl: str,
|
|
497
|
+
apikey: str,
|
|
498
|
+
page: int,
|
|
499
|
+
academy_code: str | None = None,
|
|
500
|
+
timeout_seconds: float,
|
|
501
|
+
make_http_error: Callable[[int, str], Exception],
|
|
502
|
+
make_sdk_error: Callable[[int, str, str], Exception],
|
|
503
|
+
) -> dict[str, Any]:
|
|
504
|
+
normalized_apiurl = normalize_apiurl(apiurl)
|
|
505
|
+
normalized_apikey = str(apikey or "").strip()
|
|
506
|
+
try:
|
|
507
|
+
normalized_page = int(page)
|
|
508
|
+
except (TypeError, ValueError) as exc:
|
|
509
|
+
raise ValueError("page must be a positive integer") from exc
|
|
510
|
+
if not normalized_apikey:
|
|
511
|
+
raise ValueError("magiccampus apikey is required")
|
|
512
|
+
if normalized_page < 1:
|
|
513
|
+
raise ValueError("page must be a positive integer")
|
|
514
|
+
|
|
515
|
+
params: dict[str, object] = {"page": normalized_page}
|
|
516
|
+
normalized_academy_code = str(academy_code or "").strip()
|
|
517
|
+
if normalized_academy_code:
|
|
518
|
+
params["academyCode"] = normalized_academy_code
|
|
519
|
+
|
|
520
|
+
response = httpx.get(
|
|
521
|
+
f"{normalized_apiurl}/api/v1/accounts/allUsers",
|
|
522
|
+
params=params,
|
|
523
|
+
headers={"Authorization": f"Bearer {normalized_apikey}"},
|
|
524
|
+
timeout=timeout_seconds,
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
try:
|
|
528
|
+
response.raise_for_status()
|
|
529
|
+
except httpx.HTTPStatusError as exc:
|
|
530
|
+
raise make_http_error(exc.response.status_code, exc.response.text) from exc
|
|
531
|
+
|
|
532
|
+
payload = response.json()
|
|
533
|
+
if not isinstance(payload, dict):
|
|
534
|
+
raise ValueError("magiccampus allUsers response must be a JSON object")
|
|
535
|
+
|
|
536
|
+
code = int(payload.get("code", 0) or 0)
|
|
537
|
+
if code != 200:
|
|
538
|
+
raise make_sdk_error(code, str(payload.get("msg", "") or ""), "")
|
|
539
|
+
|
|
540
|
+
data = payload.get("data")
|
|
541
|
+
if not isinstance(data, dict):
|
|
542
|
+
raise ValueError("magiccampus allUsers response data must be a JSON object")
|
|
543
|
+
|
|
544
|
+
return data
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
def fetch_random_users(
|
|
548
|
+
*,
|
|
549
|
+
apiurl: str,
|
|
550
|
+
apikey: str,
|
|
551
|
+
filter_no_face_url: bool = True,
|
|
552
|
+
academy_code: str | None = None,
|
|
553
|
+
timeout_seconds: float,
|
|
554
|
+
make_http_error: Callable[[int, str], Exception],
|
|
555
|
+
make_sdk_error: Callable[[int, str, str], Exception],
|
|
556
|
+
) -> list[dict[str, Any]]:
|
|
557
|
+
normalized_apiurl = normalize_apiurl(apiurl)
|
|
558
|
+
normalized_apikey = str(apikey or "").strip()
|
|
559
|
+
if not normalized_apikey:
|
|
560
|
+
raise ValueError("magiccampus apikey is required")
|
|
561
|
+
|
|
562
|
+
params: dict[str, object] = {"filterNoFaceURL": filter_no_face_url}
|
|
563
|
+
normalized_academy_code = str(academy_code or "").strip()
|
|
564
|
+
if normalized_academy_code:
|
|
565
|
+
params["academyCode"] = normalized_academy_code
|
|
566
|
+
|
|
567
|
+
response = httpx.get(
|
|
568
|
+
f"{normalized_apiurl}/api/v1/accounts/randomUsers",
|
|
569
|
+
params=params,
|
|
570
|
+
headers={"Authorization": f"Bearer {normalized_apikey}"},
|
|
571
|
+
timeout=timeout_seconds,
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
try:
|
|
575
|
+
response.raise_for_status()
|
|
576
|
+
except httpx.HTTPStatusError as exc:
|
|
577
|
+
raise make_http_error(exc.response.status_code, exc.response.text) from exc
|
|
578
|
+
|
|
579
|
+
payload = response.json()
|
|
580
|
+
if not isinstance(payload, dict):
|
|
581
|
+
raise ValueError("magiccampus randomUsers response must be a JSON object")
|
|
582
|
+
|
|
583
|
+
code = int(payload.get("code", 0) or 0)
|
|
584
|
+
if code != 200:
|
|
585
|
+
raise make_sdk_error(code, str(payload.get("msg", "") or ""), "")
|
|
586
|
+
|
|
587
|
+
data = payload.get("data")
|
|
588
|
+
if not isinstance(data, list):
|
|
589
|
+
raise ValueError("magiccampus randomUsers response data must be a JSON array")
|
|
590
|
+
if any(not isinstance(user, dict) for user in data):
|
|
591
|
+
raise ValueError("magiccampus randomUsers response data items must be JSON objects")
|
|
592
|
+
|
|
593
|
+
return data
|