You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

426 lines
14 KiB

2 years ago
  1. # coding: utf-8
  2. """
  3. Seaweedfs Master Server API
  4. The Seaweedfs Master Server API allows you to store blobs # noqa: E501
  5. The version of the OpenAPI document: 3.43.0
  6. Generated by: https://openapi-generator.tech
  7. """
  8. from __future__ import absolute_import
  9. import copy
  10. import logging
  11. import multiprocessing
  12. import sys
  13. import urllib3
  14. import http.client as httplib
  15. from openapi_client.exceptions import ApiValueError
  16. JSON_SCHEMA_VALIDATION_KEYWORDS = {
  17. 'multipleOf', 'maximum', 'exclusiveMaximum',
  18. 'minimum', 'exclusiveMinimum', 'maxLength',
  19. 'minLength', 'pattern', 'maxItems', 'minItems'
  20. }
  21. class Configuration(object):
  22. """NOTE: This class is auto generated by OpenAPI Generator
  23. Ref: https://openapi-generator.tech
  24. Do not edit the class manually.
  25. :param host: Base url.
  26. :param api_key: Dict to store API key(s).
  27. Each entry in the dict specifies an API key.
  28. The dict key is the name of the security scheme in the OAS specification.
  29. The dict value is the API key secret.
  30. :param api_key_prefix: Dict to store API prefix (e.g. Bearer).
  31. The dict key is the name of the security scheme in the OAS specification.
  32. The dict value is an API key prefix when generating the auth data.
  33. :param username: Username for HTTP basic authentication.
  34. :param password: Password for HTTP basic authentication.
  35. :param access_token: Access token.
  36. :param server_index: Index to servers configuration.
  37. :param server_variables: Mapping with string values to replace variables in
  38. templated server configuration. The validation of enums is performed for
  39. variables with defined enum values before.
  40. :param server_operation_index: Mapping from operation ID to an index to server
  41. configuration.
  42. :param server_operation_variables: Mapping from operation ID to a mapping with
  43. string values to replace variables in templated server configuration.
  44. The validation of enums is performed for variables with defined enum values before.
  45. :param ssl_ca_cert: str - the path to a file of concatenated CA certificates
  46. in PEM format.
  47. """
  48. _default = None
  49. def __init__(self, host=None,
  50. api_key=None, api_key_prefix=None,
  51. username=None, password=None,
  52. access_token=None,
  53. server_index=None, server_variables=None,
  54. server_operation_index=None, server_operation_variables=None,
  55. ssl_ca_cert=None,
  56. ):
  57. """Constructor
  58. """
  59. self._base_path = "https://127.0.0.1:9333" if host is None else host
  60. """Default Base url
  61. """
  62. self.server_index = 0 if server_index is None and host is None else server_index
  63. self.server_operation_index = server_operation_index or {}
  64. """Default server index
  65. """
  66. self.server_variables = server_variables or {}
  67. self.server_operation_variables = server_operation_variables or {}
  68. """Default server variables
  69. """
  70. self.temp_folder_path = None
  71. """Temp file folder for downloading files
  72. """
  73. # Authentication Settings
  74. self.api_key = {}
  75. if api_key:
  76. self.api_key = api_key
  77. """dict to store API key(s)
  78. """
  79. self.api_key_prefix = {}
  80. if api_key_prefix:
  81. self.api_key_prefix = api_key_prefix
  82. """dict to store API prefix (e.g. Bearer)
  83. """
  84. self.refresh_api_key_hook = None
  85. """function hook to refresh API key if expired
  86. """
  87. self.username = username
  88. """Username for HTTP basic authentication
  89. """
  90. self.password = password
  91. """Password for HTTP basic authentication
  92. """
  93. self.access_token = access_token
  94. """Access token
  95. """
  96. self.logger = {}
  97. """Logging Settings
  98. """
  99. self.logger["package_logger"] = logging.getLogger("openapi_client")
  100. self.logger["urllib3_logger"] = logging.getLogger("urllib3")
  101. self.logger_format = '%(asctime)s %(levelname)s %(message)s'
  102. """Log format
  103. """
  104. self.logger_stream_handler = None
  105. """Log stream handler
  106. """
  107. self.logger_file_handler = None
  108. """Log file handler
  109. """
  110. self.logger_file = None
  111. """Debug file location
  112. """
  113. self.debug = False
  114. """Debug switch
  115. """
  116. self.verify_ssl = True
  117. """SSL/TLS verification
  118. Set this to false to skip verifying SSL certificate when calling API
  119. from https server.
  120. """
  121. self.ssl_ca_cert = ssl_ca_cert
  122. """Set this to customize the certificate file to verify the peer.
  123. """
  124. self.cert_file = None
  125. """client certificate file
  126. """
  127. self.key_file = None
  128. """client key file
  129. """
  130. self.assert_hostname = None
  131. """Set this to True/False to enable/disable SSL hostname verification.
  132. """
  133. self.connection_pool_maxsize = multiprocessing.cpu_count() * 5
  134. """urllib3 connection pool's maximum number of connections saved
  135. per pool. urllib3 uses 1 connection as default value, but this is
  136. not the best value when you are making a lot of possibly parallel
  137. requests to the same host, which is often the case here.
  138. cpu_count * 5 is used as default value to increase performance.
  139. """
  140. self.proxy = None
  141. """Proxy URL
  142. """
  143. self.proxy_headers = None
  144. """Proxy headers
  145. """
  146. self.safe_chars_for_path_param = ''
  147. """Safe chars for path_param
  148. """
  149. self.retries = None
  150. """Adding retries to override urllib3 default value 3
  151. """
  152. # Enable client side validation
  153. self.client_side_validation = True
  154. self.socket_options = None
  155. """Options to pass down to the underlying urllib3 socket
  156. """
  157. def __deepcopy__(self, memo):
  158. cls = self.__class__
  159. result = cls.__new__(cls)
  160. memo[id(self)] = result
  161. for k, v in self.__dict__.items():
  162. if k not in ('logger', 'logger_file_handler'):
  163. setattr(result, k, copy.deepcopy(v, memo))
  164. # shallow copy of loggers
  165. result.logger = copy.copy(self.logger)
  166. # use setters to configure loggers
  167. result.logger_file = self.logger_file
  168. result.debug = self.debug
  169. return result
  170. def __setattr__(self, name, value):
  171. object.__setattr__(self, name, value)
  172. @classmethod
  173. def set_default(cls, default):
  174. """Set default instance of configuration.
  175. It stores default configuration, which can be
  176. returned by get_default_copy method.
  177. :param default: object of Configuration
  178. """
  179. cls._default = default
  180. @classmethod
  181. def get_default_copy(cls):
  182. """Deprecated. Please use `get_default` instead.
  183. Deprecated. Please use `get_default` instead.
  184. :return: The configuration object.
  185. """
  186. return cls.get_default()
  187. @classmethod
  188. def get_default(cls):
  189. """Return the default configuration.
  190. This method returns newly created, based on default constructor,
  191. object of Configuration class or returns a copy of default
  192. configuration.
  193. :return: The configuration object.
  194. """
  195. if cls._default is None:
  196. cls._default = Configuration()
  197. return cls._default
  198. @property
  199. def logger_file(self):
  200. """The logger file.
  201. If the logger_file is None, then add stream handler and remove file
  202. handler. Otherwise, add file handler and remove stream handler.
  203. :param value: The logger_file path.
  204. :type: str
  205. """
  206. return self.__logger_file
  207. @logger_file.setter
  208. def logger_file(self, value):
  209. """The logger file.
  210. If the logger_file is None, then add stream handler and remove file
  211. handler. Otherwise, add file handler and remove stream handler.
  212. :param value: The logger_file path.
  213. :type: str
  214. """
  215. self.__logger_file = value
  216. if self.__logger_file:
  217. # If set logging file,
  218. # then add file handler and remove stream handler.
  219. self.logger_file_handler = logging.FileHandler(self.__logger_file)
  220. self.logger_file_handler.setFormatter(self.logger_formatter)
  221. for _, logger in self.logger.items():
  222. logger.addHandler(self.logger_file_handler)
  223. @property
  224. def debug(self):
  225. """Debug status
  226. :param value: The debug status, True or False.
  227. :type: bool
  228. """
  229. return self.__debug
  230. @debug.setter
  231. def debug(self, value):
  232. """Debug status
  233. :param value: The debug status, True or False.
  234. :type: bool
  235. """
  236. self.__debug = value
  237. if self.__debug:
  238. # if debug status is True, turn on debug logging
  239. for _, logger in self.logger.items():
  240. logger.setLevel(logging.DEBUG)
  241. # turn on httplib debug
  242. httplib.HTTPConnection.debuglevel = 1
  243. else:
  244. # if debug status is False, turn off debug logging,
  245. # setting log level to default `logging.WARNING`
  246. for _, logger in self.logger.items():
  247. logger.setLevel(logging.WARNING)
  248. # turn off httplib debug
  249. httplib.HTTPConnection.debuglevel = 0
  250. @property
  251. def logger_format(self):
  252. """The logger format.
  253. The logger_formatter will be updated when sets logger_format.
  254. :param value: The format string.
  255. :type: str
  256. """
  257. return self.__logger_format
  258. @logger_format.setter
  259. def logger_format(self, value):
  260. """The logger format.
  261. The logger_formatter will be updated when sets logger_format.
  262. :param value: The format string.
  263. :type: str
  264. """
  265. self.__logger_format = value
  266. self.logger_formatter = logging.Formatter(self.__logger_format)
  267. def get_api_key_with_prefix(self, identifier, alias=None):
  268. """Gets API key (with prefix if set).
  269. :param identifier: The identifier of apiKey.
  270. :param alias: The alternative identifier of apiKey.
  271. :return: The token for api key authentication.
  272. """
  273. if self.refresh_api_key_hook is not None:
  274. self.refresh_api_key_hook(self)
  275. key = self.api_key.get(identifier, self.api_key.get(alias) if alias is not None else None)
  276. if key:
  277. prefix = self.api_key_prefix.get(identifier)
  278. if prefix:
  279. return "%s %s" % (prefix, key)
  280. else:
  281. return key
  282. def get_basic_auth_token(self):
  283. """Gets HTTP basic authentication header (string).
  284. :return: The token for basic HTTP authentication.
  285. """
  286. username = ""
  287. if self.username is not None:
  288. username = self.username
  289. password = ""
  290. if self.password is not None:
  291. password = self.password
  292. return urllib3.util.make_headers(
  293. basic_auth=username + ':' + password
  294. ).get('authorization')
  295. def auth_settings(self):
  296. """Gets Auth Settings dict for api client.
  297. :return: The Auth Settings information dict.
  298. """
  299. auth = {}
  300. return auth
  301. def to_debug_report(self):
  302. """Gets the essential information for debugging.
  303. :return: The report for debugging.
  304. """
  305. return "Python SDK Debug Report:\n"\
  306. "OS: {env}\n"\
  307. "Python Version: {pyversion}\n"\
  308. "Version of the API: 3.43.0\n"\
  309. "SDK Package Version: 1.0.0".\
  310. format(env=sys.platform, pyversion=sys.version)
  311. def get_host_settings(self):
  312. """Gets an array of host settings
  313. :return: An array of host settings
  314. """
  315. return [
  316. {
  317. 'url': "https://127.0.0.1:9333",
  318. 'description': "No description provided",
  319. }
  320. ]
  321. def get_host_from_settings(self, index, variables=None, servers=None):
  322. """Gets host URL based on the index and variables
  323. :param index: array index of the host settings
  324. :param variables: hash of variable and the corresponding value
  325. :param servers: an array of host settings or None
  326. :return: URL based on host settings
  327. """
  328. if index is None:
  329. return self._base_path
  330. variables = {} if variables is None else variables
  331. servers = self.get_host_settings() if servers is None else servers
  332. try:
  333. server = servers[index]
  334. except IndexError:
  335. raise ValueError(
  336. "Invalid index {0} when selecting the host settings. "
  337. "Must be less than {1}".format(index, len(servers)))
  338. url = server['url']
  339. # go through variables and replace placeholders
  340. for variable_name, variable in server.get('variables', {}).items():
  341. used_value = variables.get(
  342. variable_name, variable['default_value'])
  343. if 'enum_values' in variable \
  344. and used_value not in variable['enum_values']:
  345. raise ValueError(
  346. "The variable `{0}` in the host URL has invalid value "
  347. "{1}. Must be {2}.".format(
  348. variable_name, variables[variable_name],
  349. variable['enum_values']))
  350. url = url.replace("{" + variable_name + "}", used_value)
  351. return url
  352. @property
  353. def host(self):
  354. """Return generated host."""
  355. return self.get_host_from_settings(self.server_index, variables=self.server_variables)
  356. @host.setter
  357. def host(self, value):
  358. """Fix base path."""
  359. self._base_path = value
  360. self.server_index = None