Skip to content

Commit fbac78d

Browse files
author
Arendelle
committed
refactor User
* sync some funcsig of async methods * generate docs * remove User.star, add param star_type in User.star_content
1 parent 04dee69 commit fbac78d

File tree

4 files changed

+50
-88
lines changed

4 files changed

+50
-88
lines changed

docs/docsgen/user-method.md

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def get_comments(self, target_id: str, target_type: str, take: int = 16, skip: i
3333

3434
对应的协程风格的api:
3535
```Python
36-
async def async_get_comments(self, target_id: str, target_type: str, take: int = 16, skip: int = 0)
36+
async def async_get_comments(self, target_id: str, target_type: str, take: int = 16, skip: int = 0, comment_id: Optional[str] = None)
3737
```
3838

3939
## 获取作品的详细信息, 物实第一次读取作品会使用此接口
@@ -83,7 +83,7 @@ async def async_get_message(self, message_id: str)
8383

8484
## 获取用户收到的消息
8585
```Python
86-
def get_messages(self, category_id: int = 0, skip: int = 0, take: int = 16, no_templates: bool = True) -> dict
86+
def get_messages(self, category_id: int, skip: int = 0, take: int = 16, no_templates: bool = True) -> dict
8787
```
8888
@param category_id: 消息类型:
8989
0: 全部, 1: 系统邮件, 2: 关注和粉丝, 3: 评论和回复, 4: 作品通知, 5: 管理记录
@@ -93,7 +93,7 @@ def get_messages(self, category_id: int = 0, skip: int = 0, take: int = 16, no_t
9393

9494
对应的协程风格的api:
9595
```Python
96-
async def async_get_messages(self, category_id: int = 0, skip: int = 0, take: int = 16, no_templates: bool = True)
96+
async def async_get_messages(self, category_id: int, skip: int = 0, take: int = 16, no_templates: bool = True)
9797
```
9898

9999
## 获取用户主页信息
@@ -159,13 +159,13 @@ async def async_get_user(self, user_id: Optional[str] = None, name: Optional[str
159159

160160
## 修改用户签名
161161
```Python
162-
def modify_info(self, target: str) -> dict
162+
def modify_information(self, target: str) -> dict
163163
```
164164
@param target: 新签名
165165

166166
对应的协程风格的api:
167167
```Python
168-
async def async_modify_info(self, target: str)
168+
async def async_modify_information(self, target: str)
169169
```
170170

171171
## 发表评论
@@ -184,19 +184,20 @@ async def async_post_comment(self, target_id: str, target_type: str, content: st
184184

185185
## 查询实验
186186
```Python
187-
def query_experiments(self, category: physicsLab.enums.Category, tags: Optional[List[physicsLab.enums.Tag]] = None, exclude_tags: Optional[List[physicsLab.enums.Tag]] = None, languages: Optional[List[str]] = None, user_id: Optional[str] = None, take: int = 18, skip: int = 0) -> dict
187+
def query_experiments(self, category: physicsLab.enums.Category, tags: Optional[List[physicsLab.enums.Tag]] = None, exclude_tags: Optional[List[physicsLab.enums.Tag]] = None, languages: Optional[List[str]] = None, exclude_languages: Optional[List[str]] = None, user_id: Optional[str] = None, take: int = 18, skip: int = 0) -> dict
188188
```
189189
@param category: 实验区还是黑洞区
190190
@param tags: 根据列表内的物实实验的标签进行对应的搜索
191191
@param exclude_tags: 除了列表内的标签的实验都会被搜索到
192192
@param languages: 根据列表内的语言进行对应的搜索
193+
@param exclude_languages: 除了列表内的语言的实验都会被搜索到
193194
@param user_id: 指定搜索的作品的发布者
194195
@param take: 搜索数量
195196
@param skip: 跳过搜索数量
196197

197198
对应的协程风格的api:
198199
```Python
199-
async def async_query_experiments(self, category: physicsLab.enums.Category, tags: Optional[List[physicsLab.enums.Tag]] = None, exclude_tags: Optional[List[physicsLab.enums.Tag]] = None, languages: Optional[List[str]] = None, user_id: Optional[str] = None, take: int = 18, skip: int = 0)
200+
async def async_query_experiments(self, category: physicsLab.enums.Category, tags: Optional[List[physicsLab.enums.Tag]] = None, exclude_tags: Optional[List[physicsLab.enums.Tag]] = None, languages: Optional[List[str]] = None, exclude_languages: Optional[List[str]] = None, user_id: Optional[str] = None, take: int = 18, skip: int = 0)
200201
```
201202

202203
## 领取每日签到奖励
@@ -208,19 +209,19 @@ def receive_bonus(self, activity_id: str, index: int) -> dict
208209

209210
对应的协程风格的api:
210211
```Python
211-
async def async_receive_bonus(self)
212+
async def async_receive_bonus(self, activity_id: str, index: int)
212213
```
213214

214215
## 删除评论
215216
```Python
216-
def remove_comment(self, CommentID: str, target_type: str) -> dict
217+
def remove_comment(self, comment_id: str, target_type: str) -> dict
217218
```
218-
@param CommentID: 评论ID, 可以通过`get_comments`获取
219+
@param comment_id: 评论ID, 可以通过`get_comments`获取
219220
@param target_type: User, Discussion, Experiment
220221

221222
对应的协程风格的api:
222223
```Python
223-
async def async_remove_comment(self, CommentID: str, target_type: str)
224+
async def async_remove_comment(self, comment_id: str, target_type: str)
224225
```
225226

226227
## 修改用户昵称
@@ -234,31 +235,18 @@ def rename(self, nickname: str) -> dict
234235
async def async_rename(self, nickname: str)
235236
```
236237

237-
## 收藏某个实验
238+
## 收藏/支持 某个实验
238239
```Python
239-
def star(self, content_id: str, category: physicsLab.enums.Category, status: bool = True) -> dict
240+
def star_content(self, content_id: str, category: physicsLab.enums.Category, star_type: int, status: bool = True) -> dict
240241
```
241242
@param content_id: 实验ID
242243
@param category: 实验区, 黑洞区
243-
@param status: True: 收藏, False: 取消收藏
244+
@param star_type: 0: 收藏, 1: 使用金币支持实验
245+
@param status: True: 收藏, False: 取消收藏 (对支持无作用)
244246

245247
对应的协程风格的api:
246248
```Python
247-
async def async_star(self, content_id: str, category: physicsLab.enums.Category, status: bool = True)
248-
```
249-
250-
## 使用金币支持某实验
251-
```Python
252-
def star_content(self, content_id: str, category: physicsLab.enums.Category, status: bool = True) -> dict
253-
```
254-
@content_id: 实验id
255-
@category: 实验区还是黑洞区
256-
@status: 是否支持
257-
@return: 返回的json数据
258-
259-
对应的协程风格的api:
260-
```Python
261-
async def async_star_content(self, content_id: str, category: physicsLab.enums.Category, status: bool = True)
249+
async def async_star_content(self, content_id: str, category: physicsLab.enums.Category, star_type: int, status: bool = True)
262250
```
263251

264252
## 上传实验图片

docsgen/webapi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,19 @@ def main():
2727
and not obj.__name__.startswith('_') \
2828
and not obj.__name__.startswith("async")
2929
):
30+
method_signature: inspect.Signature = inspect.signature(fn_obj)
31+
async_method_signature: inspect.Signature = inspect.signature(async_methods['async_' + fn_name])
32+
assert method_signature.parameters == async_method_signature.parameters, fn_name
33+
3034
doc: str = fn_obj.__doc__
3135
context += f"\n## {doc.splitlines()[0]}\n"
32-
context += f"```Python\ndef {fn_name}{inspect.signature(fn_obj)}\n```\n"
36+
context += f"```Python\ndef {fn_name}{method_signature}\n```\n"
3337
for line in doc.split('\n')[1:]:
3438
context += f"{(line + ' ').lstrip()}\n"
3539

3640
context += f"对应的协程风格的api:\n" \
3741
f"```Python\n" \
38-
f"async def {'async_' + fn_name}{inspect.signature(async_methods['async_' + fn_name])}\n" \
42+
f"async def {'async_' + fn_name}{async_method_signature}\n" \
3943
f"```\n"
4044

4145
with open(OUTPUT_FILE, 'w', encoding='utf-8') as f:

physicsLab/web/_api.py

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
from physicsLab.enums import Tag, Category
1313
from physicsLab._typing import Optional, List, TypedDict, Callable
1414

15-
def _take_warning() -> None:
16-
errors.warning("take is less than 1")
17-
1815
def _check_response(response: requests.Response, err_callback: Optional[Callable] = None) -> dict:
1916
''' 检查返回的response
2017
@callback: 自定义物实返回的status对应的报错信息,
@@ -211,6 +208,7 @@ def query_experiments(
211208
@param tags: 根据列表内的物实实验的标签进行对应的搜索
212209
@param exclude_tags: 除了列表内的标签的实验都会被搜索到
213210
@param languages: 根据列表内的语言进行对应的搜索
211+
@param exclude_languages: 除了列表内的语言的实验都会被搜索到
214212
@param user_id: 指定搜索的作品的发布者
215213
@param take: 搜索数量
216214
@param skip: 跳过搜索数量
@@ -292,8 +290,7 @@ def get_experiment(
292290

293291
if category is not None:
294292
# 如果传入的是实验ID, 先获取summary来得到ContentID
295-
summary = self.get_summary(content_id, category)
296-
content_id = summary["Data"]["ContentID"]
293+
content_id = self.get_summary(content_id, category)["Data"]["ContentID"]
297294

298295
response = requests.post(
299296
"https://physics-api-cn.turtlesim.com:443/Contents/GetExperiment",
@@ -405,12 +402,12 @@ def post_comment(
405402

406403
return _check_response(response)
407404

408-
def remove_comment(self, CommentID: str, target_type: str) -> dict:
405+
def remove_comment(self, comment_id: str, target_type: str) -> dict:
409406
''' 删除评论
410-
@param CommentID: 评论ID, 可以通过`get_comments`获取
407+
@param comment_id: 评论ID, 可以通过`get_comments`获取
411408
@param target_type: User, Discussion, Experiment
412409
'''
413-
if not isinstance(CommentID, str) \
410+
if not isinstance(comment_id, str) \
414411
or not isinstance(target_type, str):
415412
raise TypeError
416413
if target_type not in ("User", "Discussion", "Experiment"):
@@ -420,7 +417,7 @@ def remove_comment(self, CommentID: str, target_type: str) -> dict:
420417
"https://physics-api-cn.turtlesim.com:443/Messages/RemoveComment",
421418
json={
422419
"TargetType": target_type,
423-
"CommentID": CommentID,
420+
"CommentID": comment_id,
424421
},
425422
headers={
426423
"Content-Type": "application/json",
@@ -573,53 +570,28 @@ def get_profile(self) -> dict:
573570

574571
return _check_response(response)
575572

576-
def star(self, content_id: str, category: Category, status: bool = True) -> dict:
577-
''' 收藏某个实验
573+
def star_content(self, content_id: str, category: Category, star_type: int, status: bool = True) -> dict:
574+
''' 收藏/支持 某个实验
578575
@param content_id: 实验ID
579576
@param category: 实验区, 黑洞区
580-
@param status: True: 收藏, False: 取消收藏
577+
@param star_type: 0: 收藏, 1: 使用金币支持实验
578+
@param status: True: 收藏, False: 取消收藏 (对支持无作用)
581579
'''
582580
if not isinstance(content_id, str) \
583581
or not isinstance(category, Category) \
584-
or not isinstance(status, bool):
582+
or not isinstance(status, bool) \
583+
or not isinstance(star_type, int):
585584
raise TypeError
585+
if star_type not in (0, 1):
586+
raise ValueError
586587

587588
response = requests.post(
588589
"https://physics-api-cn.turtlesim.com/Contents/StarContent",
589590
json={
590591
"ContentID": content_id,
591592
"Status": status,
592593
"Category": category.value,
593-
},
594-
headers={
595-
"Content-Type": "application/json",
596-
"x-API-Token": self.token,
597-
"x-API-AuthCode": self.auth_code,
598-
}
599-
)
600-
601-
return _check_response(response)
602-
603-
def star_content(self, content_id: str, category: Category, status: bool = True) -> dict:
604-
''' 使用金币支持某实验
605-
@content_id: 实验id
606-
@category: 实验区还是黑洞区
607-
@status: 是否支持
608-
@return: 返回的json数据
609-
'''
610-
if not isinstance(content_id, str) \
611-
or not isinstance(category, Category) \
612-
or not isinstance(status, bool):
613-
raise TypeError
614-
615-
response = requests.post(
616-
"https://physics-api-cn.turtlesim.com:443/Contents/StarContent",
617-
json={
618-
"ContentID": content_id,
619-
"Status": status,
620-
"Category": category.value,
621-
"Status": status,
622-
"Type": 1,
594+
"Type": star_type,
623595
},
624596
headers={
625597
"Content-Type": "application/json",
@@ -846,7 +818,7 @@ def rename(self, nickname: str) -> dict:
846818

847819
return _check_response(response)
848820

849-
def modify_info(self, target: str) -> dict:
821+
def modify_information(self, target: str) -> dict:
850822
''' 修改用户签名
851823
@param target: 新签名
852824
'''

physicsLab/web/api.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,18 @@ async def async_post_comment(
9191
) -> dict:
9292
return await _async_wrapper(self.post_comment, target_id, target_type, content, reply_id)
9393

94-
async def async_remove_comment(self, CommentID: str, target_type:str):
95-
return await _async_wrapper(self.remove_comment, CommentID, target_type)
94+
async def async_remove_comment(self, comment_id: str, target_type:str):
95+
return await _async_wrapper(self.remove_comment, comment_id, target_type)
9696

9797
async def async_get_comments(
9898
self,
9999
target_id: str,
100100
target_type: str,
101101
take: int = 16,
102102
skip: int = 0,
103+
comment_id: Optional[str] = None,
103104
):
104-
return await _async_wrapper(self.get_comments, target_id, target_type, take, skip)
105+
return await _async_wrapper(self.get_comments, target_id, target_type, take, skip, comment_id)
105106

106107
async def async_get_summary(self, content_id: str, category: Category):
107108
return await _async_wrapper(self.get_summary, content_id, category)
@@ -119,11 +120,8 @@ async def async_get_user(
119120
async def async_get_profile(self):
120121
return await _async_wrapper(self.get_profile)
121122

122-
async def async_star(self, content_id: str, category: Category, status: bool = True):
123-
return await _async_wrapper(self.star, content_id, category, status)
124-
125-
async def async_star_content(self, content_id: str, category: Category, status: bool = True):
126-
return await _async_wrapper(self.star_content, content_id, category, status)
123+
async def async_star_content(self, content_id: str, category: Category, star_type: int, status: bool = True):
124+
return await _async_wrapper(self.star_content, content_id, category, star_type, status)
127125

128126
async def async_upload_image(self, policy: str, authorization: str, image_path: str):
129127
return await _async_wrapper(self.upload_image, policy, authorization, image_path)
@@ -133,7 +131,7 @@ async def async_get_message(self, message_id: str):
133131

134132
async def async_get_messages(
135133
self,
136-
category_id: int = 0,
134+
category_id: int,
137135
skip: int = 0,
138136
take: int = 16,
139137
no_templates: bool = True,
@@ -165,8 +163,8 @@ async def async_follow(self, target_id: str, action: bool = True):
165163
async def async_rename(self, nickname: str):
166164
return await _async_wrapper(self.rename, nickname)
167165

168-
async def async_modify_info(self, target: str):
169-
return await _async_wrapper(self.modify_info, target)
166+
async def async_modify_information(self, target: str):
167+
return await _async_wrapper(self.modify_information, target)
170168

171-
async def async_receive_bonus(self):
172-
return await _async_wrapper(self.receive_bonus)
169+
async def async_receive_bonus(self, activity_id: str, index: int):
170+
return await _async_wrapper(self.receive_bonus, activity_id, index)

0 commit comments

Comments
 (0)