<commit_before>from flask import Blueprint, request, g, Response
from apicore.scandirs import basedir, root, versions, blueprints
import simplejson as json

api = Blueprint('api', __name__)
from v1.auth import views, errors
# from v1.auth.models import User


def before_api():
    userAgent = request.user_agent
    g.client = {'browser': userAgent.browser,
                'platform': userAgent.platform,
                'version': userAgent.version,
                'language': userAgent.language}


api.before_request(before_api)


@api.route('/')
def index():
    if g.client['browser'] is None or g.client['platform'] is None:
        msg = {'message': "hello from API {} -- in CLI Mode".format(versions[0]), 'status': 200}
        r = Response(json.dumps(msg))
        r.headers['Content-type'] = 'application/json; charset=utf-8'
        return r, 200

    string = "<h1>hello from API v1 | {} | {} | {} | {}</h1>"
    return string.format(g.client['browser'],
                         g.client['platform'],
                         g.client['version'],
                         g.client['language'])<commit_msg>just some thoughts on v1/__init__.py<commit_after>from flask import Blueprint, request, g, Response
from apicore.scandirs import basedir, root, versions, blueprints
import simplejson as json

api = Blueprint('api', __name__)
from v1.auth import views, errors
# from v1.auth.models import User

"""
Here is how to implement a before_request on a Blueprint.
NOTE: this API must check if it's a browser-based navigation (or not)
on any single request (or maybe once?).
"""


def before_api():
    """
    JUST create a global in Flask App dict containing client's browsing
    interface.
    """
    userAgent = request.user_agent
    g.client = {'browser': userAgent.browser,
                'platform': userAgent.platform,
                'version': userAgent.version,
                'language': userAgent.language}


api.before_request(before_api)


@api.route('/')
def index():
    """
    if no browser and no platform: it's a CLI request.
    """
    if g.client['browser'] is None or g.client['platform'] is None:
        string = "hello from API {} -- in CLI Mode"
        msg = {'message': string.format(versions[0]), 'status': 200}
        r = Response(json.dumps(msg))
        r.headers['Content-type'] = 'application/json; charset=utf-8'
        return r, 200

    """
    ELSE: it's obviously on a web browser
    """
    string = "<h1>hello from API v1 | {} | {} | {} | {}</h1>"
    return string.format(g.client['browser'],
                         g.client['platform'],
                         g.client['version'],
                         g.client['language'])
{'fraction_non_alphanumeric': 0.13501483679525222, 'fraction_numerical': 0.010014836795252226, 'mean_word_length': 2.576923076923077, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7170545', 'n_tokens_mistral': 805, 'n_tokens_neox': 759, 'n_words': 231}