<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import os
import json

from settings import API_GW_BASE_URL


def response_text_from_request(base_url, resource):
    text = ''

    try:
        response = requests.get(base_url+resource)
    except requests.ConnectionError:
        text = '{"status": 500, "exception": "ConnectionError"}'
    else:
        text = response.text

    if text == '{}\n':
        text = '{"status": 404, "exception": "No resource"}'

    return text


def objects_from_request(base_url, resource):
    text = response_text_from_request(base_url, resource)
    try:
        objects = json.loads(text)
    except ValueError:
        objects = {"status": 500, "exception": "ValueError"}

    if isinstance(objects, dict):
        server_error_msg = "Internal Server Error"
        if objects.get("message") == server_error_msg:
            objects.update({"status": 500})

    return objects


def is_error_in_objects(objects):
    result = False
    if isinstance(objects, dict):
        is_status = objects.get('status')
        is_exception = objects.get('exception')
        if is_status and is_exception:
            result = True

    return result


def get_status_code_if_error_in_objects(objects):
    if is_error_in_objects(objects):
        status = objects.get("status")
        if not status:
            objects['status'] = 500
        return objects['status']

    return 200


def get_template_name_from_status_code(status_code, default):<commit_msg>empty data handling & message key handling<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import os
import json

from settings import API_GW_BASE_URL


def response_text_from_request(base_url, resource):
    text = ''

    try:
        response = requests.get(base_url+resource)
    except requests.ConnectionError:
        text = '{"status": 500, "exception": "ConnectionError"}'
    else:
        text = response.text

    if text.startswith('{}'):
        text = '{"status": 404, "exception": "No resource"}'

    return text


def objects_from_request(base_url, resource):
    text = response_text_from_request(base_url, resource)
    try:
        objects = json.loads(text)
    except ValueError:
        objects = {"status": 500, "exception": "ValueError"}

    if isinstance(objects, dict):
        server_error_msg = "Internal Server Error"
        if objects.get("message") == server_error_msg:
            objects.update({"status": 500})

    return objects


def is_error_in_objects(objects):
    result = False
    if isinstance(objects, dict):
        is_status = objects.get('status')
        is_exception = objects.get('exception')
        is_message = objects.get('message')
        if is_status and (is_exception or is_message):
            result = True

    return result


def get_status_code_if_error_in_objects(objects):
    if is_error_in_objects(objects):
        return objects['status']

    return 200


def get_template_name_from_status_code(status_code, default):
{'fraction_non_alphanumeric': 0.10939553219448095, 'fraction_numerical': 0.011498028909329829, 'mean_word_length': 3.1316146540027137, 'pattern_counts': {'":': 14, '<': 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': '4588716', 'n_tokens_mistral': 933, 'n_tokens_neox': 875, 'n_words': 218}