<commit_before># Copyright 2015 Zadara Storage, Inc.
# Originally authored by <NAME> - https://github.com/jwbrown77
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.  You may obtain a copy
# of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.

from future.standard_library import install_aliases
install_aliases()

import configparser
import http.client
import json
import os
import sys
from urllib.parse import urlencode
from zadarapy.validators import is_valid_hostname
from zadarapy.validators import is_valid_ip_address
from zadarapy.validators import is_valid_port
from zadarapy.validators import is_valid_zadara_key


class Session(object):
    """
    The session object should be instantiated before making any calls to the
    API endpoint.  It will gather the required authentication credentials, as
    well as the URL to utilize, then make the calls to the API.
    """
    zadara_host = None
    zadara_port = None
    zadara_key = None
    zadara_secure = None

    def __init__(self, host=None, port=None, key=None, configfile=None,
                 secure=True):
        """
        Configuration details for working with the API will be gathered from
        the following, in order or preference:

        * From a keyword argument passed to the this object at instantiation
        * From the appropriate environment variable, if it exists
        * From the config file, if it exists (~/.zadarapy by default)

        If no suitable configuration is found, an exception will be thrown.

        :type host: str
        :param host: The hostname or IP address of the Zadara API.  This
            should be passed directly instead of part of a URL.  e.g.
            'vsa-00000578-aws.zadaravpsa.com', not
            'https://vsa-00000578-aws.zadaravpsa.com/'.

        :type port: int
        :param port: The port for the API endpoint.  If set to None, the
            default port for the HTTP mode will be used (80 for HTTP, 443 for
            HTTPS).  Optional.

        :type key: str
        :param key: The API key for the connecting user.

        :type configfile: str
        :param configfile: Full path to configuration file that defines values
            needed for this object.

        :type secure: bool
        :param secure: If False, the API call will be made over HTTP,
            otherwise HTTPS will be used.
        """
        self._config = None

        # If a config file exists, parse it
        if configfile is not None:
            if os.path.isfile(configfile):
                self._config = configparser.ConfigParser()
                self._config.read(configfile)

        if self._config is None and configfile is None:
            configfile = os.path.expanduser('~/.zadarapy')

            if os.path.isfile(configfile):
                self._config = configparser.ConfigParser()
                self._config.read(configfile)

        # Hostname for API endpoint
        if host is not None:
            self.zadara_host = host
        elif os.getenv('ZADARA_HOST') is not None:
            self.zadara_host = os.getenv('ZADARA_HOST')
        elif self._config is not None:
            if self._config['DEFAULT'].get('host', None) is not None:
                self.zadara_host = self._config['DEFAULT'].get('host')
        else:
            raise ValueError('The API hostname was not defined.')

        # Port for API endpoint
        if port is not None:
            self.zadara_port = port
        elif os.getenv('ZADARA_PORT') is not None:
            self.zadara_port = os.getenv('ZADARA_PORT')
        elif self._config is not None:
            if self._config['DEFAULT'].get('port', None) is not None:
                self.zadara_port = self._config['DEFAULT'].get('port')
        else:
            self.zadara_port = None

        # Authorization key for API endpoint
        if key is not None:
            self.zadara_key = key
        elif os.getenv('ZADARA_KEY') is not None:
            self.zadara_key = os.getenv('ZADARA_KEY')
        elif self._config is not None:
            if self._config['DEFAULT'].get('key', None) is not None:
                self.zadara_key = self._config['DEFAULT'].get('key')
        else:
            raise ValueError('The API authentication key was not defined.')

        # If HTTPS or HTTP should be used
        false_values = ['false', 'no', 'off', 'n']

        if secure is False:
            self.zadara_secure = False
        elif os.getenv('ZADARA_SECURE') is not None:
            if os.getenv('ZADARA_SECURE').lower() in false_values:
                self.zadara_secure = False
        elif self._config is not None:
            if self._config['DEFAULT'].get('secure', None) is not None:
                if self._config['DEFAULT'].get('secure').lower() in \<commit_msg>DEFAULT object in ini parser is not a dictionary key.  Use defaults method instead.<commit_after># Copyright 2015 Zadara Storage, Inc.
# Originally authored by <NAME> - https://github.com/jwbrown77
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.  You may obtain a copy
# of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.

from future.standard_library import install_aliases
install_aliases()

import configparser
import http.client
import json
import os
import sys
from urllib.parse import urlencode
from zadarapy.validators import is_valid_hostname
from zadarapy.validators import is_valid_ip_address
from zadarapy.validators import is_valid_port
from zadarapy.validators import is_valid_zadara_key


class Session(object):
    """
    The session object should be instantiated before making any calls to the
    API endpoint.  It will gather the required authentication credentials, as
    well as the URL to utilize, then make the calls to the API.
    """
    zadara_host = None
    zadara_port = None
    zadara_key = None
    zadara_secure = None

    def __init__(self, host=None, port=None, key=None, configfile=None,
                 secure=True):
        """
        Configuration details for working with the API will be gathered from
        the following, in order or preference:

        * From a keyword argument passed to the this object at instantiation
        * From the appropriate environment variable, if it exists
        * From the config file, if it exists (~/.zadarapy by default)

        If no suitable configuration is found, an exception will be thrown.

        :type host: str
        :param host: The hostname or IP address of the Zadara API.  This
            should be passed directly instead of part of a URL.  e.g.
            'vsa-00000578-aws.zadaravpsa.com', not
            'https://vsa-00000578-aws.zadaravpsa.com/'.

        :type port: int
        :param port: The port for the API endpoint.  If set to None, the
            default port for the HTTP mode will be used (80 for HTTP, 443 for
            HTTPS).  Optional.

        :type key: str
        :param key: The API key for the connecting user.

        :type configfile: str
        :param configfile: Full path to configuration file that defines values
            needed for this object.

        :type secure: bool
        :param secure: If False, the API call will be made over HTTP,
            otherwise HTTPS will be used.
        """
        self._config = None

        # If a config file exists, parse it
        if configfile is not None:
            if os.path.isfile(configfile):
                self._config = configparser.ConfigParser()
                self._config.read(configfile)

        if self._config is None and configfile is None:
            configfile = os.path.expanduser('~/.zadarapy')

            if os.path.isfile(configfile):
                self._config = configparser.ConfigParser()
                self._config.read(configfile)

        # Hostname for API endpoint
        if host is not None:
            self.zadara_host = host
        elif os.getenv('ZADARA_HOST') is not None:
            self.zadara_host = os.getenv('ZADARA_HOST')
        elif self._config is not None:
            if self._config.defaults().get('host', None) is not None:
                self.zadara_host = self._config.defaults().get('host')
        else:
            raise ValueError('The API hostname was not defined.')

        # Port for API endpoint
        if port is not None:
            self.zadara_port = port
        elif os.getenv('ZADARA_PORT') is not None:
            self.zadara_port = os.getenv('ZADARA_PORT')
        elif self._config is not None:
            if self._config.defaults().get('port', None) is not None:
                self.zadara_port = self._config.defaults().get('port')
        else:
            self.zadara_port = None

        # Authorization key for API endpoint
        if key is not None:
            self.zadara_key = key
        elif os.getenv('ZADARA_KEY') is not None:
            self.zadara_key = os.getenv('ZADARA_KEY')
        elif self._config is not None:
            if self._config.defaults().get('key', None) is not None:
                self.zadara_key = self._config.defaults().get('key')
        else:
            raise ValueError('The API authentication key was not defined.')

        # If HTTPS or HTTP should be used
        false_values = ['false', 'no', 'off', 'n']

        if secure is False:
            self.zadara_secure = False
        elif os.getenv('ZADARA_SECURE') is not None:
            if os.getenv('ZADARA_SECURE').lower() in false_values:
                self.zadara_secure = False
        elif self._config is not None:
            if self._config.defaults().get('secure', None) is not None:
                if self._config.defaults().get('secure').lower() in \
{'fraction_non_alphanumeric': 0.07899255867200916, 'fraction_numerical': 0.005914901736309865, 'mean_word_length': 2.333227344992051, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '6232348', 'n_tokens_mistral': 2943, 'n_tokens_neox': 2840, 'n_words': 1081}