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

add Trino Python Client version to user agent #411

Merged
merged 1 commit into from
Sep 20, 2023
Merged
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
6 changes: 4 additions & 2 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
_get_token_requests,
_post_statement_requests,
)
from trino import constants
from trino import __version__, constants
from trino.auth import KerberosAuthentication, _OAuth2TokenBearer
from trino.client import (
ClientSession,
Expand Down Expand Up @@ -125,6 +125,7 @@ def assert_headers(headers):
assert headers[constants.HEADER_SOURCE] == source
assert headers[constants.HEADER_USER] == user
assert headers[constants.HEADER_SESSION] == ""
assert headers[constants.HEADER_TRANSACTION] is None
assert headers[constants.HEADER_TIMEZONE] == timezone
assert headers[constants.HEADER_CLIENT_CAPABILITIES] == "PARAMETRIC_DATETIME"
assert headers[accept_encoding_header] == accept_encoding_value
Expand All @@ -135,7 +136,8 @@ def assert_headers(headers):
"catalog1=NONE,"
"catalog2=" + urllib.parse.quote("ROLE{catalog2_role}")
)
assert len(headers.keys()) == 11
assert headers["User-Agent"] == f"{constants.CLIENT_NAME}/{__version__}"
assert len(headers.keys()) == 12

req.post("URL")
_, post_kwargs = post.call_args
Expand Down
4 changes: 3 additions & 1 deletion trino/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

import trino.logging
from trino import constants, exceptions
from trino._version import __version__

__all__ = ["ClientSession", "TrinoQuery", "TrinoRequest", "PROXIES"]

Expand Down Expand Up @@ -447,14 +448,15 @@ def transaction_id(self, value):

@property
def http_headers(self) -> Dict[str, str]:
headers = {}
headers = requests.structures.CaseInsensitiveDict()

headers[constants.HEADER_CATALOG] = self._client_session.catalog
headers[constants.HEADER_SCHEMA] = self._client_session.schema
headers[constants.HEADER_SOURCE] = self._client_session.source
headers[constants.HEADER_USER] = self._client_session.user
headers[constants.HEADER_TIMEZONE] = self._client_session.timezone
headers[constants.HEADER_CLIENT_CAPABILITIES] = 'PARAMETRIC_DATETIME'
headers["user-agent"] = f"{constants.CLIENT_NAME}/{__version__}"
if len(self._client_session.roles.values()):
headers[constants.HEADER_ROLE] = ",".join(
# ``name`` must not contain ``=``
Expand Down
2 changes: 2 additions & 0 deletions trino/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

URL_STATEMENT_PATH = "/v1/statement"

CLIENT_NAME = "Trino Python Client"

HEADER_CATALOG = "X-Trino-Catalog"
HEADER_SCHEMA = "X-Trino-Schema"
HEADER_SOURCE = "X-Trino-Source"
Expand Down