<commit_before>#!/usr/bin/env python
#
# ==============================
# Copyright 2011 Whamcloud, Inc.
# ==============================

import json
import requests
from urlparse import urljoin


class HttpRequests(object):
    def __init__(self, server_http_url = '', *args, **kwargs):
        self.server_http_url = server_http_url
        self.session = requests.session(headers = {"Accept": "application/json", "Content-type": "application/json"})

    def get(self, url, **kwargs):
        response = self.session.get(
            urljoin(self.server_http_url, url),
            **kwargs
        )

        return HttpResponse(response)

    def post(self, url, body = None, **kwargs):
        if body and 'data' not in kwargs:
            kwargs['data'] = json.dumps(body)

        response = self.session.post(
            urljoin(self.server_http_url, url),
            **kwargs
        )

        return HttpResponse(response)

    def put(self, url, **kwargs):
        response = self.session.put(
            urljoin(self.server_http_url, url),
            **kwargs
        )

        return HttpResponse(response)

    def delete(self, url, **kwargs):
        response = self.session.delete(
            urljoin(self.server_http_url, url),
            **kwargs
        )

        return HttpResponse(response)

    def request(self, method, url, **kwargs):
        response = self.session.request(
            method,
            urljoin(self.server_http_url, url),
            **kwargs
        )

        return HttpResponse(response)


class HttpResponse(requests.Response):
    def __init__(self, response, *args, **kwargs):
        super(HttpResponse, self).__init__(*args, **kwargs)
        self.__dict__.update(response.__dict__.copy())

    @property
    def json(self):
        if self.text == '[]':
            return []
        else:
            return json.loads(self.text)

    @property
    def successful(self):
        # TODO: Make better
        return 200 <= self.status_code < 300


class AuthorizedHttpRequests(HttpRequests):
    def __init__(self, username, password, *args, **kwargs):
        super(AuthorizedHttpRequests, self).__init__(*args, **kwargs)

        response = self.get("/api/session/", data = {'username': 'admin', 'password': 'password'})
        if not response.successful:
            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']

        response = self.post("/api/session/", data = json.dumps({'username': username, 'password': password}))
        if not response.successful:
            raise RuntimeError("Failed to authenticate")
<commit_msg>Remove a piece of dead code<commit_after>#!/usr/bin/env python
#
# ==============================
# Copyright 2011 Whamcloud, Inc.
# ==============================

import json
import requests
from urlparse import urljoin


class HttpRequests(object):
    def __init__(self, server_http_url = '', *args, **kwargs):
        self.server_http_url = server_http_url
        self.session = requests.session(headers = {"Accept": "application/json", "Content-type": "application/json"})

    def get(self, url, **kwargs):
        response = self.session.get(
            urljoin(self.server_http_url, url),
            **kwargs
        )

        return HttpResponse(response)

    def post(self, url, body = None, **kwargs):
        if body and 'data' not in kwargs:
            kwargs['data'] = json.dumps(body)

        response = self.session.post(
            urljoin(self.server_http_url, url),
            **kwargs
        )

        return HttpResponse(response)

    def put(self, url, **kwargs):
        response = self.session.put(
            urljoin(self.server_http_url, url),
            **kwargs
        )

        return HttpResponse(response)

    def delete(self, url, **kwargs):
        response = self.session.delete(
            urljoin(self.server_http_url, url),
            **kwargs
        )

        return HttpResponse(response)

    def request(self, method, url, **kwargs):
        response = self.session.request(
            method,
            urljoin(self.server_http_url, url),
            **kwargs
        )

        return HttpResponse(response)


class HttpResponse(requests.Response):
    def __init__(self, response, *args, **kwargs):
        super(HttpResponse, self).__init__(*args, **kwargs)
        self.__dict__.update(response.__dict__.copy())

    @property
    def json(self):
        if self.text == '[]':
            return []
        else:
            return json.loads(self.text)

    @property
    def successful(self):
        # TODO: Make better
        return 200 <= self.status_code < 300


class AuthorizedHttpRequests(HttpRequests):
    def __init__(self, username, password, *args, **kwargs):
        super(AuthorizedHttpRequests, self).__init__(*args, **kwargs)

        response = self.get("/api/session/")
        if not response.successful:
            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']

        response = self.post("/api/session/", data = json.dumps({'username': username, 'password': password}))
        if not response.successful:
            raise RuntimeError("Failed to authenticate")

{'fraction_non_alphanumeric': 0.14425820256776034, 'fraction_numerical': 0.003566333808844508, 'mean_word_length': 2.572611464968153, 'pattern_counts': {'":': 4, '<': 7, '<?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': '7632492', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1538, 'n_words': 346}