Skip to main content

Tornado REST API

Project description

This simple module creates CRUD endpoints for SQLAlchemy table. Its purpose to allow quickly create RESTful APIs, for example for admin panel. List endpoints support pagination and filtering. Was written for Angular’s ng-admin, so this module supports ng-admin querying out of the box

Installation

pip install tornado-alchemy-rest

Usage example

import tornado.web
from tornado.web import URLSpec
from .models import ItemTable
from tornado_alchemy_rest import SingleRESTAPIHandler, ListRESTAPIHandler

class SingleItemHandler(SingleRESTAPIHandler):
    table = ItemTable

    def delete(self, *args, **kwargs):
        raise HTTPError(405)


class ItemHandler(ListRESTAPIHandler):
    table = ItemTable

app = tornado.web.Application([
    URLSpec(prefix(r'items'), ItemHandler, dict(psql=psql_pool), 'items'),
    URLSpec(prefix(r'items/(\d+)'), SingleItemHandler, dict(psql=psql_pool), 'single_item'),
])

More complex example, where you can override object creation methods:

class SingleItemHandler(SingleRESTAPIHandler):
    table = TableItem

    @gen.coroutine
    def get_object_dict(self, *args):
        obj = yield super().get_object_dict(*args)

        cursor = yield self._execute_query(User.select().where(User.c.id == obj['user_id']))
        obj['user'] = cursor.fetchone()
        return obj

    @gen.coroutine
    def put_object_dict(self, id, params):
        assert params['value'] > 5
        yield super().put_object_dict(id, params)

class ItemHandler(ListRESTAPIHandler):
    table = TableItem

    @gen.coroutine
    def get_object_list(self, query):
        objects = yield super().get_object_list(query)
        for obj in objects:
            cursor = yield self._execute_query(User.select().where(User.c.id == obj['user_id']))
            obj['user'] = cursor.fetchone()
        raise gen.Return(objects)

    @gen.coroutine
    def post_object_dict(self, params):
        assert params['value'] > 5
        yield super().post_object_dict(params)

Querying

To get second page with ordering by id DESC you need to do that query:

GET /item?_page=2&_perPage=30&_sortField=id&_sortDir=DESC

To get all items, where name contains “test” and type is 5 and value is 7 or 6, you will need that query:

GET /item?_filters={"name__contains":"test", "type":5, "value__any":[7,6]}

Query params

  • _page – page name

  • _perPage – rows per page

  • _sortField – field to order by

  • _sortDir – direction to sort by

  • _filters – filter items with given params. Currently supported filters are: “” (equality), startswith, contains, icontains, any, ne

Join support

That will return data from both tables at some time:

class SingleItemHandler(SingleRESTAPIHandler):
    table = TableItem

    def get_from(self):
        return self.table.join(TableUser, isouter=True)

Requirements

tornado, sqlalchemy

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

tornado-alchemy-rest-1.0.18.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

tornado_alchemy_rest-1.0.18-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file tornado-alchemy-rest-1.0.18.tar.gz.

File metadata

File hashes

Hashes for tornado-alchemy-rest-1.0.18.tar.gz
Algorithm Hash digest
SHA256 1efee47d79d4a78a155aff8058dfad8b92fdd7652524f442bcddbbf99814258c
MD5 4287b4b24ec06dd771532c45309a2fda
BLAKE2b-256 c69ad60a31a0c86d3990c11e263dead8a9b0a864a12c96dc934b34452b25820a

See more details on using hashes here.

File details

Details for the file tornado_alchemy_rest-1.0.18-py3-none-any.whl.

File metadata

File hashes

Hashes for tornado_alchemy_rest-1.0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 29cd11525a710b4517f8c4d383e4c073236f5d6d4f2212f0b72e75a3edf5e5be
MD5 89f270b579e9836a8a9afaeb2e40f7bd
BLAKE2b-256 17f96057fc1dfc17b0e9a62f30971200c84276bd440e1eaf325ed35dfbbb857b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page