<commit_before>try:
    from urllib.parse import urljoin
except ImportError:
    from urlparse import urljoin

import requests

from django.conf import settings


class RequestFailure(requests.exceptions.RequestException):
    """The request failed."""


class TunicClient(object):

    def get(self, path):
        headers = {
            'Authorization': 'Token {}'.format(settings.TUNIC_REQUEST_TOKEN),
        }
        if settings.TUNIC_STAFF_BACKEND_ROOT in path:
            url = path
        else:
            url = self.handle_protocol_relative_paths(path)
        resp = requests.get(url, headers=headers)
        if not resp.ok:
            raise RequestFailure(response=resp)
        return resp

    def handle_protocol_relative_paths(self, path):
        url_base = settings.TUNIC_STAFF_BACKEND_ROOT
        if url_base.startswith('//'):
            url_base = 'http:' + url_base
        return urljoin(
            url_base, settings.TUNIC_API_PATH.rstrip('/') + '/' + path.lstrip('/')
        )

    def get_campaigns(self, filter_active=False):
        campaigns = {}
        path = "/campaign/"
        if filter_active:
            path += "?active=True"
        while True:
            resp = self.get(path).json()
            campaigns.update({
                c["id"]: c for c in resp.get("results", {})
            })
            path = resp.get("next")
            if not path:
                break
        return campaigns

    def get_active_campaigns(self):
        return self.get_campaigns(filter_active=True)<commit_msg>while path:<commit_after>try:
    from urllib.parse import urljoin
except ImportError:
    from urlparse import urljoin

import requests

from django.conf import settings


class RequestFailure(requests.exceptions.RequestException):
    """The request failed."""


class TunicClient(object):

    def get(self, path):
        headers = {
            'Authorization': 'Token {}'.format(settings.TUNIC_REQUEST_TOKEN),
        }
        if settings.TUNIC_STAFF_BACKEND_ROOT in path:
            url = path
        else:
            url = self.handle_protocol_relative_paths(path)
        resp = requests.get(url, headers=headers)
        if not resp.ok:
            raise RequestFailure(response=resp)
        return resp

    def handle_protocol_relative_paths(self, path):
        url_base = settings.TUNIC_STAFF_BACKEND_ROOT
        if url_base.startswith('//'):
            url_base = 'http:' + url_base
        return urljoin(
            url_base, settings.TUNIC_API_PATH.rstrip('/') + '/' + path.lstrip('/')
        )

    def get_campaigns(self, filter_active=False):
        campaigns = {}
        path = "/campaign/"
        if filter_active:
            path += "?active=True"
        while path:
            resp = self.get(path).json()
            campaigns.update({
                c["id"]: c for c in resp.get("results", {})
            })
            path = resp.get("next")
        return campaigns

    def get_active_campaigns(self):
        return self.get_campaigns(filter_active=True)
{'fraction_non_alphanumeric': 0.10474631751227496, 'fraction_numerical': 0.0, 'mean_word_length': 2.1701244813278007, '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': '5175609', 'n_tokens_mistral': 887, 'n_tokens_neox': 868, 'n_words': 192}