<commit_before>"""
Rhizi REST web API:
   - make use of rz_kernel for core logic execution
   - make use of rz_api_common for common API logic

"""
from flask import Flask
from flask import escape
from flask import jsonify
from flask import make_response
from flask import redirect
from flask import render_template
from flask import request
from flask import send_from_directory
from flask import session
from flask import url_for
import flask
import logging
import traceback

from model.graph import Attr_Diff
from model.graph import Topo_Diff
from rz_api_common import sanitize_input__attr_diff
from rz_api_common import sanitize_input__topo_diff
from rz_api_common import validate_obj__attr_diff


log = logging.getLogger('rhizi')

db_ctl = None  # injected: DB controller

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
    
    @data must be json serializable
    @error will be serialized with str()
    """
    error_str = str(error)  # convert any Exception objects to serializable form
    ret_data = __response_wrap(data, error_str)
    resp = jsonify(ret_data)  # this will create a Flask Response object<commit_msg>server/rhizi_api: on 'None' error push empty string, not a six letter string<commit_after>"""
Rhizi REST web API:
   - make use of rz_kernel for core logic execution
   - make use of rz_api_common for common API logic

"""
from flask import Flask
from flask import escape
from flask import jsonify
from flask import make_response
from flask import redirect
from flask import render_template
from flask import request
from flask import send_from_directory
from flask import session
from flask import url_for
import flask
import logging
import traceback

from model.graph import Attr_Diff
from model.graph import Topo_Diff
from rz_api_common import sanitize_input__attr_diff
from rz_api_common import sanitize_input__topo_diff
from rz_api_common import validate_obj__attr_diff


log = logging.getLogger('rhizi')

db_ctl = None  # injected: DB controller

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
    
    @data must be json serializable
    @error will be serialized with str()
    """
    if error is None:
        error_str = ""
    else:
        error_str = str(error)  # convert any Exception objects to serializable form
    ret_data = __response_wrap(data, error_str)
    resp = jsonify(ret_data)  # this will create a Flask Response object
{'fraction_non_alphanumeric': 0.05179282868525897, 'fraction_numerical': 0.0, 'mean_word_length': 4.332743362831859, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6928081', 'n_tokens_mistral': 867, 'n_tokens_neox': 834, 'n_words': 346}