Skip to content

Commit 5791ac3

Browse files
Parse port from base url (#2133)
* Parse port from base url Signed-off-by: osamaahmed17 <[email protected]> * Parse port from base url Signed-off-by: osamaahmed17 <[email protected]> * linting errors solved Signed-off-by: osamaahmed17 <[email protected]> * Update clients/python/src/model_registry/core.py Co-authored-by: Jon Burdo <[email protected]> Signed-off-by: Osama Tahir <[email protected]> * added test case Signed-off-by: osamaahmed17 <[email protected]> * solve linting error Signed-off-by: osamaahmed17 <[email protected]> --------- Signed-off-by: osamaahmed17 <[email protected]> Signed-off-by: Osama Tahir <[email protected]> Co-authored-by: Jon Burdo <[email protected]>
1 parent 28ac0bd commit 5791ac3

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

clients/python/src/model_registry/core.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from contextlib import asynccontextmanager
77
from dataclasses import dataclass
88
from typing import TypeVar, cast, overload
9+
from urllib.parse import urlparse
910

1011
from mr_openapi import (
1112
ApiClient,
@@ -56,9 +57,12 @@ def secure_connection(
5657
user_token: The PEM-encoded user token as a string.
5758
custom_ca: The path to a PEM-
5859
"""
60+
parsed_url = urlparse(server_address)
61+
host = server_address if parsed_url.port else f"{server_address}:{port}"
62+
5963
return cls(
6064
Configuration(
61-
f"{server_address}:{port}",
65+
host=host,
6266
access_token=user_token,
6367
ssl_ca_cert=custom_ca,
6468
)
@@ -78,8 +82,11 @@ def insecure_connection(
7882
port: Server port.
7983
user_token: The PEM-encoded user token as a string.
8084
"""
85+
parsed_url = urlparse(server_address)
86+
host = server_address if parsed_url.port else f"{server_address}:{port}"
87+
8188
config = Configuration(
82-
host=f"{server_address}:{port}",
89+
host=host,
8390
access_token=user_token,
8491
verify_ssl=False,
8592
)

clients/python/tests/test_client.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,8 +1178,7 @@ def test_user_token_from_envvar(monkeypatch, mock_get_registered_models):
11781178

11791179
monkeypatch.setenv("KF_PIPELINES_SA_TOKEN_PATH", token_file_path)
11801180
client = ModelRegistry(
1181-
server_address="http://localhost",
1182-
port=8080,
1181+
server_address="http://localhost:8080",
11831182
author="test_author",
11841183
is_secure=False,
11851184
# user_token=None -> ... Let it read from Env var
@@ -1201,8 +1200,7 @@ def test_user_token_from_k8s_file(monkeypatch, mock_get_registered_models):
12011200
k8s_token_file_path = k8s_token_file.name
12021201
monkeypatch.setattr("model_registry._client.DEFAULT_K8S_SA_TOKEN_PATH", k8s_token_file_path)
12031202
client = ModelRegistry(
1204-
server_address="http://localhost",
1205-
port=8080,
1203+
server_address="http://localhost:8080",
12061204
author="test_author",
12071205
is_secure=False,
12081206
# user_token=None -> ... Let it read from K8s file
@@ -1228,8 +1226,7 @@ def test_user_token_envvar_priority_over_k8s(monkeypatch, mock_get_registered_mo
12281226
k8s_token_file_path = k8s_token_file.name
12291227
monkeypatch.setattr("model_registry._client.DEFAULT_K8S_SA_TOKEN_PATH", k8s_token_file_path)
12301228
client = ModelRegistry(
1231-
server_address="http://localhost",
1232-
port=8080,
1229+
server_address="http://localhost:8080",
12331230
author="test_author",
12341231
is_secure=False,
12351232
# user_token=None -> ... Let it read from Env var (and not from K8s file, given that Env var is set)
@@ -1248,8 +1245,7 @@ def test_user_token_missing_warning(monkeypatch, mock_get_registered_models):
12481245
mock_warn = MagicMock()
12491246
monkeypatch.setattr("model_registry._client.warn", mock_warn)
12501247
client = ModelRegistry(
1251-
server_address="http://localhost",
1252-
port=8080,
1248+
server_address="http://localhost:8080",
12531249
author="test_author",
12541250
is_secure=False,
12551251
# user_token=None -> ... Let it try to read, but missing Env var and K8s file, it will emit a warning
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from model_registry.core import ModelRegistryAPIClient
2+
3+
4+
def test_connection_args():
5+
"""Test connection arguments parsing."""
6+
# old-style: explicit port
7+
server_address = "http://localhost"
8+
port = 8080
9+
host = f"{server_address}:{port}"
10+
11+
client = ModelRegistryAPIClient.insecure_connection(server_address, port)
12+
assert client.config.host == host
13+
14+
# new-style: port in url
15+
server_address_with_port = "http://localhost:9090"
16+
client = ModelRegistryAPIClient.insecure_connection(server_address_with_port, port)
17+
assert client.config.host == server_address_with_port
18+
19+
# Secure connection tests
20+
client = ModelRegistryAPIClient.secure_connection(
21+
server_address, port, user_token="token" # noqa: S106
22+
)
23+
assert client.config.host == host
24+
25+
client = ModelRegistryAPIClient.secure_connection(
26+
server_address_with_port, port, user_token="token" # noqa: S106
27+
)
28+
assert client.config.host == server_address_with_port

0 commit comments

Comments
 (0)