3131 transform_paramstyle ,
3232 ColumnTable ,
3333 ColumnQueue ,
34+ build_client_context ,
3435)
3536from databricks .sql .parameters .native import (
3637 DbsqlParameterBase ,
5253
5354from databricks .sql .auth .common import ClientContext
5455from databricks .sql .common .unified_http_client import UnifiedHttpClient
56+ from databricks .sql .common .http import HttpMethod
5557
5658from databricks .sql .thrift_api .TCLIService .ttypes import (
5759 TOpenSessionResp ,
@@ -254,14 +256,14 @@ def read(self) -> Optional[OAuthToken]:
254256 "telemetry_batch_size" , TelemetryClientFactory .DEFAULT_BATCH_SIZE
255257 )
256258
257- client_context = self . _build_client_context (server_hostname , ** kwargs )
258- http_client = UnifiedHttpClient (client_context )
259+ client_context = build_client_context (server_hostname , __version__ , ** kwargs )
260+ self . http_client = UnifiedHttpClient (client_context )
259261
260262 try :
261263 self .session = Session (
262264 server_hostname ,
263265 http_path ,
264- http_client ,
266+ self . http_client ,
265267 http_headers ,
266268 session_configuration ,
267269 catalog ,
@@ -350,50 +352,6 @@ def _set_use_inline_params_with_warning(self, value: Union[bool, str]):
350352
351353 return value
352354
353- def _build_client_context (self , server_hostname : str , ** kwargs ):
354- """Build ClientContext for HTTP client configuration."""
355- from databricks .sql .auth .common import ClientContext
356- from databricks .sql .types import SSLOptions
357-
358- # Extract SSL options
359- ssl_options = SSLOptions (
360- tls_verify = not kwargs .get ("_tls_no_verify" , False ),
361- tls_verify_hostname = kwargs .get ("_tls_verify_hostname" , True ),
362- tls_trusted_ca_file = kwargs .get ("_tls_trusted_ca_file" ),
363- tls_client_cert_file = kwargs .get ("_tls_client_cert_file" ),
364- tls_client_cert_key_file = kwargs .get ("_tls_client_cert_key_file" ),
365- tls_client_cert_key_password = kwargs .get ("_tls_client_cert_key_password" ),
366- )
367-
368- # Build user agent
369- user_agent_entry = kwargs .get ("user_agent_entry" , "" )
370- if user_agent_entry :
371- user_agent = f"PyDatabricksSqlConnector/{ __version__ } ({ user_agent_entry } )"
372- else :
373- user_agent = f"PyDatabricksSqlConnector/{ __version__ } "
374-
375- return ClientContext (
376- hostname = server_hostname ,
377- ssl_options = ssl_options ,
378- socket_timeout = kwargs .get ("_socket_timeout" ),
379- retry_stop_after_attempts_count = kwargs .get (
380- "_retry_stop_after_attempts_count"
381- ),
382- retry_delay_min = kwargs .get ("_retry_delay_min" ),
383- retry_delay_max = kwargs .get ("_retry_delay_max" ),
384- retry_stop_after_attempts_duration = kwargs .get (
385- "_retry_stop_after_attempts_duration"
386- ),
387- retry_delay_default = kwargs .get ("_retry_delay_default" ),
388- retry_dangerous_codes = kwargs .get ("_retry_dangerous_codes" ),
389- http_proxy = kwargs .get ("_http_proxy" ),
390- proxy_username = kwargs .get ("_proxy_username" ),
391- proxy_password = kwargs .get ("_proxy_password" ),
392- pool_connections = kwargs .get ("_pool_connections" ),
393- pool_maxsize = kwargs .get ("_pool_maxsize" ),
394- user_agent = user_agent ,
395- )
396-
397355 # The ideal return type for this method is perhaps Self, but that was not added until 3.11, and we support pre-3.11 pythons, currently.
398356 def __enter__ (self ) -> "Connection" :
399357 return self
@@ -447,7 +405,7 @@ def get_protocol_version(openSessionResp: TOpenSessionResp):
447405 @property
448406 def open (self ) -> bool :
449407 """Return whether the connection is open by checking if the session is open."""
450- return hasattr ( self , "session" ) and self .session .is_open
408+ return self .session .is_open
451409
452410 def cursor (
453411 self ,
@@ -497,6 +455,10 @@ def _close(self, close_cursors=True) -> None:
497455
498456 TelemetryClientFactory .close (self .get_session_id_hex ())
499457
458+ # Close HTTP client that was created by this connection
459+ if self .http_client :
460+ self .http_client .close ()
461+
500462 def commit (self ):
501463 """No-op because Databricks does not support transactions"""
502464 pass
@@ -796,8 +758,8 @@ def _handle_staging_put(
796758 )
797759
798760 with open (local_file , "rb" ) as fh :
799- r = self .connection .session . http_client .request (
800- " PUT" , presigned_url , body = fh .read (), headers = headers
761+ r = self .connection .http_client .request (
762+ HttpMethod . PUT , presigned_url , body = fh .read (), headers = headers
801763 )
802764
803765 # fmt: off
@@ -837,8 +799,8 @@ def _handle_staging_get(
837799 session_id_hex = self .connection .get_session_id_hex (),
838800 )
839801
840- r = self .connection .session . http_client .request (
841- " GET" , presigned_url , headers = headers
802+ r = self .connection .http_client .request (
803+ HttpMethod . GET , presigned_url , headers = headers
842804 )
843805
844806 # response.ok verifies the status code is not between 400-600.
@@ -860,8 +822,8 @@ def _handle_staging_remove(
860822 ):
861823 """Make an HTTP DELETE request to the presigned_url"""
862824
863- r = self .connection .session . http_client .request (
864- " DELETE" , presigned_url , headers = headers
825+ r = self .connection .http_client .request (
826+ HttpMethod . DELETE , presigned_url , headers = headers
865827 )
866828
867829 if r .status >= 400 :
0 commit comments