<commit_before>import os
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()

import tempfile
import requests

from OpenSSL import crypto
from jose import jws


FURS_TEST_ENDPOINT = 'https://blagajne-test.fu.gov.si:9002'
FURS_PRODUCTION_ENDPOINT = 'https://blagajne-test.fu.gov.si:9002'

FURS_TEST_CERT = os.path.join(os.path.dirname(__file__), 'certs/test_certificate.pem')
# TODO add production CERT to certs
FURS_PRODUCTION_CERT = os.path.join(os.path.dirname(__file__), 'certs/test_certificate.pem')


class Connector(object):
    """
    Connector performs all the communication with the FURS server.

    """
    def __init__(self, p12_path, p12_password, p12_buffer=None, production=True, request_timeout=2):
        """
        Initializes and loads certs to memory.

        :param p12_path: (string) Path to the .p12 file for current client
        :param p12_password: (string) Password for the .p12 file
        :param p12_buffer: (string) Buffer of the .p12 file
        :param production: (boolean) Should we use FURS Production server of Test server
        :param request_timeout: (float) How long should we wait for the request to timeout
        :return: None
        """
        self.p12_path = p12_path
        self.p12_buffer = p12_buffer
        self.endpoint = FURS_PRODUCTION_ENDPOINT if production else FURS_TEST_ENDPOINT
        self.cert = FURS_PRODUCTION_CERT if production else FURS_TEST_CERT

        self.p12 = None
        self.cert_temp = None
        self.pkey_temp = None

        self.request_timeout = request_timeout

        self.furs_cert = open(self.cert, 'rt').read()
        # load certificate...
        self._load_p12(p12_password)

    def _load_p12(self, p12_password):
        """
        Load .p12 cert to memory

        :param p12_password: (string) password for the .p12 file
        :return: None
        """
        if self.p12_buffer is None:
            self.p12_buffer = file(self.p12_path, 'rb').read()
        self.p12 = crypto.load_pkcs12(buffer=self.p12_buffer, passphrase=<PASSWORD>)
        self._store_temp_files()

    def _store_temp_files(self):
        """
        Requests library requires string path to PKey and Cert - therefore we save those into
        temporary files on the file system.

        :return: None
        """
        self.cert_temp = tempfile.NamedTemporaryFile()
        self.cert_temp.write(crypto.dump_certificate(crypto.FILETYPE_PEM, self.p12.get_certificate()))
        self.cert_temp.flush()

        self.pkey_temp = tempfile.NamedTemporaryFile()
        self.pkey_temp.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, self.p12.get_privatekey()))
        self.pkey_temp.flush()

    def _get_jws_header(self):
        """<commit_msg>Don't delete temp file on windows<commit_after>import os
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()

import tempfile
import requests

from OpenSSL import crypto
from jose import jws


FURS_TEST_ENDPOINT = 'https://blagajne-test.fu.gov.si:9002'
FURS_PRODUCTION_ENDPOINT = 'https://blagajne-test.fu.gov.si:9002'

FURS_TEST_CERT = os.path.join(os.path.dirname(__file__), 'certs/test_certificate.pem')
# TODO add production CERT to certs
FURS_PRODUCTION_CERT = os.path.join(os.path.dirname(__file__), 'certs/test_certificate.pem')


class Connector(object):
    """
    Connector performs all the communication with the FURS server.

    """
    def __init__(self, p12_path, p12_password, p12_buffer=None, production=True, request_timeout=2):
        """
        Initializes and loads certs to memory.

        :param p12_path: (string) Path to the .p12 file for current client
        :param p12_password: (string) Password for the .p12 file
        :param p12_buffer: (string) Buffer of the .p12 file
        :param production: (boolean) Should we use FURS Production server of Test server
        :param request_timeout: (float) How long should we wait for the request to timeout
        :return: None
        """
        self.p12_path = p12_path
        self.p12_buffer = p12_buffer
        self.endpoint = FURS_PRODUCTION_ENDPOINT if production else FURS_TEST_ENDPOINT
        self.cert = FURS_PRODUCTION_CERT if production else FURS_TEST_CERT

        self.p12 = None
        self.cert_temp = None
        self.pkey_temp = None

        self.request_timeout = request_timeout

        self.furs_cert = open(self.cert, 'rt').read()
        # load certificate...
        self._load_p12(p12_password)

    def _load_p12(self, p12_password):
        """
        Load .p12 cert to memory

        :param p12_password: (string) password for the .p12 file
        :return: None
        """
        if self.p12_buffer is None:
            self.p12_buffer = file(self.p12_path, 'rb').read()
        self.p12 = crypto.load_pkcs12(buffer=self.p12_buffer, passphrase=p<PASSWORD>password)
        self._store_temp_files()

    def _store_temp_files(self):
        """
        Requests library requires string path to PKey and Cert - therefore we save those into
        temporary files on the file system.

        :return: None
        """
        self.cert_temp = tempfile.NamedTemporaryFile(delete=False)
        self.cert_temp.write(crypto.dump_certificate(crypto.FILETYPE_PEM, self.p12.get_certificate()))
        self.cert_temp.flush()

        self.pkey_temp = tempfile.NamedTemporaryFile(delete=False)
        self.pkey_temp.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, self.p12.get_privatekey()))
        self.pkey_temp.flush()

    def _get_jws_header(self):
        """
{'fraction_non_alphanumeric': 0.0924430084365464, 'fraction_numerical': 0.025130138215760187, 'mean_word_length': 3.0028735632183907, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '6240238', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1775, 'n_words': 417}