创建REST API库的无样板方法
项目描述
REST API库创建器
REST API库创建器是一个用于创建RESTful API库的无样板方法(特别是使用Django REST framework创建的,但也适用于其他框架)。
注意:
- 这是我为自己创建的一个个人项目的移植。它可能或可能无法解决您的需求(但它解决了我的需求)。
- 这仍然是alpha版本。我在GitHub上打开它只是为了看看这是否是我应该改进的地方(或者不是)。
示例
- 创建您自己的库的最低要求
from rest_api_lib_creator.core import ViewsetRestApiLib
class User(ViewsetRestApiLib):
base_api_url = 'http://super.cool/api/users'
- 有了这个,您可以玩转您的API
users = User.list() # Triggers a requests.get with url=http://super.cool/api/users
isinstance(users[0], User)
user = User.create(first_name='Filipe', last_name='Waitman', email='filwaitman@gmail.com', photo=open('image.png', 'rb')) # Triggers a requests.post with url=http://super.cool/api/users and data={'first_name': 'Filipe', 'last_name': 'Waitman', 'email': 'filwaitman@gmail.com'} and files={'photo': <file binary content>}
# Similarly to the call above you could create an empty object and save it:
user = User()
user.first_name = 'Filipe'
user.last_name = 'Waitman'
user.email = 'filwaitman@gmail.com'
user.photo = open('image.png', 'rb')
user.save() # Triggers a requests.post with url=http://super.cool/api/users and data={'first_name': 'Filipe', 'last_name': 'Waitman', 'email': 'filwaitman@gmail.com'} and files={'photo': <file binary content>}
isinstance(user, User)
print(user.id) # Prints the user id (assuming the API returned this field)
print(user.first_name) # )rints the user first name (assuming the API returned this field)
user.first_name = 'New name'
user.save() # Triggers a requests.patch with url=http://super.cool/api/users/<user-id> and data={'first_name': 'New name'}
user.delete() # Triggers a requests.delete with url=http://super.cool/api/users/<user-id>
- 如果您的资源返回其他嵌套资源,您也可以解析它们
class Pet(ViewsetRestApiLib):
base_api_url = 'http://super.cool/api/pets'
nested_objects = {
'owner': User,
}
pet = Pet.retrieve('pet-id')
isinstance(pet, Pet)
isinstance(pet.owner, User)
特别感谢
- Django REST framework - 你是最好的,真的。
- Stripe-python库 - 我以它为灵感创建了此项目。
开发
运行检查器
pip install -r requirements_dev.txt
isort -rc .
tox -e lint
通过 tox
运行测试
pip install -r requirements_dev.txt
tox
发布新的大/小/补丁版本
pip install -r requirements_dev.txt
bump2version <PART> # <PART> can be either 'patch' or 'minor' or 'major'
上传到PyPI
pip install -r requirements_dev.txt
python setup.py sdist bdist_wheel
python -m twine upload dist/*
贡献
如果您发现问题,请 提交问题,或在可能的情况下 创建拉取请求。如果是拉取请求,请考虑以下事项
- 尊重行长度(132个字符)
- 编写自动化测试
- 在本地运行
tox
以查看是否一切正常(包括代码检查器和其他Python版本)
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源代码发行版
rest-api-lib-creator-0.4.0.tar.gz (7.1 kB 查看哈希值)