<commit_before>import requests
import socket
from netbox import exceptions


class NetboxConnection(object):

    def __init__(self, ssl_verify=False, use_ssl=True, host=None, limit=1000, auth_token=None, auth=None,
                 port=80):
        self.use_ssl = use_ssl
        self.host = host
        self.limit = limit
        self.auth_token = auth_token
        self.port = port
        self.auth = auth

        if use_ssl:
            self.port = 443

        self.base_url = 'http{s}://{host}:{p}/api'.format(s='s' if use_ssl else '', p=self.port, host=self.host)
        self.session = requests.Session()
        self.session.verify = ssl_verify

        if auth:
            self.session.auth = auth

        if auth_token:
            token = 'Token {}'.format(self.auth_token)
            self.session.headers.update({'Authorization': token})
            self.session.headers.update({'Accept': 'application/json'})
            self.session.headers.update({'Content-Type': 'application/json'})

        if auth and auth_token:
            raise ValueError('Only one authentication method is possible. Please use auth or auth_token')
        elif auth is None and auth_token is None:
            raise ValueError('Please use auth or auth_token for authentication')

    def __request(self, method, params=None, key=None, body=None):
        if key is not None:
            url = self.base_url + str(params) + str('{}/'.format(key))
        else:
            url = self.base_url + str(params)

        request = requests.Request(method=method, url=url, json=body)
        prepared_request = self.session.prepare_request(request)

        try:
            response = self.session.send(prepared_request)
        except socket.gaierror:
            err_msg = 'Unable to find address: {}'.format(self.host)
            raise socket.gaierror(err_msg)
        except requests.exceptions.ConnectionError:
            err_msg = 'Unable to connect to Netbox host: {}'.format(self.host)
            raise ConnectionError(err_msg)
        except requests.exceptions.Timeout:
            raise TimeoutError('Connection to Netbox host timed out')
        except Exception as e:
            raise Exception(e)
        finally:
            self.close()

        try:
            response_data = response.json()
        except:
            response_data = response.content

        return response.ok, response.status_code, response_data

    def get(self, params, key=None):
        self.session.params.update({'limit': self.limit})
        resp_ok, resp_status, resp_data = self.__request('GET', params, key)

        if resp_ok and resp_status == 200:
            return resp_data
        else:
            return list

    def put(self, params):
        if self.auth:
            raise exceptions.AuthException('With basic auth the API is not writable')
        return self.__request('PUT', params)<commit_msg>rewritten get method. If there is more time the url will fixed later.<commit_after>import requests
import socket
from netbox import exceptions


class NetboxConnection(object):

    def __init__(self, ssl_verify=False, use_ssl=True, host=None, limit=1000, auth_token=None, auth=None,
                 port=80):
        self.use_ssl = use_ssl
        self.host = host
        self.limit = limit
        self.auth_token = auth_token
        self.port = port
        self.auth = auth

        if use_ssl:
            self.port = 443

        self.base_url = 'http{s}://{host}:{p}/api'.format(s='s' if use_ssl else '', p=self.port, host=self.host)
        self.session = requests.Session()
        self.session.verify = ssl_verify

        if auth:
            self.session.auth = auth

        if auth_token:
            token = 'Token {}'.format(self.auth_token)
            self.session.headers.update({'Authorization': token})
            self.session.headers.update({'Accept': 'application/json'})
            self.session.headers.update({'Content-Type': 'application/json'})

        if auth and auth_token:
            raise ValueError('Only one authentication method is possible. Please use auth or auth_token')
        elif auth is None and auth_token is None:
            raise ValueError('Please use auth or auth_token for authentication')

    def __request(self, method, params=None, key=None, body=None, url=None):
        if url is None:
            if key is not None:
                url = self.base_url + str(params) + str('{}/'.format(key))
            else:
                url = self.base_url + str(params)

        request = requests.Request(method=method, url=url, json=body)
        prepared_request = self.session.prepare_request(request)

        try:
            response = self.session.send(prepared_request)
        except socket.gaierror:
            err_msg = 'Unable to find address: {}'.format(self.host)
            raise socket.gaierror(err_msg)
        except requests.exceptions.ConnectionError:
            err_msg = 'Unable to connect to Netbox host: {}'.format(self.host)
            raise ConnectionError(err_msg)
        except requests.exceptions.Timeout:
            raise TimeoutError('Connection to Netbox host timed out')
        except Exception as e:
            raise Exception(e)
        finally:
            self.close()

        try:
            response_data = response.json()
        except:
            response_data = response.content

        return response.ok, response.status_code, response_data

    def get(self, param, key=None, **kwargs):
        self.session.params.update({'limit': self.limit})

        if kwargs:
            url = '{}{}?{}'.format(self.base_url, param,
                                   '&'.join('{}={}'.format(key, val) for key, val in kwargs.items()))
        elif key:
            url = '{}{}/?q={}'.format(self.base_url, param, key)
        else:
            url = '{}{}'.format(self.base_url, param)

        resp_ok, resp_status, resp_data = self.__request('GET', params=param, key=key, url=url)

        if resp_ok and resp_status == 200:
            return resp_data['results']
        else:
            return list

    def put(self, params):
        if self.auth:
            raise exceptions.AuthException('With basic auth the API is not writable')
        return self.__request('PUT', params)
{'fraction_non_alphanumeric': 0.10701810098443951, 'fraction_numerical': 0.003810733566211496, 'mean_word_length': 2.2137755102040817, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1638158', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1718, 'n_words': 458}