Boilerplate-free way for creating libs for REST APIs
Project description
REST API lib creator
REST API lib creator is a boilerplate-free way for creating libs for RESTful APIs (specially the ones created using Django REST framework - but certainly adaptable for other frameworks).
NOTES:
- This is a port for a personal project I made for myself. It may or may not solve your needs (it solves mine).
- This is still alpha. I opened this on GH just so I can see if this is something I should improve (or not).
Installation:
pip install rest-api-lib-creator
Examples:
- The bare minimum for creating your own lib:
from rest_api_lib_creator.core import ViewsetRestApiLib
class User(ViewsetRestApiLib):
base_api_url = 'http://super.cool/api/users'
- With this you can play around with your 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>
- If your resource return other nested resources you can parse them as well:
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)
You can see all possible customizations here (someday I'll improve this doc).
Special thanks:
- Django REST framework - you're the best, seriously.
- Stripe-python lib - which I used as an inspiration to create this project.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
rest-api-lib-creator-0.1.1.tar.gz
(18.1 kB
view hashes)
Built Distribution
Close
Hashes for rest-api-lib-creator-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14d815962d6a68523d0beeb2524292ca7c9415965d7c78acb33a423a688e8aff |
|
MD5 | 4a5df85f4be98e5c6647feba6233f6c6 |
|
BLAKE2b-256 | c66ece9f2e59e6eddbf11669c1135cf941c0ef0afc5b85fa5c1697ba6618dce8 |
Close
Hashes for rest_api_lib_creator-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bf49b00902e6d75d84b1c4401cba89a49c8d18e55ed902e4956dd514c3f5a3f |
|
MD5 | 3fd87f0df50099f0976656080e9e7c2f |
|
BLAKE2b-256 | e1d58a3b03ffbf8530a543ca0928724cc6837f10b93661a3d63173e2f9d92d71 |