<commit_before>import requests
import json
from hashlib import md5
from .models import *
from .exceptions import *


class TBA:
    """
    Main library class.

    Contains methods for interacting with The Blue Alliance.
    """

    READ_URL_PRE = 'https://www.thebluealliance.com/api/v3/'
    WRITE_URL_PRE = 'https://www.thebluealliance.com/api/trusted/v1/'
    session = requests.Session()
    auth_secret = ''
    event_key = ''

    def __init__(self, auth_key, auth_id='', auth_secret='', event_key=''):
        """
        Store auth key so we can reuse it as many times as we make a request.

        :param auth_key: Your application authorization key, obtainable at https://www.thebluealliance.com/account.
        :param auth_id: Your event authorization ID, obtainable at https://www.thebluealliance.com/request/apiwrite
        :param auth_secret: Your event authorization secret, obtainable at https://www.thebluealliance.com/request/apiwrite
        :param event_key: The event key that is linked to the ID and secret provided.
        """
        self.auth_secret = auth_secret
        self.event_key = event_key
        self.session.headers.update({'X-TBA-Auth-Key': auth_key, 'X-TBA-Auth-Id': auth_id})

    def _get(self, url):
        """
        Helper method: GET data from given URL on TBA's API.

        :param url: URL string to get data from.
        :return: Requested data in JSON format.
        """
        raw = self.session.get(self.READ_URL_PRE + url).json()
        self._detect_errors(raw)
        return raw

    def _post(self, url, data):
        """
        Helper method: POST data to a given URL on TBA's API.

        :param url: URL string to post data to and hash.
        :pararm data: JSON data to post and hash.
        :return: Requests Response object.

        """
        raw = self.session.post(
            self.WRITE_URL_PRE + url % self.event_key, 
            data=data, 
            headers={
                'X-TBA-Auth-Sig': md5((self.auth_secret + '/api/trusted/v1/' + url % self.event_key + data).encode('utf-8')).hexdigest()
            }
        )

        self._detect_errors(raw)
        return raw

    def _detect_errors(self, json):
        if not isinstance(json, dict):
            return

        errors = json.get('Errors')
        if errors is not None:
            raise TBAErrorList([error.popitem() for error in errors])

    @staticmethod
    def team_key(identifier):
        """
        Take raw team number or string key and return string key.

        Used by all team-related methods to support either an integer team number or team key being passed.

        (We recommend passing an integer, just because it's cleaner. But whatever works.)

        :param identifier: int team number or str 'frc####'
        :return: string team key in format 'frc####'
        """
        return identifier if type(identifier) == str else 'frc%s' % identifier

    def status(self):
        """
        Get TBA API status information.

        :return: Data on current status of the TBA API as APIStatus object.
        """
        return APIStatus(self._get('status'))<commit_msg>Add basic extra headers<commit_after>import requests
import json
from hashlib import md5
from .models import *
from .exceptions import *


class TBA:
    """
    Main library class.

    Contains methods for interacting with The Blue Alliance.
    """

    READ_URL_PRE = 'https://www.thebluealliance.com/api/v3/'
    WRITE_URL_PRE = 'https://www.thebluealliance.com/api/trusted/v1/'
    session = requests.Session()
    auth_secret = ''
    event_key = ''

    def __init__(self, auth_key, auth_id='', auth_secret='', event_key=''):
        """
        Store auth key so we can reuse it as many times as we make a request.

        :param auth_key: Your application authorization key, obtainable at https://www.thebluealliance.com/account.
        :param auth_id: Your event authorization ID, obtainable at https://www.thebluealliance.com/request/apiwrite
        :param auth_secret: Your event authorization secret, obtainable at https://www.thebluealliance.com/request/apiwrite
        :param event_key: The event key that is linked to the ID and secret provided.
        """
        self.auth_secret = auth_secret
        self.event_key = event_key
        self.session.headers.update({'X-TBA-Auth-Key': auth_key, 'X-TBA-Auth-Id': auth_id})
        self.if_modified_since = None

    def _get(self, url):
        """
        Helper method: GET data from given URL on TBA's API.

        :param url: URL string to get data from.
        :return: Requested data in JSON format.
        """
        extra_headers = {}
        if self.if_modified_since is not None:
            extra_headers['If-Modified-Since': self.if_modified_since]
        
        response = self.session.get(self.READ_URL_PRE + url, headers=extra_headers)
        last_modified = response.headers.get('Last-Modified')
        if response.status_code == 304 and last_modified is not None:
            raise LastModifiedException(response.headers['Last-Modified'])

        raw = response.json()
        self._detect_errors(raw)
        return raw

    def _post(self, url, data):
        """
        Helper method: POST data to a given URL on TBA's API.

        :param url: URL string to post data to and hash.
        :pararm data: JSON data to post and hash.
        :return: Requests Response object.

        """
        raw = self.session.post(
            self.WRITE_URL_PRE + url % self.event_key, 
            data=data, 
            headers={
                'X-TBA-Auth-Sig': md5((self.auth_secret + '/api/trusted/v1/' + url % self.event_key + data).encode('utf-8')).hexdigest()
            }
        )

        self._detect_errors(raw)
        return raw

    def _detect_errors(self, json):
        if not isinstance(json, dict):
            return

        errors = json.get('Errors')
        if errors is not None:
            raise TBAErrorList([error.popitem() for error in errors])

    def _cache_response(func):
        def wrapper(*args, **kwargs):

            func(*args, **kwargs)
        return wrapper

    @staticmethod
    def team_key(identifier):
        """
        Take raw team number or string key and return string key.

        Used by all team-related methods to support either an integer team number or team key being passed.

        (We recommend passing an integer, just because it's cleaner. But whatever works.)

        :param identifier: int team number or str 'frc####'
        :return: string team key in format 'frc####'
        """
        return identifier if type(identifier) == str else 'frc%s' % identifier

    def status(self):
        """
        Get TBA API status information.

        :return: Data on current status of the TBA API as APIStatus object.
        """
        return APIStatus(self._get('status'))
{'fraction_non_alphanumeric': 0.10007251631617115, 'fraction_numerical': 0.002175489485134155, 'mean_word_length': 2.3233734939759034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 10, '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': '710242', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1905, 'n_words': 636}