<commit_before>from debuild import app
from chatham.builders import Builder

import json
from flask import request

API_BASE = '/api'
DEBUG = True


# Core verbs:
#
#  - token
#  - ping
#  - result


def serialize(obj, allok):
    obj['status'] = 'ok' if allok else 'nokay'

    if DEBUG:
        return json.dumps(obj, sort_keys=True, indent=4)
    return json.dumps(obj)


def api_abort(code, text):
    return serialize({
        'code': code,
        'text': text
    }, False)


def api_validate(keys):
    req = request.values
    for key in ['node', 'signature']:
        if key not in req:
            return False

    for key in keys:
        if key not in req:
            return False

    node = req['node']
    builder = Builder(node)
    return builder.validate_request(req['signature'])


@app.route("%s/ping" % (API_BASE), methods=['GET', 'POST'])
def ping():
    req = request.values
    if not api_validate(req):
        return api_abort('bad-sig', 'bad signature')
    builder = Builder(req['node'])
    builder.ping()
    return serialize({
        'ping': 'pung'
    }, True)


@app.route('%s/token' % (API_BASE), methods=['GET', 'POST'])
def token():
    """ Unauth'd ping """
    req = request.values
    if 'node' not in req:
        return api_abort('nsp-node', 'No such param: node')

    node = req['node']
    bob = Builder(node)
    key = bob.new_token()

    return serialize({
        "token": key
    }, True)<commit_msg>building out some api<commit_after>from debuild import app
from monomoy.core import db
from monomoy.utils import JSONEncoder
from debuild.utils import db_find
from chatham.builders import Builder

from flask import request
import json

API_BASE = '/api'
DEBUG = True


# Core verbs:
#
#  - token
#  - ping
#  - result


def serialize(obj, allok):
    obj['status'] = 'ok' if allok else 'nokay'

    if DEBUG:
        return json.dumps(obj, sort_keys=True, indent=4, cls=JSONEncoder)
    return json.dumps(obj, cls=JSONEncoder)


def api_abort(code, text):
    return serialize({
        'code': code,
        'text': text
    }, False)


def api_validate(keys):
    req = request.values
    for key in ['node', 'signature']:
        if key not in req:
            return False

    for key in keys:
        if key not in req:
            return False

    node = req['node']
    builder = Builder(node)
    return builder.validate_request(req['signature'])


@app.route('%s/token' % (API_BASE), methods=['GET', 'POST'])
def token():
    """ Unauth'd ping """
    req = request.values
    if 'node' not in req:
        return api_abort('nsp-node', 'No such param: node')

    node = req['node']
    bob = Builder(node)
    key = bob.new_token()

    return serialize({
        "token": key
    }, True)


@app.route("%s/ping" % (API_BASE), methods=['GET', 'POST'])
def ping():
    req = request.values
    if not api_validate([]):
        return api_abort('bad-sig', 'bad signature')
    builder = Builder(req['node'])
    builder.ping()
    return serialize({
        'ping': 'pung'
    }, True)


@app.route("%s/result" % (API_BASE), methods=['GET', 'POST'])
def result():
    req = request.values
    if not api_validate(['data']):
        return api_abort('bad-sig', 'bad signature')  # factor this out
    builder = Builder(req['node'])
    builder.ping()
    data = json.loads(req['data'])
    job = data['job']
    jobj = db_find('jobs', job)
    if jobj['builder'] is None:
        return api_abort('bad-builder', 'bad builder node')

    builder = Builder(jobj['builder'])
    if jobj['builder'] != builder._obj['_id']:  # XXX: Fixme
        return api_abort('bad-builder', 'foo bad builder node')

    check_id = db.checks.insert(data)

    return serialize({
        'check': check_id
    }, True)
{'fraction_non_alphanumeric': 0.14676947620313746, 'fraction_numerical': 0.0005317734645041213, 'mean_word_length': 2.784708249496982, 'pattern_counts': {'":': 2, '<': 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': '3177008', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1217, 'n_words': 349}