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

# Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

import requests

from django.http import HttpResponse


class HttpOK(HttpResponse):
    status_code = 200


class Requester():
    """
    wrapper class for requests
    """
    def __init__(
        self, method='get', url_template=None, url_args=None,
        headers=None, data=None, accept_codes=[]
    ):
        self.method = method
        self.url = url_template.format(**url_args)
        self.headers = headers
        self.data = data
        self.error = None

        kwargs = {}
        if self.headers:
            kwargs.update(headers=self.headers)
        if self.data:
            kwargs.update(data=self.data)
        try:
            self.response = getattr(requests, self.method)(self.url, **kwargs)
        except Exception, e:
            raise Exception("Error in request. Error msg:\n {message}".format(message=e.message))
        if not (self.response.status_code == HttpOK.status_code or
                self.response.status_code in accept_codes):
            error_msg = "Non-OK response. Status: {status}\nMethod: {method}\nURL:{url}{message}".format(
                status=self.response.status_code,
                method=self.method,
                url=self.url,
                message="\nResponse text: {text}".format(text=self.response.text) if self.response.text else ""
            )
            raise Exception(error_msg)<commit_msg>[#1450 #1551] Markup cleanup Cordaid requester.py<commit_after># -*- coding: utf-8 -*-
"""Akvo RSR is covered by the GNU Affero General Public License.

See more details in the license.txt file located at the root folder of the Akvo RSR module.
For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
"""

import requests
from django.http import HttpResponse


class HttpOK(HttpResponse):

    """."""

    status_code = 200


class Requester():

    """Wrapper class for requests."""

    def __init__(
        self, method='get', url_template=None, url_args=None,
        headers=None, data=None, accept_codes=None
    ):
        """."""
        if accept_codes is None:
            accept_codes = []

        self.method = method
        self.url = url_template.format(**url_args)
        self.headers = headers
        self.data = data
        self.error = None

        kwargs = {}
        if self.headers:
            kwargs.update(headers=self.headers)
        if self.data:
            kwargs.update(data=self.data)
        try:
            self.response = getattr(requests, self.method)(self.url, **kwargs)
        except Exception, e:
            raise Exception("Error in request. Error msg:\n {message}".format(message=e.message))
        if not (self.response.status_code == HttpOK.status_code or
                self.response.status_code in accept_codes):
            text = "\nResponse text: {}".format(self.response.text) if self.response.text else ""
            error_msg = "Non-OK response. Status: {}\nMethod: {}\nURL:{}{}".format(
                self.response.status_code,
                self.method,
                self.url,
                text
            )
            raise Exception(error_msg)
{'fraction_non_alphanumeric': 0.11269656377402446, 'fraction_numerical': 0.004659289458357601, 'mean_word_length': 2.240566037735849, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2342513', 'n_tokens_mistral': 979, 'n_tokens_neox': 943, 'n_words': 257}