<commit_before># -*- coding: utf-8 -*-

import json
from functools import wraps
from time import time

import falcon

from ..di import DiContainer


class ContextMiddleware:
    def process_resource(self, req, *_):
        req.context["flow_id"] = DiContainer.flow_id()


def http_api_logger(func):
    """
    :type func: types.FunctionType
    :rtype: types.FunctionType
    """
    @wraps(func)
    def wrapper(object, req, resp, *args, **kwargs):
        """
        :type object: object
        :type req: falcon.request.Request
        :type resp: falcon.request.Response
        """
        start = time()

        logger = DiContainer.logger(flow_id=req.context["flow_id"])
        logger.info("Received API request %s.", func.__name__, extra={
            "request_method": req.method,
            "request_url": req.url,
            "request_user_agent": req.user_agent,
            "request_referer": req.referer,
            "request_func": func.__name__,
        })

        try:
            func(object, req, resp, *args, logger=logger, **kwargs)
            logger.info("Completed API request %s.", func.__name__, extra={
                "request_method": req.method,
                "request_url": req.url,
                "request_func": func.__name__,
                "request_user_agent": req.user_agent,
                "request_referer": req.referer,
                "duration_in_secs": time() - start,
            })
        except falcon.HTTPInvalidParam:
            logger.warning("Wrong parameter was sent in API request %s.", func.__name__, extra={
                "request_method": req.method,
                "request_url": req.url,
                "request_func": func.__name__,
                "request_user_agent": req.user_agent,
                "request_referer": req.referer,
                "duration_in_secs": time() - start,
            })
            raise

        except Exception:
            logger.exception("Exception raised on API request %s.", func.__name__, extra={
                "request_method": req.method,
                "request_url": req.url,
                "request_func": func.__name__,
                "request_user_agent": req.user_agent,
                "request_referer": req.referer,
                "duration_in_secs": time() - start,
            })
<commit_msg>Add one blank line in try-except, #27570<commit_after># -*- coding: utf-8 -*-

import json
from functools import wraps
from time import time

import falcon

from ..di import DiContainer


class ContextMiddleware:
    def process_resource(self, req, *_):
        req.context["flow_id"] = DiContainer.flow_id()


def http_api_logger(func):
    """
    :type func: types.FunctionType
    :rtype: types.FunctionType
    """
    @wraps(func)
    def wrapper(object, req, resp, *args, **kwargs):
        """
        :type object: object
        :type req: falcon.request.Request
        :type resp: falcon.request.Response
        """
        start = time()

        logger = DiContainer.logger(flow_id=req.context["flow_id"])
        logger.info("Received API request %s.", func.__name__, extra={
            "request_method": req.method,
            "request_url": req.url,
            "request_user_agent": req.user_agent,
            "request_referer": req.referer,
            "request_func": func.__name__,
        })

        try:
            func(object, req, resp, *args, logger=logger, **kwargs)
            logger.info("Completed API request %s.", func.__name__, extra={
                "request_method": req.method,
                "request_url": req.url,
                "request_func": func.__name__,
                "request_user_agent": req.user_agent,
                "request_referer": req.referer,
                "duration_in_secs": time() - start,
            })

        except falcon.HTTPInvalidParam:
            logger.warning("Wrong parameter was sent in API request %s.", func.__name__, extra={
                "request_method": req.method,
                "request_url": req.url,
                "request_func": func.__name__,
                "request_user_agent": req.user_agent,
                "request_referer": req.referer,
                "duration_in_secs": time() - start,
            })
            raise

        except Exception:
            logger.exception("Exception raised on API request %s.", func.__name__, extra={
                "request_method": req.method,
                "request_url": req.url,
                "request_func": func.__name__,
                "request_user_agent": req.user_agent,
                "request_referer": req.referer,
                "duration_in_secs": time() - start,
            })

{'fraction_non_alphanumeric': 0.11806148590947908, 'fraction_numerical': 0.0014944491887275832, 'mean_word_length': 1.9117464263517714, 'pattern_counts': {'":': 46, '<': 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': '2626733', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1293, 'n_words': 294}