Skip to content

Commit bfd773a

Browse files
mdesmethashhar
authored andcommitted
Assign port, http schema automatically based on passed hostname
1 parent 7c66e94 commit bfd773a

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ $ pip install trino
2828

2929
Use the DBAPI interface to query Trino:
3030

31+
if `host` is a valid url, the port and http schema will be automatically determined. For example `https://my-trino-server:9999` will assign the `http_schema` property to `https` and port to `9999`.
32+
3133
```python
3234
from trino.dbapi import connect
3335

tests/unit/test_dbapi.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
)
3030
from trino import constants
3131
from trino.auth import OAuth2Authentication
32-
from trino.dbapi import connect
32+
from trino.dbapi import Connection, connect
3333

3434

3535
@patch("trino.dbapi.trino.client")
@@ -272,3 +272,40 @@ def test_role_is_set_when_specified(mock_client):
272272

273273
_, passed_role = mock_client.ClientSession.call_args
274274
assert passed_role["roles"] == roles
275+
276+
277+
def test_hostname_parsing():
278+
https_server_with_port = Connection("https://mytrinoserver.domain:9999")
279+
assert https_server_with_port.host == "mytrinoserver.domain"
280+
assert https_server_with_port.port == 9999
281+
assert https_server_with_port.http_scheme == constants.HTTPS
282+
283+
https_server_without_port = Connection("https://mytrinoserver.domain")
284+
assert https_server_without_port.host == "mytrinoserver.domain"
285+
assert https_server_without_port.port == 8080
286+
assert https_server_without_port.http_scheme == constants.HTTPS
287+
288+
http_server_with_port = Connection("http://mytrinoserver.domain:9999")
289+
assert http_server_with_port.host == "mytrinoserver.domain"
290+
assert http_server_with_port.port == 9999
291+
assert http_server_with_port.http_scheme == constants.HTTP
292+
293+
http_server_without_port = Connection("http://mytrinoserver.domain")
294+
assert http_server_without_port.host == "mytrinoserver.domain"
295+
assert http_server_without_port.port == 8080
296+
assert http_server_without_port.http_scheme == constants.HTTP
297+
298+
http_server_with_path = Connection("http://mytrinoserver.domain/some_path")
299+
assert http_server_with_path.host == "mytrinoserver.domain/some_path"
300+
assert http_server_with_path.port == 8080
301+
assert http_server_with_path.http_scheme == constants.HTTP
302+
303+
only_hostname = Connection("mytrinoserver.domain")
304+
assert only_hostname.host == "mytrinoserver.domain"
305+
assert only_hostname.port == 8080
306+
assert only_hostname.http_scheme == constants.HTTP
307+
308+
only_hostname_with_path = Connection("mytrinoserver.domain/some_path")
309+
assert only_hostname_with_path.host == "mytrinoserver.domain/some_path"
310+
assert only_hostname_with_path.port == 8080
311+
assert only_hostname_with_path.http_scheme == constants.HTTP

trino/dbapi.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import uuid
2424
from decimal import Decimal
2525
from typing import Any, Dict, List, NamedTuple, Optional # NOQA for mypy types
26+
from urllib.parse import urlparse
2627

2728
import trino.client
2829
import trino.exceptions
@@ -92,7 +93,7 @@ class Connection(object):
9293

9394
def __init__(
9495
self,
95-
host,
96+
host: str,
9697
port=constants.DEFAULT_PORT,
9798
user=None,
9899
source=constants.DEFAULT_SOURCE,
@@ -114,8 +115,11 @@ def __init__(
114115
roles=None,
115116
timezone=None,
116117
):
117-
self.host = host
118-
self.port = port
118+
# Automatically assign http_schema, port based on hostname
119+
parsed_host = urlparse(host, allow_fragments=False)
120+
121+
self.host = host if parsed_host.hostname is None else parsed_host.hostname + parsed_host.path
122+
self.port = port if parsed_host.port is None else parsed_host.port
119123
self.user = user
120124
self.source = source
121125
self.catalog = catalog
@@ -141,7 +145,7 @@ def __init__(
141145
else:
142146
self._http_session = http_session
143147
self.http_headers = http_headers
144-
self.http_scheme = http_scheme
148+
self.http_scheme = http_scheme if not parsed_host.scheme else parsed_host.scheme
145149
self.auth = auth
146150
self.extra_credential = extra_credential
147151
self.redirect_handler = redirect_handler

0 commit comments

Comments
 (0)