__init__.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from inspect import signature
  2. from typing import Optional, Union, Dict, Any
  3. from urllib.parse import urlparse, parse_qs
  4. import clickhouse_connect.driver.ctypes
  5. from clickhouse_connect.driver.client import Client
  6. from clickhouse_connect.driver.common import dict_copy
  7. from clickhouse_connect.driver.exceptions import ProgrammingError
  8. from clickhouse_connect.driver.httpclient import HttpClient
  9. # pylint: disable=too-many-arguments,too-many-locals,too-many-branches
  10. def create_client(*,
  11. host: str = None,
  12. username: str = None,
  13. password: str = '',
  14. database: str = '__default__',
  15. interface: Optional[str] = None,
  16. port: int = 0,
  17. secure: Union[bool, str] = False,
  18. dsn: Optional[str] = None,
  19. settings: Optional[Dict[str, Any]] = None,
  20. generic_args: Optional[Dict[str, Any]] = None,
  21. **kwargs) -> Client:
  22. """
  23. The preferred method to get a ClickHouse Connect Client instance
  24. :param host: The hostname or IP address of the ClickHouse server. If not set, localhost will be used.
  25. :param username: The ClickHouse username. If not set, the default ClickHouse user will be used.
  26. :param password: The password for username.
  27. :param database: The default database for the connection. If not set, ClickHouse Connect will use the
  28. default database for username.
  29. :param interface: Must be http or https. Defaults to http, or to https if port is set to 8443 or 443
  30. :param port: The ClickHouse HTTP or HTTPS port. If not set will default to 8123, or to 8443 if secure=True
  31. or interface=https.
  32. :param secure: Use https/TLS. This overrides inferred values from the interface or port arguments.
  33. :param dsn: A string in standard DSN (Data Source Name) format. Other connection values (such as host or user)
  34. will be extracted from this string if not set otherwise.
  35. :param settings: ClickHouse server settings to be used with the session/every request
  36. :param generic_args: Used internally to parse DBAPI connection strings into keyword arguments and ClickHouse settings.
  37. It is not recommended to use this parameter externally.
  38. :param kwargs -- Recognized keyword arguments (used by the HTTP client), see below
  39. :param compress: Enable compression for ClickHouse HTTP inserts and query results. True will select the preferred
  40. compression method (lz4). A str of 'lz4', 'zstd', 'brotli', or 'gzip' can be used to use a specific compression type
  41. :param query_limit: Default LIMIT on returned rows. 0 means no limit
  42. :param connect_timeout: Timeout in seconds for the http connection
  43. :param send_receive_timeout: Read timeout in seconds for http connection
  44. :param client_name: client_name prepended to the HTTP User Agent header. Set this to track client queries
  45. in the ClickHouse system.query_log.
  46. :param send_progress: Deprecated, has no effect. Previous functionality is now automatically determined
  47. :param verify: Verify the server certificate in secure/https mode
  48. :param ca_cert: If verify is True, the file path to Certificate Authority root to validate ClickHouse server
  49. certificate, in .pem format. Ignored if verify is False. This is not necessary if the ClickHouse server
  50. certificate is trusted by the operating system. To trust the maintained list of "global" public root
  51. certificates maintained by the Python 'certifi' package, set ca_cert to 'certifi'
  52. :param client_cert: File path to a TLS Client certificate in .pem format. This file should contain any
  53. applicable intermediate certificates
  54. :param client_cert_key: File path to the private key for the Client Certificate. Required if the private key
  55. is not included the Client Certificate key file
  56. :param session_id ClickHouse session id. If not specified and the common setting 'autogenerate_session_id'
  57. is True, the client will generate a UUID1 session id
  58. :param pool_mgr Optional urllib3 PoolManager for this client. Useful for creating separate connection
  59. pools for multiple client endpoints for applications with many clients
  60. :param http_proxy http proxy address. Equivalent to setting the HTTP_PROXY environment variable
  61. :param https_proxy https proxy address. Equivalent to setting the HTTPS_PROXY environment variable
  62. :param server_host_name This is the server host name that will be checked against a TLS certificate for
  63. validity. This option can be used if using an ssh_tunnel or other indirect means to an ClickHouse server
  64. where the `host` argument refers to the tunnel or proxy and not the actual ClickHouse server
  65. :return: ClickHouse Connect Client instance
  66. """
  67. if dsn:
  68. parsed = urlparse(dsn)
  69. username = username or parsed.username
  70. password = password or parsed.password
  71. host = host or parsed.hostname
  72. port = port or parsed.port
  73. if parsed.path and (not database or database == '__default__'):
  74. database = parsed.path[1:].split('/')[0]
  75. database = database or parsed.path
  76. kwargs.update(dict(parse_qs(parsed.query)))
  77. use_tls = str(secure).lower() == 'true' or interface == 'https' or (not interface and port in (443, 8443))
  78. if not host:
  79. host = 'localhost'
  80. if not interface:
  81. interface = 'https' if use_tls else 'http'
  82. port = port or default_port(interface, use_tls)
  83. if username is None and 'user' in kwargs:
  84. username = kwargs.pop('user')
  85. if username is None and 'user_name' in kwargs:
  86. username = kwargs.pop('user_name')
  87. if password and username is None:
  88. username = 'default'
  89. if 'compression' in kwargs and 'compress' not in kwargs:
  90. kwargs['compress'] = kwargs.pop('compression')
  91. settings = settings or {}
  92. if interface.startswith('http'):
  93. if generic_args:
  94. client_params = signature(HttpClient).parameters
  95. for name, value in generic_args.items():
  96. if name in client_params:
  97. kwargs[name] = value
  98. elif name == 'compression':
  99. if 'compress' not in kwargs:
  100. kwargs['compress'] = value
  101. else:
  102. if name.startswith('ch_'):
  103. name = name[3:]
  104. settings[name] = value
  105. return HttpClient(interface, host, port, username, password, database, settings=settings, **kwargs)
  106. raise ProgrammingError(f'Unrecognized client type {interface}')
  107. def default_port(interface: str, secure: bool):
  108. if interface.startswith('http'):
  109. return 8443 if secure else 8123
  110. raise ValueError('Unrecognized ClickHouse interface')