Introduction¶
rest_toolkit is a Python package which provides a very convenient way to build REST servers. It is build on top of Pyramid, but you do not need to know much about Pyramid to use rest_toolkit.
Quick example¶
This is a minimal example which defines a Root resource with a GET view, and starts a simple HTTP server. If you run this example you can request http://localhost:8080/ and you will see a JSON response with a status message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from rest_toolkit import quick_serve
from rest_toolkit import resource
@resource('/')
class Root(object):
def __init__(self, request):
pass
@Root.GET()
def show_root(root, request):
return {'status': 'OK'}
if __name__ == '__main__':
quick_serve()
|
The previous example is simple, but real REST services are likely to be much more complex, for example because they need to request data from a SQL server. The next example shows how you can use SQL data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | from rest_toolkit import quick_serve
from rest_toolkit import resource
from rest_toolkit.ext.sql import SQLResource
from sqlalchemy import Column, Integer, String, bindparam
from sqlalchemy.orm import Query
from pyramid_sqlalchemy import BaseObject
from pyramid_sqlalchemy import DBSession
class User(BaseObject):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
fullname = Column(String)
@resource('/users')
class UserCollection(object):
def __init__(self, request):
pass
@UserCollection.GET()
def list_users(collection, request):
return {'users': [{'id': user.id,
'fullname': user.fullname}
for user in DBSession.query(User)]}
@resource('/users/{id}')
class UserResource(SQLResource):
context_query = Query(User) .filter(User.id == bindparam('id'))
@UserResource.GET()
def show_user(user, request):
return {'id': user.id, 'fullname': user.fullname}
if __name__ == '__main__':
quick_serve(DBSession)
|
This example creates two resources: a /users collection which will return a list of all users for a GET-request, and a /users/<id> resource which will return information for an individual user on a GET-request.
Contents¶
- Basic usage
- SQL support
- Security
- Philosophy
- API documentation
- Comparison with other frameworks
- Changelog
- 0.14 - May 25, 2017
- 0.13 - August 18, 2016
- 0.12 - June 1, 2016
- 0.11 - May 6, 2016
- 0.10 - May 4, 2016
- 0.9 - September 20, 2015
- 0.8 - September 5, 2015
- 0.7 - March 12, 2015
- 0.6 - November 4, 2014
- 0.5 - October 24, 2014
- 0.4.1 - July 18, 2014
- 0.4 - July 18, 2014
- 0.3 - July 11, 2014
- 0.2.2 - July 11, 2014
- 0.2.1 - July 10, 2014
- 0.2 - July 9, 2014
- 0.1 - Released 24 June, 2014