蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

RESTful API实践:验证、权限、文档与三组练习:独立实例

PYTHONPython3.12、PyJWT2.15.1、SQLite;Node原生fetch及Edge浏览器观察单独保存practices.py
practices.py15 行
from client import API
import sys,json
api=API(sys.argv[1]);category=api.create('categories',{'name':'本站技术实践'});book=api.create('books',{'title':'蓝色天空网学习示范','author':'Sky Team','isbn':'9780000000002','price':'19.90','categoryId':category['id'],'stock':5,'publishedDate':'2026-11-03','description':'仅作教学结构,ISBN不宣称出版登记'})
r=api.call('GET','/api/categories/'+str(category['id'])+'/books');api.expect(r,200);assert any(x['id']==book['id'] for x in r['body']['data']);api.expect(api.call('DELETE','/api/categories/'+str(category['id'])),409)
bad=api.call('POST','/api/books',{'title':'x','author':'x','isbn':'123','price':19.9,'categoryId':category['id']});api.expect(bad,422)
tasks=[api.create('tasks',{'title':'批量学习'+str(i),'priority':'high','dueDate':'2026-12-01'}) for i in range(2)];r=api.call('PATCH','/api/tasks/batch',{'ids':[x['id'] for x in tasks],'status':'completed'});api.expect(r,200);assert r['body']['data']['updated']==2
stats=api.call('GET','/api/tasks/stats');assert stats['body']['data']['completed']>=2
import uuid,os
new_email=uuid.uuid4().hex+'@example.invalid';guest=API(sys.argv[1],login=False);registered=guest.call('POST','/api/auth/register',{'name':'新学习会员','email':new_email,'password':os.environ['MBSKY_DEMO_PASSWORD']});api.expect(registered,201);new_member=API(sys.argv[1],email=new_email);profile=new_member.call('GET','/api/users/profile');api.expect(profile,200);assert profile['body']['data']['role']=='member'
post=api.create('posts',{'title':'学习记录','content':'理解原理再验证'});member=API(sys.argv[1],email='learner@example.invalid');comment=member.create('comments',{'content':'完成一次真实练习'},'/api/posts/'+str(post['id'])+'/comments')
a=member.call('POST','/api/posts/'+str(post['id'])+'/like');b=member.call('POST','/api/posts/'+str(post['id'])+'/like');api.expect(a,201);api.expect(b,200);assert a['body']['data']['id']==b['body']['data']['id'];api.expect(member.call('DELETE','/api/posts/'+str(post['id'])),403)
f=member.call('POST','/api/users/'+str(api.user['id'])+'/follow');api.expect(f,201);notifications=api.call('GET','/api/notifications');assert len(notifications['body']['data'])>=3
api.expect(member.call('POST','/api/posts',{'title':'x','content':'测试禁用词'}),422)
print(json.dumps({'book_fields_verified':True,'batch_atomic':True,'stats':stats['body']['data'],'like_statuses':[201,200],'ownership_rejection':403,'local_notifications':len(notifications['body']['data']),'external_messages_sent':False},ensure_ascii=False))
本例需在 Python3.12、PyJWT2.15.1、SQLite;Node原生fetch及Edge浏览器观察单独保存 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Python3.12、PyJWT2.15.1、SQLite;Node原生fetch及Edge浏览器观察单独保存

入口文件:projects/rest-lab/practices.py

预期结果与验证条件观察提示
  • 本章明确的输入及错误分支断言对应真实服务状态
  • 没有修改现有网站数据,也未发送真实邮件或通知

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:RESTful API

课程关联工程包(登记关联10章) · 含当前章节或实例 · mbskycom202611a547.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习