|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +# ----------------------------------------------------------------------------------- |
| 4 | +# <copyright company="Aspose" file="configuration.py"> |
| 5 | +# Copyright (c) 2020 Aspose.Tasks Cloud |
| 6 | +# </copyright> |
| 7 | +# <summary> |
| 8 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +# of this software and associated documentation files (the "Software"), to deal |
| 10 | +# in the Software without restriction, including without limitation the rights |
| 11 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +# copies of the Software, and to permit persons to whom the Software is |
| 13 | +# furnished to do so, subject to the following conditions: |
| 14 | +# |
| 15 | +# The above copyright notice and this permission notice shall be included in all |
| 16 | +# copies or substantial portions of the Software. |
| 17 | +# |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +# SOFTWARE. |
| 25 | +# </summary> |
| 26 | +# ----------------------------------------------------------------------------------- |
| 27 | + |
| 28 | +from __future__ import absolute_import |
| 29 | + |
| 30 | +import copy |
| 31 | +import logging |
| 32 | +import multiprocessing |
| 33 | +import sys |
| 34 | +import urllib3 |
| 35 | + |
| 36 | +import six |
| 37 | +from six.moves import http_client as httplib |
| 38 | + |
| 39 | + |
| 40 | +class TypeWithDefault(type): |
| 41 | + def __init__(cls, name, bases, dct): |
| 42 | + super(TypeWithDefault, cls).__init__(name, bases, dct) |
| 43 | + cls._default = None |
| 44 | + |
| 45 | + def __call__(cls): |
| 46 | + if cls._default is None: |
| 47 | + cls._default = type.__call__(cls) |
| 48 | + return copy.copy(cls._default) |
| 49 | + |
| 50 | + def set_default(cls, default): |
| 51 | + cls._default = copy.copy(default) |
| 52 | + |
| 53 | + |
| 54 | +class Configuration(six.with_metaclass(TypeWithDefault, object)): |
| 55 | + """ Class which contains configuration parameters |
| 56 | + """ |
| 57 | + |
| 58 | + def __init__(self): |
| 59 | + """Constructor""" |
| 60 | + # Default Base url |
| 61 | + self.host = "https://api.aspose.com" |
| 62 | + # Default api version is v1. Available values are v1, v1.1, v2, v3.0 |
| 63 | + self.api_version = "v3.0" |
| 64 | + # Temp file folder for downloading files |
| 65 | + self.temp_folder_path = None |
| 66 | + |
| 67 | + # Authentication Settings |
| 68 | + # dict to store API key(s) |
| 69 | + self.api_key = {} |
| 70 | + self.api_key['api_key'] = "" |
| 71 | + self.api_key['app_sid'] = "" |
| 72 | + # dict to store API prefix (e.g. Bearer) |
| 73 | + self.api_key_prefix = {} |
| 74 | + # Username for HTTP basic authentication |
| 75 | + self.username = "" |
| 76 | + # Password for HTTP basic authentication |
| 77 | + self.password = "" |
| 78 | + |
| 79 | + # access token for OAuth |
| 80 | + self.access_token = "" |
| 81 | + |
| 82 | + # Logging Settings |
| 83 | + self.logger = {} |
| 84 | + self.logger["package_logger"] = logging.getLogger("asposetaskscloud") |
| 85 | + self.logger["urllib3_logger"] = logging.getLogger("urllib3") |
| 86 | + # Log format |
| 87 | + self.logger_format = '%(asctime)s %(levelname)s %(message)s' |
| 88 | + # Log stream handler |
| 89 | + self.logger_stream_handler = None |
| 90 | + # Log file handler |
| 91 | + self.logger_file_handler = None |
| 92 | + # Debug file location |
| 93 | + self.logger_file = None |
| 94 | + # Debug switch |
| 95 | + self.debug = False |
| 96 | + |
| 97 | + # SSL/TLS verification |
| 98 | + # Set this to false to skip verifying SSL certificate when calling API |
| 99 | + # from https server. |
| 100 | + self.verify_ssl = True |
| 101 | + # Set this to customize the certificate file to verify the peer. |
| 102 | + self.ssl_ca_cert = None |
| 103 | + # client certificate file |
| 104 | + self.cert_file = None |
| 105 | + # client key file |
| 106 | + self.key_file = None |
| 107 | + # Set this to True/False to enable/disable SSL hostname verification. |
| 108 | + self.assert_hostname = None |
| 109 | + |
| 110 | + # urllib3 connection pool's maximum number of connections saved |
| 111 | + # per pool. urllib3 uses 1 connection as default value, but this is |
| 112 | + # not the best value when you are making a lot of possibly parallel |
| 113 | + # requests to the same host, which is often the case here. |
| 114 | + # cpu_count * 5 is used as default value to increase performance. |
| 115 | + self.connection_pool_maxsize = multiprocessing.cpu_count() * 5 |
| 116 | + |
| 117 | + # Proxy URL |
| 118 | + self.proxy = None |
| 119 | + # Safe chars for path_param |
| 120 | + self.safe_chars_for_path_param = '' |
| 121 | + |
| 122 | + @property |
| 123 | + def logger_file(self): |
| 124 | + """The logger file. |
| 125 | +
|
| 126 | + If the logger_file is None, then add stream handler and remove file |
| 127 | + handler. Otherwise, add file handler and remove stream handler. |
| 128 | +
|
| 129 | + :param value: The logger_file path. |
| 130 | + :type: str |
| 131 | + """ |
| 132 | + return self.__logger_file |
| 133 | + |
| 134 | + @logger_file.setter |
| 135 | + def logger_file(self, value): |
| 136 | + """The logger file. |
| 137 | +
|
| 138 | + If the logger_file is None, then add stream handler and remove file |
| 139 | + handler. Otherwise, add file handler and remove stream handler. |
| 140 | +
|
| 141 | + :param value: The logger_file path. |
| 142 | + :type: str |
| 143 | + """ |
| 144 | + self.__logger_file = value |
| 145 | + if self.__logger_file: |
| 146 | + # If set logging file, |
| 147 | + # then add file handler and remove stream handler. |
| 148 | + self.logger_file_handler = logging.FileHandler(self.__logger_file) |
| 149 | + self.logger_file_handler.setFormatter(self.logger_formatter) |
| 150 | + for _, logger in six.iteritems(self.logger): |
| 151 | + logger.addHandler(self.logger_file_handler) |
| 152 | + if self.logger_stream_handler: |
| 153 | + logger.removeHandler(self.logger_stream_handler) |
| 154 | + else: |
| 155 | + # If not set logging file, |
| 156 | + # then add stream handler and remove file handler. |
| 157 | + self.logger_stream_handler = logging.StreamHandler() |
| 158 | + self.logger_stream_handler.setFormatter(self.logger_formatter) |
| 159 | + for _, logger in six.iteritems(self.logger): |
| 160 | + logger.addHandler(self.logger_stream_handler) |
| 161 | + if self.logger_file_handler: |
| 162 | + logger.removeHandler(self.logger_file_handler) |
| 163 | + |
| 164 | + @property |
| 165 | + def debug(self): |
| 166 | + """Debug status |
| 167 | +
|
| 168 | + :param value: The debug status, True or False. |
| 169 | + :type: bool |
| 170 | + """ |
| 171 | + return self.__debug |
| 172 | + |
| 173 | + @debug.setter |
| 174 | + def debug(self, value): |
| 175 | + """Debug status |
| 176 | +
|
| 177 | + :param value: The debug status, True or False. |
| 178 | + :type: bool |
| 179 | + """ |
| 180 | + self.__debug = value |
| 181 | + if self.__debug: |
| 182 | + # if debug status is True, turn on debug logging |
| 183 | + for _, logger in six.iteritems(self.logger): |
| 184 | + logger.setLevel(logging.DEBUG) |
| 185 | + # turn on httplib debug |
| 186 | + httplib.HTTPConnection.debuglevel = 1 |
| 187 | + else: |
| 188 | + # if debug status is False, turn off debug logging, |
| 189 | + # setting log level to default `logging.WARNING` |
| 190 | + for _, logger in six.iteritems(self.logger): |
| 191 | + logger.setLevel(logging.WARNING) |
| 192 | + # turn off httplib debug |
| 193 | + httplib.HTTPConnection.debuglevel = 0 |
| 194 | + |
| 195 | + @property |
| 196 | + def logger_format(self): |
| 197 | + """The logger format. |
| 198 | +
|
| 199 | + The logger_formatter will be updated when sets logger_format. |
| 200 | +
|
| 201 | + :param value: The format string. |
| 202 | + :type: str |
| 203 | + """ |
| 204 | + return self.__logger_format |
| 205 | + |
| 206 | + @logger_format.setter |
| 207 | + def logger_format(self, value): |
| 208 | + """The logger format. |
| 209 | +
|
| 210 | + The logger_formatter will be updated when sets logger_format. |
| 211 | +
|
| 212 | + :param value: The format string. |
| 213 | + :type: str |
| 214 | + """ |
| 215 | + self.__logger_format = value |
| 216 | + self.logger_formatter = logging.Formatter(self.__logger_format) |
| 217 | + |
| 218 | + def get_api_key_with_prefix(self, identifier): |
| 219 | + """Gets API key (with prefix if set). |
| 220 | +
|
| 221 | + :param identifier: The identifier of apiKey. |
| 222 | + :return: The token for api key authentication. |
| 223 | + """ |
| 224 | + if (self.api_key.get(identifier) and |
| 225 | + self.api_key_prefix.get(identifier)): |
| 226 | + return self.api_key_prefix[identifier] + ' ' + self.api_key[identifier] # noqa: E501 |
| 227 | + elif self.api_key.get(identifier): |
| 228 | + return self.api_key[identifier] |
| 229 | + |
| 230 | + def get_basic_auth_token(self): |
| 231 | + """Gets HTTP basic authentication header (string). |
| 232 | +
|
| 233 | + :return: The token for basic HTTP authentication. |
| 234 | + """ |
| 235 | + return urllib3.util.make_headers( |
| 236 | + basic_auth=self.username + ':' + self.password |
| 237 | + ).get('authorization') |
| 238 | + |
| 239 | + def auth_settings(self): |
| 240 | + """Gets Auth Settings dict for api client. |
| 241 | +
|
| 242 | + :return: The Auth Settings information dict. |
| 243 | + """ |
| 244 | + return { |
| 245 | + |
| 246 | + 'JWT': |
| 247 | + { |
| 248 | + 'type': 'oauth2', |
| 249 | + 'in': 'header', |
| 250 | + 'key': 'Authorization', |
| 251 | + 'value': 'Bearer ' + self.access_token |
| 252 | + }, |
| 253 | + |
| 254 | + } |
| 255 | + |
| 256 | + def to_debug_report(self): |
| 257 | + """Gets the essential information for debugging. |
| 258 | +
|
| 259 | + :return: The report for debugging. |
| 260 | + """ |
| 261 | + return "Python SDK Debug Report:\n"\ |
| 262 | + "OS: {env}\n"\ |
| 263 | + "Python Version: {pyversion}\n"\ |
| 264 | + "Version of the API: 3.0\n"\ |
| 265 | + "SDK Package Version: 19.12.0".\ |
| 266 | + format(env=sys.platform, pyversion=sys.version) |
0 commit comments