<commit_before>""" Request module """

# Disable warning for relative imports
# pylint: disable=W0403

import urllib2
import cookielib
import Cookie
import json


class Session(object):
    """An HTTP session"""
    def __init__(self):
        cookie_jar = cookielib.CookieJar()
        self.opener = urllib2.build_opener(
            urllib2.HTTPCookieProcessor(cookie_jar),
            # urllib2.HTTPHandler(debuglevel=1)
        )
        self.headers = {}
        self.headers['X-CSRFToken'] = ''
        self.headers['Content-Type'] = 'application/json'

    def update_csrf_token(self, cookie_content):
        """ Updates csrf token if it is found on a cookie """
        if cookie_content == '':
            return

        cookie = Cookie.SimpleCookie(cookie_content)
        if cookie.get('csrftoken') != None:
            self.headers['X-CSRFToken'] = cookie['csrftoken'].value

    def make_request(self, request):
        """ makes a request and returns an object with the result """
        res = {}
        try:
            response = self.opener.open(request)
        except urllib2.HTTPError as error:
            res['content'] = error.read()
            res['cookie'] = ''
            res['succeed'] = False
        except urllib2.URLError as error:
            res['content'] = str(error)
            res['cookie'] = ''
            res['succeed'] = False
        else:
            res['content'] = json.loads(response.read())
            res['cookie'] = response.headers.get('Set-Cookie')
            res['succeed'] = True
        return res


    def get(self, url, headers):
        """ Performs a GET to a url, with headers and using a opener with cookies """
        for key in self.headers:
            headers[key] = self.headers[key]

        request = urllib2.Request(url=url, data=None, headers=headers)
        res = self.make_request(request)<commit_msg>Added a badstatusline except control point on Request.py<commit_after>""" Request module """

# Disable warning for relative imports
# pylint: disable=W0403

import httplib
import urllib2
import cookielib
import Cookie
import json


class Session(object):
    """An HTTP session"""
    def __init__(self):
        cookie_jar = cookielib.CookieJar()
        self.opener = urllib2.build_opener(
            urllib2.HTTPCookieProcessor(cookie_jar),
            # urllib2.HTTPHandler(debuglevel=1)
        )
        self.headers = {}
        self.headers['X-CSRFToken'] = ''
        self.headers['Content-Type'] = 'application/json'

    def update_csrf_token(self, cookie_content):
        """ Updates csrf token if it is found on a cookie """
        if cookie_content == '':
            return

        cookie = Cookie.SimpleCookie(cookie_content)
        if cookie.get('csrftoken') != None:
            self.headers['X-CSRFToken'] = cookie['csrftoken'].value

    def make_request(self, request):
        """ makes a request and returns an object with the result """
        res = {}
        try:
            response = self.opener.open(request)
        except urllib2.HTTPError as error:
            res['content'] = error.read()
            res['cookie'] = ''
            res['succeed'] = False
        except urllib2.URLError as error:
            res['content'] = str(error)
            res['cookie'] = ''
            res['succeed'] = False
        except httplib.BadStatusLine as error:
            res['content'] = str(error)
            res['cookie'] = ''
            res['succeed'] = False
        else:
            res['content'] = json.loads(response.read())
            res['cookie'] = response.headers.get('Set-Cookie')
            res['succeed'] = True
        return res


    def get(self, url, headers):
        """ Performs a GET to a url, with headers and using a opener with cookies """
        for key in self.headers:
            headers[key] = self.headers[key]

        request = urllib2.Request(url=url, data=None, headers=headers)
        res = self.make_request(request)
{'fraction_non_alphanumeric': 0.12057272042200452, 'fraction_numerical': 0.006028636021100226, 'mean_word_length': 2.245313773431133, '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': '7382250', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1138, 'n_words': 281}