tornado-instant-webapi

The library for automatically generating web API from Python object based on Tornado.


Keywords
api-server, tornado
License
MIT
Install
pip install tornado-instant-webapi==1.0.0

Documentation

Tornado Instant WebAPI MIT License

This is a library for automatically generating Tornado web API server from Python object with Type Hints.

Installation

pip install git+https://github.com/Hiroshiba/tornado_instant_webapi

Usage

You can make web API from Python object: Classes, Modules, Dicts, Objects.

This is an example of starting API server from a class.

import tornado.ioloop
from tornado_instant_webapi import make_application

class Calculator(object):
    @staticmethod
    def double(number: float) -> float:
        return 2 * number

if __name__ == '__main__':
    app = make_application(Calculator)
    app.listen(8000)
    tornado.ioloop.IOLoop.current().start()

Then, you can call API:

$ curl http://localhost:8000/double?number=100
200.0

Original type parameter

You can use an original type easily, by adding the encoder or decoder.

This is the example for using NumPy.

import tornado.ioloop
from tornado_instant_webapi import common_converter, make_application

import json
import numpy

class NumpyCalculator(object):
    @staticmethod
    def sum(array: numpy.ndarray) -> float:
        return array.sum()

def nparray_decoder(s: str):
    return numpy.asarray(json.loads(s))

if __name__ == '__main__':
    common_converter.register_new_type(
        key='nparray',
        t=numpy.ndarray,
        decoder=nparray_decoder,
    )

    app = make_application(NumpyCalculator)
    app.listen(8000)
    tornado.ioloop.IOLoop.current().start()

Then, you can call API:

$ curl --globoff http://localhost:8000/sum?array=[1,2,3]
6

Nested object

This library can make web API from nested Python object.

if __name__ == '__main__':
    nested = {
        'Calculator': Calculator,
        'NumpyCalculator': NumpyCalculator,
    }
    app = make_application(nested, string_case='snake')
    app.listen(8000)
    tornado.ioloop.IOLoop.current().start()

Then, you can call API:

$ curl http://localhost:8000/calculator/double?number=100
200.0

$ curl --globoff http://localhost:8000/numpy_calculator/sum?array=[1,2,3]
6

Sample Code

Image Converter is the sample web API server for the image convert.

License

MIT License, see LICENSE.