Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support proxy per-connection rather than just environment variables #320

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/databricks/sql/auth/thrift_http_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import logging
import urllib.parse
from typing import Dict, Union
from typing import Dict, Optional, Union

import six
import thrift
Expand Down Expand Up @@ -31,6 +31,7 @@ def __init__(
ssl_context=None,
max_connections: int = 1,
retry_policy: Union[DatabricksRetryPolicy, int] = 0,
proxies: Optional[Dict[str, str]] = None,
):
if port is not None:
warnings.warn(
Expand Down Expand Up @@ -60,8 +61,11 @@ def __init__(
self.path = parsed.path
if parsed.query:
self.path += "?%s" % parsed.query

if proxies is None:
proxies = urllib.request.getproxies()
try:
proxy = urllib.request.getproxies()[self.scheme]
proxy = proxies[self.scheme]
except KeyError:
proxy = None
else:
Expand Down
17 changes: 14 additions & 3 deletions src/databricks/sql/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def read(self) -> Optional[OAuthToken]:
STRUCT is returned as Dict[str, Any]
ARRAY is returned as numpy.ndarray
When False, complex types are returned as a strings. These are generally deserializable as JSON.
:param proxies: An optional dictionary mapping protocol to the URL of the proxy.
"""

# Internal arguments in **kwargs:
Expand Down Expand Up @@ -206,6 +207,7 @@ def read(self) -> Optional[OAuthToken]:
self.port = kwargs.get("_port", 443)
self.disable_pandas = kwargs.get("_disable_pandas", False)
self.lz4_compression = kwargs.get("enable_query_result_lz4_compression", True)
self.proxies = kwargs.get("proxies")

auth_provider = get_python_sql_connector_auth_provider(
server_hostname, **kwargs
Expand Down Expand Up @@ -651,7 +653,12 @@ def _handle_staging_put(
raise Error("Cannot perform PUT without specifying a local_file")

with open(local_file, "rb") as fh:
r = requests.put(url=presigned_url, data=fh, headers=headers)
r = requests.put(
url=presigned_url,
data=fh,
headers=headers,
proxies=self.connection.proxies,
)

# fmt: off
# Design borrowed from: https://stackoverflow.com/a/2342589/5093960
Expand Down Expand Up @@ -685,7 +692,9 @@ def _handle_staging_get(
if local_file is None:
raise Error("Cannot perform GET without specifying a local_file")

r = requests.get(url=presigned_url, headers=headers)
r = requests.get(
url=presigned_url, headers=headers, proxies=self.connection.proxies
)

# response.ok verifies the status code is not between 400-600.
# Any 2xx or 3xx will evaluate r.ok == True
Expand All @@ -700,7 +709,9 @@ def _handle_staging_get(
def _handle_staging_remove(self, presigned_url: str, headers: dict = None):
"""Make an HTTP DELETE request to the presigned_url"""

r = requests.delete(url=presigned_url, headers=headers)
r = requests.delete(
url=presigned_url, headers=headers, proxies=self.connection.proxies
)

if not r.ok:
raise Error(
Expand Down
5 changes: 4 additions & 1 deletion src/databricks/sql/thrift_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import uuid
import threading
from ssl import CERT_NONE, CERT_REQUIRED, create_default_context
from typing import List, Union
from typing import List, Union, Optional, Dict

import pyarrow
import thrift.transport.THttpClient
Expand Down Expand Up @@ -220,6 +220,9 @@ def __init__(

additional_transport_args["retry_policy"] = self.retry_policy

if "proxies" in kwargs:
additional_transport_args["proxies"] = kwargs["proxies"]

self._transport = databricks.sql.auth.thrift_http_client.THttpClient(
auth_provider=self._auth_provider,
uri_or_host=uri,
Expand Down
Loading