<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from flask import request

from flask.ext import restful


class Resource(restful.Resource):

    def __init__(self, api, *args, **kwargs):
        self.api = api
        if self.validate_payload not in self.method_decorators:
            self.method_decorators.insert(0, self.validate_payload)

    def validate_payload(self, func):
        '''Perform a payload validation on expected model'''
        def wrapper(*args, **kwargs):
            # TODO: proper content negociation
            data = request.get_json()
            if hasattr(func, '__apidoc__'):
                model = func.__apidoc__.get('body')
                validate = func.__apidoc__.get('validate', False)
                if model and validate and hasattr(model, 'validate'):
                    model.validate(data, self.api.refresolver)

            return func(*args, **kwargs)
        return wrapper<commit_msg>Only parse json if validation is required<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from flask import request

from flask.ext import restful


class Resource(restful.Resource):

    def __init__(self, api, *args, **kwargs):
        self.api = api
        if self.validate_payload not in self.method_decorators:
            self.method_decorators.insert(0, self.validate_payload)

    def validate_payload(self, func):
        '''Perform a payload validation on expected model'''
        def wrapper(*args, **kwargs):
            if hasattr(func, '__apidoc__'):
                model = func.__apidoc__.get('body')
                validate = func.__apidoc__.get('validate', False)
                if model and validate and hasattr(model, 'validate'):
                    # TODO: proper content negociation
                    data = request.get_json()
                    model.validate(data, self.api.refresolver)
            return func(*args, **kwargs)
        return wrapper
{'fraction_non_alphanumeric': 0.09994952044422009, 'fraction_numerical': 0.0020191822311963654, 'mean_word_length': 2.3822525597269624, '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': '6508701', 'n_tokens_mistral': 536, 'n_tokens_neox': 504, 'n_words': 149}