reliure.web

helpers to build HTTP/Json Api from reliure engines

reliure.web.app_routes(app)

list of route of an app

class reliure.web.EngineView(engine, name=None)

Bases: object

View over an Engine or a Block

>>> engine = Engine("count_a", "count_b")
>>>
>>> engine.count_a.setup(in_name='in')
>>> engine.count_a.set(lambda chaine: chaine.count("a"))
>>>
>>> engine.count_b.setup(in_name='in')
>>> engine.count_b.set(lambda chaine: chaine.count("b"))
>>> 
>>> 
>>> # we can build a view on this engine
>>> egn_view = EngineView(engine, name="count")
>>> egn_view.add_output("count_a")  # note that by default block outputs are named by block's name
>>> egn_view.add_output("count_b")
>>> # one can specify a short url for this engine
>>> egn_view.play_route("/q/<in>")
>>>
>>> # this view can be added to a reliure API
>>> api = ReliureAPI("api")
>>> api.register_view(egn_view)
__init__(engine, name=None)
add_input(in_name, type_or_parse=None)

Declare a possible input

add_output(out_name, type_or_serialize=None, **kwargs)

Declare an output

options()

Engine options discover HTTP entry point

parse_request()

Parse request for play()

play()

Main http entry point: run the engine

play_route(*routes)

Define routes for GET play.

This use Flask route syntax, see: http://flask.pocoo.org/docs/0.10/api/#url-route-registrations

run(inputs_data, options)

Run the engine/block according to some inputs data and options

It is called from play()

Parameters:
  • inputs_data – dict of input data
  • options – engine/block configuration dict
set_input_type(type_or_parse)

Set an unique input type.

If you use this then you have only one input for the play.

set_outputs(*outputs)

Set the outputs of the view

short_play(**kwargs)

Main http entry point: run the engine

class reliure.web.ComponentView(component)

Bases: reliure.web.EngineView

View over a simple component (Composable or simple function)

__init__(component)
add_input(in_name, type_or_parse=None)
add_output(out_name, type_or_serialize=None, **kwargs)
class reliure.web.ReliureAPI(name='api', url_prefix=None, expose_route=True, **kwargs)

Bases: flask.blueprints.Blueprint

Standart Flask json API view over a Reliure Engine.

This is a Flask Blueprint (see http://flask.pocoo.org/docs/blueprints/)

Here is a simple usage exemple:

>>> from reliure.engine import Engine
>>> engine = Engine("process")
>>> engine.process.setup(in_name="in", out_name="out")
>>> # setup the block's component
>>> engine.process.set(lambda x: x**2)
>>> 
>>> # configure a view for the engine
>>> egn_view = EngineView(engine)
>>> # configure view input/output
>>> from reliure.types import Numeric
>>> egn_view.set_input_type(Numeric())
>>> egn_view.add_output("out")
>>> 
>>> ## create the API blueprint
>>> api = ReliureAPI()
>>> api.register_view(egn_view, url_prefix="egn")
>>>
>>> # here you get your blueprint
>>> # you can register it to your app with
>>> app.register_blueprint(api, url_prefix="/api")    

Then you will have two routes:

  • [GET] /api/: returns a json that desctibe your api routes
  • [GET] /api/egn: returns a json that desctibe your engine
  • [POST] /api/egn: run the engine itself

To use the “POST” entry point you can do :

>>> request = {
...     "in": 5,       # this is the name of my input
...     "options": {}   # this this the api/engine configuration
... }
>>> res = requests.get(
...     SERVER_URL+"/api/egn",
...     data=json.dumps(request),
...     headers={"content-type": "application/json"}
... )                                                       
>>> data = res.json()                                       
{
    meta: {...}
    results: {
        "out": 25
    }
}
__init__(name='api', url_prefix=None, expose_route=True, **kwargs)

Build the Blueprint view over a Engine.

Parameters:name – the name of this api (used as url prefix by default)
Expose_route:wether / returns all api routes default True
register(app, options, first_registration=False)
register_view(view, url_prefix=None)

Associate a EngineView to this api

class reliure.web.RemoteApi(url, **kwargs)

Bases: flask.blueprints.Blueprint

Proxy to a remote ReliureJsonAPI

__init__(url, **kwargs)

Function doc :param url: engine api url

add_url_rule(path, endpoint, *args, **kwargs)
forward(**kwargs)

remote http call to api endpoint accept ONLY GET and POST

register(app, options, first_registration=False)