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 for custom host in ApiClient #576

Open
wants to merge 1 commit 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
9 changes: 7 additions & 2 deletions databricks_cli/sdk/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ def __init__(self, user=None, password=None, host=None, token=None,
self.session = requests.Session()
self.session.auth = FallbackNetrcAuth()
self.session.mount('https://', TlsV1HttpAdapter(max_retries=retries))

parsed_url = urlparse(host)
scheme = parsed_url.scheme
hostname = parsed_url.hostname
# this is to allow enterprise users to specify a custom host if they are forced to interact with Databricks via a proxy
if "databricks.com" in host:
hostname = parsed_url.hostname
else:
hostname = parsed_url.hostname + parsed_url.path

self.url = "%s://%s/api/" % (scheme, hostname)
if user is not None and password is not None:
encoded_auth = (user + ":" + password).encode()
Expand Down
4 changes: 4 additions & 0 deletions tests/sdk/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def test_api_client_url_parsing():
client = ApiClient(host='https://databricks.com?o=123')
assert client.get_url('') == 'https://databricks.com/api/2.0'

# this is to test that users can set a custom host for the apiclient if they are forced to interact with Databricks via a proxy
client = ApiClient(host='https://mydatabricksproxyservice.com/databricks/proxy')
assert client.get_url('') == 'https://mydatabricksproxyservice.com/databricks/proxy/api/2.0'

# NOTE: this technically is not possible since we validate that the "host" has a prefix of https:// in
# databricks_cli.configure.cli
client = ApiClient(host='http://databricks.com')
Expand Down