<commit_before>"""
common.py

Shared functions for meerkat_frontend.
"""
from datetime import datetime, timedelta
from dateutil.parser import parse
from flask import abort, request
from meerkat_frontend import app, auth
import requests
import logging
import json
import os


def api(url, api_key=False, params=None):
    """
    Returns JSON data from API request.
    Args:
        url (str): The Meerkat API url from which data is requested
        api_key (optional bool): Whethe or not we should include the api
        key. Defaults to False.
    Returns:
        dict: A python dictionary formed from the API reponse json string.
    """
    if(app.config['TESTING']):
        path = os.path.dirname(os.path.realpath(__file__))+"/apiData"+url
        with open(path+'.json') as data_file:
            return json.load(data_file)
    else:
        api_uri = add_domain(''.join([app.config['INTERNAL_API_ROOT'], url]))
        try:
            if api_key:
                r = requests.get(
                    api_uri,
                    headers={'authorization': 'Bearer ' + auth.get_token()},
                    params=params
                )
            else:
                r = requests.get(
                    api_uri,
                    params=params
                )
            if r.status_code == 502:
                abort(500, "Can not access the api at " + api_uri)
        except requests.exceptions.RequestException as e:
            logging.error("Failed to access the API.")
            logging.error(e)
            abort(500, "Failed to access the API.")
        try:
            output = r.json()
        except Exception as e:
            logging.error("Failed to convert API response to JSON.")
            logging.error(e)
            abort(500, "Failed to convert API response to JSON.")
        return output


def authenticate(username=app.config['SERVER_AUTH_USERNAME'],
                 password=app.config['SERVER_AUTH_PASSWORD']):
    """
    Makes an authentication request to meerkat_auth using the specified
    username and password, or the server username and password by default by
    default.

    Returns:
        str The JWT token.
    """
    # Assemble auth request params
    url = app.config['AUTH_ROOT'] + '/api/login'
    data = {'username': username, 'password': password}
    headers = {'content-type': 'application/json'}

    # Make the auth request and log the result
    r = requests.request('POST', url, json=data, headers=headers)
    logging.warning("Received authentication response: " + str(r))

    # Log an error if authentication fails, and return an empty token
    if r.status_code != 200:
        logging.error('Authentication as {} failed'.format(username))
        return ''

    # Return the token
    return r.cookies.get('meerkat_jwt', '')


def hermes(url, method, data={}):
    """
    Makes a Hermes API request.
    Args:
       url (str): The Meerkat Hermes url for the desired function.
       method (str):  The desired HTML function: GET, POST or PUT.
       data (optional dict): The data to be sent to the url. Defaults
       to ```{}```.
    Returns:
       dict: a dictionary formed from the json data in the response.
    """
    # Assemble the request params.
    url = app.config['HERMES_ROOT'] + url
    headers = {'content-type': 'application/json',
               'authorization': 'Bearer {}'.format(authenticate())}

    # Log the request
    logging.debug("Sending json: {}\nTo url: {}\nWith headers: {}".format(
                  json.dumps(data), url, headers))

    # Make the request and handle the response.
    try:
        r = requests.request(method, url, json=data, headers=headers)
    except requests.exceptions.RequestException as e:
        logging.error("Failed to access Hermes.")
        logging.error(e)
        abort(500, "Problem accessing the Hermes api.")
<commit_msg>Improve logging.<commit_after>"""
common.py

Shared functions for meerkat_frontend.
"""
from datetime import datetime, timedelta
from dateutil.parser import parse
from flask import abort, request
from meerkat_frontend import app, auth
import requests
import logging
import json
import os


def api(url, api_key=False, params=None):
    """
    Returns JSON data from API request.
    Args:
        url (str): The Meerkat API url from which data is requested
        api_key (optional bool): Whethe or not we should include the api
        key. Defaults to False.
    Returns:
        dict: A python dictionary formed from the API reponse json string.
    """
    if(app.config['TESTING']):
        path = os.path.dirname(os.path.realpath(__file__))+"/apiData"+url
        with open(path+'.json') as data_file:
            return json.load(data_file)
    else:
        api_uri = add_domain(''.join([app.config['INTERNAL_API_ROOT'], url]))
        try:
            if api_key:
                r = requests.get(
                    api_uri,
                    headers={'authorization': 'Bearer ' + auth.get_token()},
                    params=params
                )
            else:
                r = requests.get(
                    api_uri,
                    params=params
                )
            if r.status_code == 502:
                abort(500, "Can not access the api at " + api_uri)
        except requests.exceptions.RequestException as e:
            logging.error("Failed to access the API.")
            logging.error(e)
            abort(500, "Failed to access the API.")
        try:
            output = r.json()
        except Exception as e:
            logging.error("Failed to convert API response to JSON.")
            logging.error(e)
            abort(500, "Failed to convert API response to JSON.")
        return output


def authenticate(username=app.config['SERVER_AUTH_USERNAME'],
                 password=app.config['SERVER_AUTH_PASSWORD']):
    """
    Makes an authentication request to meerkat_auth using the specified
    username and password, or the server username and password by default by
    default.

    Returns:
        str The JWT token.
    """
    # Assemble auth request params
    url = app.config['AUTH_ROOT'] + '/api/login'
    data = {'username': username, 'password': password}
    headers = {'content-type': 'application/json'}

    # Make the auth request and log the result
    r = requests.request('POST', url, json=data, headers=headers)
    logging.warning("Received authentication response: " + str(r))

    # Log an error if authentication fails, and return an empty token
    if r.status_code != 200:
        logging.error('Authentication as {} failed'.format(username))
        return ''

    # Return the token
    return r.cookies.get('meerkat_jwt', '')


def hermes(url, method, data={}):
    """
    Makes a Hermes API request.
    Args:
       url (str): The Meerkat Hermes url for the desired function.
       method (str):  The desired HTML function: GET, POST or PUT.
       data (optional dict): The data to be sent to the url. Defaults
       to ```{}```.
    Returns:
       dict: a dictionary formed from the json data in the response.
    """
    # Assemble the request params.
    url = app.config['HERMES_ROOT'] + url
    headers = {'content-type': 'application/json',
               'authorization': 'Bearer {}'.format(authenticate())}

    # Log the request
    logging.info("Sending json: {}\nTo url: {}\nWith headers: {}".format(
                  json.dumps(data), url, headers))

    # Make the request and handle the response.
    try:
        r = requests.request(method, url, json=data, headers=headers)
    except requests.exceptions.RequestException as e:
        logging.error("Failed to access Hermes.")
        logging.error(e)
        abort(500, "Problem accessing the Hermes api.")

{'fraction_non_alphanumeric': 0.10378086419753087, 'fraction_numerical': 0.004629629629629629, 'mean_word_length': 2.2883720930232556, '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': '3485375', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2088, 'n_words': 713}