"""
Rhizi web API
"""
import os
import db_controller as dbc
import json
from flask import jsonify

from flask import Flask
from flask import request

webapp = Flask(__name__)
webapp.debug = True

# injected: DB controller
db_ctl = None

def __sanitize_input():
    pass

def __response_wrap(data=None, error=None):
    """
    wrap response data/errors as dict - this should always be used when returning
    data to allow easy return of list objects, assist in error case distinction, etc. 
    """
    return dict(data=data, error=error)

@webapp.route("/load/node-single", methods=['POST'])
def load_node_by_id_attr():
    """
    @return: a list containing a single node whose id attribute matches 'id' or
            an empty list if the requested node is not found
    @raise exception: on error
    """
    node_id = request.form['id']
    
    __sanitize_input(node_id)
    
    op = dbc.DBO_load_node_set_by_id_attribute([node_id])
    try:
        n = db_ctl.exec_op(op)
        ret = __response_wrap(data=n)
        return jsonify(ret)
    except Exception as e:
        return jsonify(__response_wrap(error='unable to load node with id: {0}'.format(node_id)))

@webapp.route("/add/node-single", methods=['POST'])
def add_node(n):
    pass

@webapp.route("/add/node-set", methods=['POST'])
def add_node_set(n_set):
    op = dbc.DBO_add_node_set()
    pass

@@ -141,16 +141,433 @@
  request
+%0Afrom flask import make_response%0A%0A%0Aclass FlaskExt(Flask):%0A    %22%22%22%0A    Flask server customization%0A    %22%22%22%0A%0A    def make_default_options_response(self):%0A        # sup = super(Flask, self)%0A        ret = Flask.make_default_options_response(self)%0A        ret.headers%5B'Access-Control-Allow-Origin'%5D = '*'%0A        ret.headers%5B'Access-Control-Allow-Headers'%5D = %22Origin, Content-Type, Accept, Authorization%22%0A        return ret
 %0A%0Awebapp
@@ -574,16 +574,19 @@
  = Flask
+Ext
 (__name_

extend Flask class to allow customization of HTTP OPTIONS method response