<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

import json
import os
import requests
import tarfile

from clint.textui import progress
from clint.textui.progress import Bar
from hestia.list_utils import to_list
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor

from polyaxon_client import settings
from polyaxon_client.exceptions import ERRORS_MAPPING, PolyaxonShouldExitError
from polyaxon_client.logger import logger


class HttpTransportMixin(object):
    """HTTP operations transport."""
    MAX_UPLOAD_SIZE = 1024 * 1024 * 150

    @property
    def session(self):
        if not hasattr(self, '_session'):
            self._session = requests.Session()
        return self._session

    @staticmethod
    def create_progress_callback(encoder):
        encoder_len = encoder.len
        progress_bar = Bar(expected_size=encoder_len, filled_char='=')

        def callback(monitor):
            progress_bar.show(monitor.bytes_read)

        return callback, progress_bar

    @staticmethod
    def format_sizeof(num, suffix='B'):
        """
        Print in human friendly format
        """
        for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
            if abs(num) < 1024.0:
                return "%3.1f%s%s" % (num, unit, suffix)
            num /= 1024.0
        return "%.1f%s%s" % (num, 'Yi', suffix)

    def request(self,
                method,
                url,
                params=None,
                data=None,
                files=None,
                json=None,  # noqa
                timeout=None,
                headers=None,
                session=None):
        """Send a request with the given data as json to the given URL.

        Args:
            method: HTTP method
            url: The url to send the request to.
            params: Dictionary of values to format url.
            data:
            files:
            json:
            timeout:
            headers: extra headers to add to the request.

        Returns:
            Request response if successful, Exception otherwise.

        Raises:
            PolyaxonHTTPError or one of its subclasses.
        """
        logger.debug("Starting request to url: %s with params: %s, data: %s",
                     url, params, data)

        request_headers = self._get_headers(headers=headers)
        timeout = timeout if timeout is not None else settings.LONG_REQUEST_TIMEOUT
        session = session or self.session

        try:
            response = session.request(method,
                                       url,
                                       params=params,
                                       data=data,
                                       json=json,
                                       headers=request_headers,
                                       files=files,
                                       timeout=timeout)
        except (requests.exceptions.RequestException,
                requests.exceptions.Timeout,
                requests.exceptions.HTTPError) as exception:
            try:
                logger.debug("Exception: %s", exception, exc_info=True)
            except TypeError:
                pass
            raise PolyaxonShouldExitError(
                "Error connecting to Polyaxon server on `{}`.\n"<commit_msg>set verify in session request to VERIFY_SSL setting if it is not None, otherwise default to True.<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

import json
import os
import requests
import tarfile

from clint.textui import progress
from clint.textui.progress import Bar
from hestia.list_utils import to_list
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor

from polyaxon_client import settings
from polyaxon_client.exceptions import ERRORS_MAPPING, PolyaxonShouldExitError
from polyaxon_client.logger import logger


class HttpTransportMixin(object):
    """HTTP operations transport."""
    MAX_UPLOAD_SIZE = 1024 * 1024 * 150

    @property
    def session(self):
        if not hasattr(self, '_session'):
            self._session = requests.Session()
        return self._session

    @staticmethod
    def create_progress_callback(encoder):
        encoder_len = encoder.len
        progress_bar = Bar(expected_size=encoder_len, filled_char='=')

        def callback(monitor):
            progress_bar.show(monitor.bytes_read)

        return callback, progress_bar

    @staticmethod
    def format_sizeof(num, suffix='B'):
        """
        Print in human friendly format
        """
        for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
            if abs(num) < 1024.0:
                return "%3.1f%s%s" % (num, unit, suffix)
            num /= 1024.0
        return "%.1f%s%s" % (num, 'Yi', suffix)

    def request(self,
                method,
                url,
                params=None,
                data=None,
                files=None,
                json=None,  # noqa
                timeout=None,
                headers=None,
                session=None):
        """Send a request with the given data as json to the given URL.

        Args:
            method: HTTP method
            url: The url to send the request to.
            params: Dictionary of values to format url.
            data:
            files:
            json:
            timeout:
            headers: extra headers to add to the request.

        Returns:
            Request response if successful, Exception otherwise.

        Raises:
            PolyaxonHTTPError or one of its subclasses.
        """
        logger.debug("Starting request to url: %s with params: %s, data: %s",
                     url, params, data)

        request_headers = self._get_headers(headers=headers)
        timeout = timeout if timeout is not None else settings.LONG_REQUEST_TIMEOUT
        session = session or self.session
        verify = settings.VERIFY_SSL if settings.VERIFY_SSL is not None else True

        try:
            response = session.request(method,
                                       url,
                                       params=params,
                                       data=data,
                                       json=json,
                                       headers=request_headers,
                                       files=files,
                                       timeout=timeout,
                                       verify=verify)
        except (requests.exceptions.RequestException,
                requests.exceptions.Timeout,
                requests.exceptions.HTTPError) as exception:
            try:
                logger.debug("Exception: %s", exception, exc_info=True)
            except TypeError:
                pass
            raise PolyaxonShouldExitError(
                "Error connecting to Polyaxon server on `{}`.\n"
{'fraction_non_alphanumeric': 0.07682366456059736, 'fraction_numerical': 0.007179781734635267, 'mean_word_length': 1.6243406179351922, 'pattern_counts': {'":': 0, '<': 5, '<?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': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7310036', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1679, 'n_words': 517}