<commit_before># -*- coding: utf-8 -*-
from webservices.models import BaseConsumer
import requests


class SyncConsumer(BaseConsumer):
    def __init__(self, base_url, public_key, private_key):
        super(SyncConsumer, self).__init__(base_url, public_key, private_key)
        self.session = requests.session()
        
    def send_request(self, url, data, headers): # pragma: no cover
        response = self.session.post(url, data=data, headers=headers)
        self.raise_for_status(response.status_code, response.content)
        return response.content


class DjangoTestingConsumer(SyncConsumer):
    def __init__(self, test_client, base_url, public_key, private_key):
        self.test_client = test_client
        super(DjangoTestingConsumer, self).__init__(base_url, public_key, private_key)
    
    def build_url(self, path):
        return path
    
    def send_request(self, url, data, headers):
        headers = {'HTTP_%s' % header.upper().replace('-', '_'): value for header, value in headers.items()}
        response = self.test_client.post(url, data=data, content_type='application/json', **headers)
        self.raise_for_status(response.status_code, response.content)
        return response.content
    

class FlaskTestingConsumer(DjangoTestingConsumer):
    def send_request(self, url, data, headers):
        response = self.test_client.post(url, data=data, headers=headers)
        self.raise_for_status(response.status_code, response.data)
        return response.data
    

def provider_for_django(provider):
    from django.http import HttpResponse
    from django.views.decorators.csrf import csrf_exempt
    def provider_view(request):
        def get_header(key, default):
            django_key = 'HTTP_%s' % key.upper().replace('-', '_')
            return request.META.get(django_key, default)
        method = request.method
        signed_data = request.raw_post_data
        status_code, data = provider.get_response(method, signed_data, get_header)
        return HttpResponse(data, status=status_code)
    return csrf_exempt(provider_view)

def provider_for_flask(app, url, provider):
    from flask import request
    def provider_view():
        def get_header(key, default):
            return request.headers.get(key, default)
        method = request.method
        signed_data = request.data
        status_code, data = provider.get_response(method, signed_data, get_header)
        return data, status_code
    return app.route(url, methods=['POST'])(provider_view)
<commit_msg>added python 2.6 compatibility<commit_after># -*- coding: utf-8 -*-
from webservices.models import BaseConsumer
import requests


class SyncConsumer(BaseConsumer):
    def __init__(self, base_url, public_key, private_key):
        super(SyncConsumer, self).__init__(base_url, public_key, private_key)
        self.session = requests.session()
        
    def send_request(self, url, data, headers): # pragma: no cover
        response = self.session.post(url, data=data, headers=headers)
        self.raise_for_status(response.status_code, response.content)
        return response.content


class DjangoTestingConsumer(SyncConsumer):
    def __init__(self, test_client, base_url, public_key, private_key):
        self.test_client = test_client
        super(DjangoTestingConsumer, self).__init__(base_url, public_key, private_key)
    
    def build_url(self, path):
        return path
    
    def send_request(self, url, data, headers):
        headers = dict([('HTTP_%s' % header.upper().replace('-', '_'), value) for header, value in headers.items()])
        response = self.test_client.post(url, data=data, content_type='application/json', **headers)
        self.raise_for_status(response.status_code, response.content)
        return response.content
    

class FlaskTestingConsumer(DjangoTestingConsumer):
    def send_request(self, url, data, headers):
        response = self.test_client.post(url, data=data, headers=headers)
        self.raise_for_status(response.status_code, response.data)
        return response.data
    

def provider_for_django(provider):
    from django.http import HttpResponse
    from django.views.decorators.csrf import csrf_exempt
    def provider_view(request):
        def get_header(key, default):
            django_key = 'HTTP_%s' % key.upper().replace('-', '_')
            return request.META.get(django_key, default)
        method = request.method
        signed_data = request.raw_post_data
        status_code, data = provider.get_response(method, signed_data, get_header)
        return HttpResponse(data, status=status_code)
    return csrf_exempt(provider_view)

def provider_for_flask(app, url, provider):
    from flask import request
    def provider_view():
        def get_header(key, default):
            return request.headers.get(key, default)
        method = request.method
        signed_data = request.data
        status_code, data = provider.get_response(method, signed_data, get_header)
        return data, status_code
    return app.route(url, methods=['POST'])(provider_view)

{'fraction_non_alphanumeric': 0.10049164208456243, 'fraction_numerical': 0.0007866273352999017, 'mean_word_length': 3.545129579982127, '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': '7207021', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1456, 'n_words': 325}