diff --git a/README.md b/README.md index 4d476dc..41e69eb 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,9 @@ pip install netboxlabs-diode-sdk ### Example -* `target` should be the address of the Diode service, e.g. `grpc://localhost:8080/diode` for insecure connection - or `grpcs://example.com` for secure connection. +* `target` should be the address of the Diode service. + * Insecure connections: `grpc://localhost:8080/diode` or `http://localhost:8080/diode` + * Secure connections: `grpcs://example.com` or `https://example.com` ```python from netboxlabs.diode.sdk import DiodeClient diff --git a/netboxlabs/diode/sdk/client.py b/netboxlabs/diode/sdk/client.py index 85f45b8..045ab0d 100644 --- a/netboxlabs/diode/sdk/client.py +++ b/netboxlabs/diode/sdk/client.py @@ -63,15 +63,18 @@ def parse_target(target: str) -> tuple[str, str, bool]: """Parse the target into authority, path and tls_verify.""" parsed_target = urlparse(target) - if parsed_target.scheme not in ["grpc", "grpcs"]: - raise ValueError("target should start with grpc:// or grpcs://") + if parsed_target.scheme not in ["grpc", "grpcs", "http", "https"]: + raise ValueError("target should start with grpc://, grpcs://, http:// or https://") - tls_verify = parsed_target.scheme == "grpcs" + tls_verify = parsed_target.scheme in ["grpcs", "https"] authority = parsed_target.netloc if ":" not in authority: - authority += ":443" + if parsed_target.scheme in ["grpc", "http"]: + authority += ":80" + elif parsed_target.scheme in ["grpcs", "https"]: + authority += ":443" return authority, parsed_target.path, tls_verify diff --git a/tests/test_client.py b/tests/test_client.py index d2178d4..a265ddc 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -104,17 +104,10 @@ def test_load_certs_returns_bytes(): assert isinstance(_load_certs(), bytes) -def test_parse_target_handles_http_prefix(): - """Check that parse_target raises an error when the target contains http://.""" +def test_parse_target_handles_ftp_prefix(): + """Check that parse_target raises an error when the target contains ftp://.""" with pytest.raises(ValueError): - parse_target("http://localhost:8081") - - -def test_parse_target_handles_https_prefix(): - """Check that parse_target raises an error when the target contains https://.""" - with pytest.raises(ValueError): - parse_target("https://localhost:8081") - + parse_target("ftp://localhost:8081") def test_parse_target_parses_authority_correctly(): """Check that parse_target parses the authority correctly.""" @@ -127,6 +120,12 @@ def test_parse_target_parses_authority_correctly(): def test_parse_target_adds_default_port_if_missing(): """Check that parse_target adds the default port if missing.""" authority, _, _ = parse_target("grpc://localhost") + assert authority == "localhost:80" + authority, _, _ = parse_target("http://localhost") + assert authority == "localhost:80" + authority, _, _ = parse_target("grpcs://localhost") + assert authority == "localhost:443" + authority, _, _ = parse_target("https://localhost") assert authority == "localhost:443" @@ -144,8 +143,14 @@ def test_parse_target_handles_no_path(): def test_parse_target_parses_tls_verify_correctly(): """Check that parse_target parses tls_verify correctly.""" + _, _, tls_verify = parse_target("grpc://localhost:8081") + assert tls_verify is False + _, _, tls_verify = parse_target("http://localhost:8081") + assert tls_verify is False _, _, tls_verify = parse_target("grpcs://localhost:8081") assert tls_verify is True + _, _, tls_verify = parse_target("https://localhost:8081") + assert tls_verify is True def test_get_sentry_dsn_returns_env_var_when_no_input():