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

from flask import Flask
from flask import request
from flask import make_response


class FlaskExt(Flask):
    """
    Flask server customization
    """

    def make_default_options_response(self):
        # sup = super(Flask, self)
        ret = Flask.make_default_options_response(self)
        ret.headers['Access-Control-Allow-Origin'] = '*'
        ret.headers['Access-Control-Allow-Headers'] = "Origin, Content-Type, Accept, Authorization"
        return ret

webapp = FlaskExt(__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)

def __common_resp_handle(data=None, error=None):
    """
    provide common response handling
    """
    ret_data = __response_wrap(data, error)
    resp = jsonify(ret_data)

    # more response processing

    return resp

@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<commit_msg>load_node_by_id_attr -> load_node_single_by_id_attr (unimplemented currently), load_node_set_by_id_attr<commit_after>"""
Rhizi web API
"""
import os
import db_controller as dbc
import json
from flask import jsonify

from flask import Flask
from flask import request
from flask import make_response


class FlaskExt(Flask):
    """
    Flask server customization
    """

    def make_default_options_response(self):
        # sup = super(Flask, self)
        ret = Flask.make_default_options_response(self)
        ret.headers['Access-Control-Allow-Origin'] = '*'
        ret.headers['Access-Control-Allow-Headers'] = "Origin, Content-Type, Accept, Authorization"
        return ret

webapp = FlaskExt(__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)

def __common_resp_handle(data=None, error=None):
    """
    provide common response handling
    """
    ret_data = __response_wrap(data, error)
    resp = jsonify(ret_data)

    # more response processing

    return resp

@webapp.route("/load/node-single", methods=['POST'])
def load_node_single_by_id_attr():
    assert False

@webapp.route("/load/node-set", methods=['POST'])
def load_node_set_by_id_attr():
    """
    @param id_set: list of node ids to match id attribute against
    @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
    """
    id_set = request.get_json()['id_set']
    __sanitize_input(id_set)

    return __load_node_set_by_id_attr_common(id_set)

def __load_node_set_by_id_attr_common(id_set):
    op = dbc.DBO_load_node_set_by_id_attribute(id_set)
    try:
        n_set = db_ctl.exec_op(op)
        return __common_resp_handle(data=n_set)
    except Exception as e:
        log.exception(e)
        return __common_resp_handle(error='unable to load node with ids: {0}'.format(id_set))

@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
{'fraction_non_alphanumeric': 0.10488408732838173, 'fraction_numerical': 0.00045014629754670267, 'mean_word_length': 3.658280922431866, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2182394', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1415, 'n_words': 375}