Code Monkey home page Code Monkey logo

tornado-rest's Introduction

Tornado REST

tornado_rest is a framework built for tornado.

Installation :

You can install tornado_rest with pip :

pip install tornado_rest

Quick start

from tornado.web import Application
from tornado_rest import Api, Namespace, Resource
from marshmallow import Schema, fields, INCLUDE, ValidationError

app = Application()

api = Api()


class TodoSchema(Schema):
    id = fields.Int(dump_only=True)
    task = fields.Str()  


class TodoService:
    def __init__(self):
        self.counter = 0
        self.todos = []

    def get(self, id):
        for todo in self.todos:
            if todo['id'] == id:
                return todo

    def create(self, data):
        todo = data
        todo['id'] = self.counter = self.counter + 1
        self.todos.append(todo)
        return todo

    def update(self, id, data):
        todo = self.get(id)
        todo.update(data)
        return todo

    def delete(self, id):
        todo = self.get(id)
        self.todos.remove(todo)

    def get_all(self):
        return self.todos


todo_service = TodoService()
todo_service.create({'task': 'Build an API'})
todo_servicce.create({'task': 'Build a REST FRAMEWORK'})

ns = Namespace("todo")

@ns.route('/')
class TodoList(Resource):
    '''Shows a list of all todos, and lets you POST to add new tasks'''
    @ns.doc('list_todos')
    @ns.marshal_list_with(todo)
    def get(self):
        '''List all tasks'''
        return todo_service.get_all()

    @ns.marshal_list_with(todo)
    def post(self):
        '''Create a new task'''
        return todo_service.create(api.payload), 201


@ns.route('/<int:id>')
class Todo(Resource):
    '''Show a single todo item and lets you delete them'''
    
    @ns.marshal_with(todo)
    def get(self, id):
        '''Fetch a given resource'''
        return todo_service.get(id)


    def delete(self, id):
        '''Delete a task given its identifier'''
        todo_service.delete(id)
        return '', 204

    @ns.marshal_with(todo)
    def put(self, id):
        '''Update a task given its identifier'''
        return todo_service.update(id, api.payload)

TODO :

  • payload validation : add a resource decorator to validate the request payload against a schema
@ns.route('/<int:id>')
class Todo(Resource):
    '''Show a single todo item and lets you delete them'''

    @ns.expect(todo)
    def put(self, id):
        '''Update a task given its identifier'''
        return todo_service.update(id, api.payload)


- error handling : define error handlers at Api level 

tornado-rest's People

Contributors

augustinbarbe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.