<commit_before># -*- coding: utf-8 -*-
from flask import g, jsonify

import kuas_api.kuas.cache as cache
import kuas_api.modules.error as error
import kuas_api.modules.const as const
from kuas_api.modules.stateless_auth import auth
from .doc import auto

# Nestable blueprints problem
# not sure isn't this a best practice now.
# https://github.com/mitsuhiko/flask/issues/593
#from kuas_api.views.v2 import api_v2
routes = []


def route(rule, **options):
    def decorator(f):
        url_rule = {
            "rule": rule,
            "view_func": f,
            "options": options if options else {}
        }

        routes.append(url_rule)
        return f

    return decorator


@route("/token")
@auto.doc(groups=["public"])
@auth.login_required
def get_auth_token():
    """Login to KUAS, and return token for KUAS API.

    **Example request**:

    .. sourcecode:: http

        GET /latest/token HTTP/1.1
        Host: kuas.grd.idv.tw:14769
        Authorization: Basic xxxxxxxxxxxxx=
        Accept: */*

    **Example response**:

    .. sourcecode:: http

        HTTP/1.1 200 OK
        Content-Type: application/json

        {
          "duration": 600,
          "auth_token": "<PASSWORD>"
        }


    :statuscode 200: success login
    :statuscode 401: login fail or auth_token expired
    """

    token = g.token
    return jsonify({
        'auth_token': token.decode('ascii'),
        'duration': const.token_duration
    })


@route('/versions/<string:device_type>')
@auto.doc(groups=["public"])
def device_version(device_type):
    """Get latest version for app on (`device_type`) in webstore.

    **Example request**

    .. sourcecode:: http

            GET /latest/versions/android HTTP/1.1
            Host: kuas.grd.idv.tw:14769



    **Response**

    +----------------+---------------+-------------------------------------------+
    |                |               |                                           |
    | Parameter Name | Type          | Description                               |
    |                |               |                                           |
    +================+===============+===========================================+
    |                |               |                                           |
    | version        | Object        |                                           |
    |                |               |                                           |
    +----------------+---------------+-------------------------------------------+

    **Version object**

    +----------------+---------------+-------------------------------------------+<commit_msg>Add /v2/token response<commit_after># -*- coding: utf-8 -*-
from flask import g, jsonify

import kuas_api.kuas.cache as cache
import kuas_api.modules.error as error
import kuas_api.modules.const as const
from kuas_api.modules.stateless_auth import auth
from .doc import auto

# Nestable blueprints problem
# not sure isn't this a best practice now.
# https://github.com/mitsuhiko/flask/issues/593
#from kuas_api.views.v2 import api_v2
routes = []


def route(rule, **options):
    def decorator(f):
        url_rule = {
            "rule": rule,
            "view_func": f,
            "options": options if options else {}
        }

        routes.append(url_rule)
        return f

    return decorator


@route("/token")
@auto.doc(groups=["public"])
@auth.login_required
def get_auth_token():
    """Login to KUAS, and return token for KUAS API.

    **Example request**:

    .. sourcecode:: http

        GET /latest/token HTTP/1.1
        Host: kuas.grd.idv.tw:14769
        Authorization: Basic xxxxxxxxxxxxx=
        Accept: */*

    **Example response**:

    .. sourcecode:: http

        HTTP/1.1 200 OK
        Content-Type: application/json

        {
          "duration": 600,
          "token_type": "Basic",
          "auth_token": "<PASSWORD>"
        }


    :statuscode 200: success login
    :statuscode 401: login fail or auth_token expired
    """

    token = g.token
    return jsonify(
        auth_token=token.decode('ascii'),
        token_type="Basic",
        duration=const.token_duration)


@route('/versions/<string:device_type>')
@auto.doc(groups=["public"])
def device_version(device_type):
    """Get latest version for app on (`device_type`) in webstore.

    **Example request**

    .. sourcecode:: http

            GET /latest/versions/android HTTP/1.1
            Host: kuas.grd.idv.tw:14769



    **Response**

    +----------------+---------------+-------------------------------------------+
    |                |               |                                           |
    | Parameter Name | Type          | Description                               |
    |                |               |                                           |
    +================+===============+===========================================+
    |                |               |                                           |
    | version        | Object        |                                           |
    |                |               |                                           |
    +----------------+---------------+-------------------------------------------+

    **Version object**

    +----------------+---------------+-------------------------------------------+
{'fraction_non_alphanumeric': 0.2110758903598732, 'fraction_numerical': 0.01286593324631736, 'mean_word_length': 1.2211180124223602, 'pattern_counts': {'":': 11, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6863395', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1290, 'n_words': 313}