<commit_before># ==============================
# Copyright 2011 Whamcloud, Inc.
# ==============================

import sys

from chroma_cli.exceptions import BadRequest

import requests
from urlparse import urljoin
import simplejson as json


class ApiClient(object):
    __schema = None

    def __init__(self, url, username=None, password=<PASSWORD>):
        self.url = url

        self._session = requests.session(headers = {'Accept': "application/json", 'Content-Type': "application/json"})
        response = self._session.get(urljoin(self.url, "session/"))
        if not 200 <= response.status_code < 300:
            raise RuntimeError("Failed to open session")
        self._session.headers['X-CSRFToken'] = response.cookies['csrftoken']
        self._session.cookies['csrftoken'] = response.cookies['csrftoken']
        self._session.cookies['sessionid'] = response.cookies['sessionid']

        self.login(username, password)

    def handle_response(self, response):
        if 200 <= response.status_code < 304:
            return response.text
        elif response.status_code == 400:
            # A bad request -- deserialize the errors
            raise BadRequest(json.loads(response.text))
        elif response.status_code == 404:
            raise AttributeError("No resource with that id")
        else:  # TODO: add more classes of reponse exceptions
            raise RuntimeError(response.text)

    def login(self, username, password):
        response = self._session.post(urljoin(self.url, "session/"),
                                      data = json.dumps({'username': username,
                                                        'password': password}))
        if not 200 <= response.status_code < 300:
            raise RuntimeError("failed to authenticate")

    def get(self, url):
        response = self._session.get(urljoin(self.url, url))
        return json.loads(self.handle_response(response))

    def post(self, url, **kwargs):
        response = self._session.post(url, json.dumps(kwargs))
        return json.loads(self.handle_response(response))

    def delete(self, url, **kwargs):
        response = self._session.delete(url)
        return json.loads(self.handle_response(response))

    def put(self, url, **kwargs):<commit_msg>Include HTTP status code in unknown exceptions<commit_after># ==============================
# Copyright 2011 Whamcloud, Inc.
# ==============================

import sys

from chroma_cli.exceptions import BadRequest

import requests
from urlparse import urljoin
import simplejson as json


class ApiClient(object):
    __schema = None

    def __init__(self, url, username=None, password=<PASSWORD>):
        self.url = url

        self._session = requests.session(headers = {'Accept': "application/json", 'Content-Type': "application/json"})
        response = self._session.get(urljoin(self.url, "session/"))
        if not 200 <= response.status_code < 300:
            raise RuntimeError("Failed to open session")
        self._session.headers['X-CSRFToken'] = response.cookies['csrftoken']
        self._session.cookies['csrftoken'] = response.cookies['csrftoken']
        self._session.cookies['sessionid'] = response.cookies['sessionid']

        self.login(username, password)

    def handle_response(self, response):
        if 200 <= response.status_code < 304:
            return response.text
        elif response.status_code == 400:
            # A bad request -- deserialize the errors
            raise BadRequest(json.loads(response.text))
        elif response.status_code == 404:
            raise AttributeError("No resource with that id")
        else:  # TODO: add more classes of reponse exceptions
            raise RuntimeError("status: %s, text: %s" % (response.status_code,
                                                         response.text))

    def login(self, username, password):
        response = self._session.post(urljoin(self.url, "session/"),
                                      data = json.dumps({'username': username,
                                                        'password': password}))
        if not 200 <= response.status_code < 300:
            raise RuntimeError("failed to authenticate")

    def get(self, url):
        response = self._session.get(urljoin(self.url, url))
        return json.loads(self.handle_response(response))

    def post(self, url, **kwargs):
        response = self._session.post(url, json.dumps(kwargs))
        return json.loads(self.handle_response(response))

    def delete(self, url, **kwargs):
        response = self._session.delete(url)
        return json.loads(self.handle_response(response))

    def put(self, url, **kwargs):
{'fraction_non_alphanumeric': 0.13471722092776953, 'fraction_numerical': 0.01186189366659606, 'mean_word_length': 2.6407093292212798, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4858212', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1217, 'n_words': 306}