ethanlin01x commented on code in PR #3992:
URL: https://github.com/apache/iggy/pull/3992#discussion_r3888984262


##########
examples/python/http/producer.py:
##########
@@ -0,0 +1,166 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import argparse
+import asyncio
+import typing
+import urllib.parse
+
+from apache_iggy import HttpConfig, IggyClient, StreamDetails, TopicDetails
+from apache_iggy import SendMessage as Message
+from loguru import logger
+
+STREAM_NAME = "sample-stream"
+TOPIC_NAME = "sample-topic"
+STREAM_ID = 0
+TOPIC_ID = 0
+PARTITION_ID = 0
+BATCHES_LIMIT = 5
+
+
+class ArgNamespace(typing.NamedTuple):
+    api_url: str
+    retries: int
+
+
+class ValidateUrl(argparse.Action):
+    def __call__(
+        self,
+        parser: argparse.ArgumentParser,
+        namespace: argparse.Namespace,
+        values: str,
+        _option_string: str | None = None,
+    ):
+        parsed_url: urllib.parse.ParseResult = urllib.parse.urlparse(values)
+        if parsed_url.scheme not in ("http", "https") or parsed_url.netloc == 
"":
+            parser.error(f"Invalid API URL: {values}")
+        setattr(namespace, self.dest, values)
+
+
+def parse_args() -> ArgNamespace:
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        "--api-url",
+        help="Iggy HTTP API URL",
+        action=ValidateUrl,
+        default="http://127.0.0.1:3000";,
+    )
+    parser.add_argument(
+        "--retries",
+        type=int,
+        default=3,
+        help="Number of retries to perform on transient errors",
+    )
+    args = parser.parse_args()
+    return ArgNamespace(**vars(args))
+
+
+def build_config(args: ArgNamespace) -> HttpConfig:
+    """Build an HTTP client configuration."""
+
+    return HttpConfig(
+        api_url=args.api_url,
+        retries=args.retries,
+    )
+
+
+async def main():
+    args: ArgNamespace = parse_args()
+    try:
+        config = build_config(args)
+    except ValueError as error:
+        logger.error(f"Invalid client configuration: {error}")
+        return
+    logger.info(f"Connecting to {args.api_url}")
+
+    client = IggyClient.http(config)
+    logger.info("Connecting to IggyClient")
+    await client.connect()
+    logger.info("Connected.")
+    # HTTP is a stateless transport: log in explicitly rather than relying

Review Comment:
   Nit: "stateless" is loose here - `HttpClient` does hold a JWT and swaps it 
on `/users/refresh-token`. It explains the missing reconnection policy fine, 
but not the missing auto-login. The second half of the sentence already gives 
the real reason, so dropping the first clause would be enough.  Same in 
`consumer.py` and the README.



##########
foreign/python/tests/test_http_config.py:
##########
@@ -0,0 +1,145 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Tests for the HTTP client configuration surface.
+
+`HttpConfig` mirrors the Rust SDK's `HttpClientConfig` the same way
+`TcpConfig`/`QuicConfig` do, so most of these assert that a value set from
+Python survives to the getters and that unset fields fall back to the Rust
+defaults. HTTP is stateless per-request, so unlike TCP/QUIC there is no
+`AutoLogin` or reconnection policy to configure.
+"""
+
+import ast
+from datetime import timedelta
+
+import pytest
+
+from apache_iggy import HttpConfig, IggyClient
+
+from .utils import get_http_server_config, wait_for_ping
+
+
[email protected]
+class TestHttpConfig:
+    """Test the transport configuration."""
+
+    def test_defaults_match_the_rust_sdk(self):
+        """Test that an unconfigured transport matches the Rust SDK 
defaults."""
+        config = HttpConfig()
+
+        assert config.api_url == "http://127.0.0.1:3000";
+        assert config.retries == 3
+        assert config.has_jwt is False

Review Comment:
   Also assert `heartbeat_interval`



##########
foreign/python/tests/test_http_config.py:
##########
@@ -0,0 +1,145 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Tests for the HTTP client configuration surface.
+
+`HttpConfig` mirrors the Rust SDK's `HttpClientConfig` the same way
+`TcpConfig`/`QuicConfig` do, so most of these assert that a value set from
+Python survives to the getters and that unset fields fall back to the Rust
+defaults. HTTP is stateless per-request, so unlike TCP/QUIC there is no
+`AutoLogin` or reconnection policy to configure.
+"""
+
+import ast
+from datetime import timedelta
+
+import pytest
+
+from apache_iggy import HttpConfig, IggyClient
+
+from .utils import get_http_server_config, wait_for_ping
+
+
[email protected]
+class TestHttpConfig:
+    """Test the transport configuration."""
+
+    def test_defaults_match_the_rust_sdk(self):
+        """Test that an unconfigured transport matches the Rust SDK 
defaults."""
+        config = HttpConfig()
+
+        assert config.api_url == "http://127.0.0.1:3000";
+        assert config.retries == 3
+        assert config.has_jwt is False
+
+    def test_every_field_round_trips(self):
+        """Test that each configured field is readable back unchanged."""
+        config = HttpConfig(
+            api_url="http://127.0.0.1:3001";,
+            retries=5,
+            jwt="a-token",
+        )
+
+        assert config.api_url == "http://127.0.0.1:3001";
+        assert config.retries == 5
+        assert config.has_jwt is True

Review Comment:
   Also assert `heartbeat_interval`



##########
foreign/python/tests/test_http_config.py:
##########
@@ -0,0 +1,145 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Tests for the HTTP client configuration surface.
+
+`HttpConfig` mirrors the Rust SDK's `HttpClientConfig` the same way
+`TcpConfig`/`QuicConfig` do, so most of these assert that a value set from
+Python survives to the getters and that unset fields fall back to the Rust
+defaults. HTTP is stateless per-request, so unlike TCP/QUIC there is no
+`AutoLogin` or reconnection policy to configure.
+"""
+
+import ast
+from datetime import timedelta
+
+import pytest
+
+from apache_iggy import HttpConfig, IggyClient
+
+from .utils import get_http_server_config, wait_for_ping
+
+
[email protected]
+class TestHttpConfig:
+    """Test the transport configuration."""
+
+    def test_defaults_match_the_rust_sdk(self):
+        """Test that an unconfigured transport matches the Rust SDK 
defaults."""
+        config = HttpConfig()
+
+        assert config.api_url == "http://127.0.0.1:3000";
+        assert config.retries == 3
+        assert config.has_jwt is False
+
+    def test_every_field_round_trips(self):
+        """Test that each configured field is readable back unchanged."""
+        config = HttpConfig(
+            api_url="http://127.0.0.1:3001";,
+            retries=5,
+            jwt="a-token",
+        )
+
+        assert config.api_url == "http://127.0.0.1:3001";
+        assert config.retries == 5
+        assert config.has_jwt is True
+
+    def test_arguments_are_keyword_only(self):
+        """Test that the API URL cannot be passed positionally."""
+        with pytest.raises(TypeError):
+            # pyrefly: ignore  # bad-argument-count
+            HttpConfig("http://127.0.0.1:3000";)
+
+    def test_repr_hides_the_jwt(self):
+        """Test that the JWT does not leak through repr but still parses as 
Python."""
+        config = HttpConfig(jwt="a-secret-token")
+
+        printed = repr(config)
+
+        assert "a-secret-token" not in printed
+        ast.parse(printed)
+
+    def test_repr_shows_every_field_as_python(self):
+        """Test that repr covers the configured fields and parses as Python."""
+        config = HttpConfig(api_url="http://127.0.0.1:3001";, retries=5)
+
+        printed = repr(config)
+
+        assert 'api_url="http://127.0.0.1:3001";' in printed
+        assert "retries=5" in printed
+        ast.parse(printed)
+
+    @pytest.mark.parametrize(
+        "invalid_url",
+        ["", "not-a-url", "http://127.0.0.1:0";],
+    )
+    def test_invalid_api_url_is_rejected(self, invalid_url: str):
+        """Test that a malformed API URL fails at construction, not at 
connect."""
+        with pytest.raises(ValueError):
+            HttpConfig(api_url=invalid_url)
+
+    @pytest.mark.parametrize("out_of_range", [-1, 2**32])
+    def test_out_of_range_retries_is_rejected(self, out_of_range: int):
+        """Test that a retry count outside the wire range names the argument.
+
+        The conversion pyo3 does on its own raises OverflowError, which is not 
a
+        ValueError and so escapes the handler a caller wraps construction in.
+        """
+        with pytest.raises(ValueError, match="retries"):
+            HttpConfig(retries=out_of_range)
+
+    def test_negative_heartbeat_interval_is_rejected(self):
+        """Test that a negative heartbeat interval fails at construction."""
+        with pytest.raises(ValueError, match="negative"):
+            HttpConfig(heartbeat_interval=timedelta(seconds=-3))
+
+    def test_zero_heartbeat_interval_is_rejected(self):
+        """Test that a zero heartbeat interval fails at construction.
+
+        Nothing downstream reads zero as "disabled"; it heartbeats in a
+        continuous loop for as long as the client lives.
+        """
+        with pytest.raises(ValueError, match="zero"):
+            HttpConfig(heartbeat_interval=timedelta(0))
+
+
[email protected]
+class TestClientConstruction:

Review Comment:
   nit: `test_client_config.py` already has a class with this name. Please 
rename it to `TestHttpClientConstruction`.



##########
foreign/python/tests/test_http_config.py:
##########
@@ -0,0 +1,145 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+Tests for the HTTP client configuration surface.
+
+`HttpConfig` mirrors the Rust SDK's `HttpClientConfig` the same way
+`TcpConfig`/`QuicConfig` do, so most of these assert that a value set from
+Python survives to the getters and that unset fields fall back to the Rust
+defaults. HTTP is stateless per-request, so unlike TCP/QUIC there is no
+`AutoLogin` or reconnection policy to configure.
+"""
+
+import ast
+from datetime import timedelta
+
+import pytest
+
+from apache_iggy import HttpConfig, IggyClient
+
+from .utils import get_http_server_config, wait_for_ping
+
+
[email protected]
+class TestHttpConfig:
+    """Test the transport configuration."""
+
+    def test_defaults_match_the_rust_sdk(self):
+        """Test that an unconfigured transport matches the Rust SDK 
defaults."""
+        config = HttpConfig()
+
+        assert config.api_url == "http://127.0.0.1:3000";
+        assert config.retries == 3
+        assert config.has_jwt is False
+
+    def test_every_field_round_trips(self):
+        """Test that each configured field is readable back unchanged."""
+        config = HttpConfig(
+            api_url="http://127.0.0.1:3001";,
+            retries=5,
+            jwt="a-token",
+        )
+
+        assert config.api_url == "http://127.0.0.1:3001";
+        assert config.retries == 5
+        assert config.has_jwt is True
+
+    def test_arguments_are_keyword_only(self):
+        """Test that the API URL cannot be passed positionally."""
+        with pytest.raises(TypeError):
+            # pyrefly: ignore  # bad-argument-count
+            HttpConfig("http://127.0.0.1:3000";)
+
+    def test_repr_hides_the_jwt(self):
+        """Test that the JWT does not leak through repr but still parses as 
Python."""
+        config = HttpConfig(jwt="a-secret-token")
+
+        printed = repr(config)
+
+        assert "a-secret-token" not in printed
+        ast.parse(printed)
+
+    def test_repr_shows_every_field_as_python(self):
+        """Test that repr covers the configured fields and parses as Python."""
+        config = HttpConfig(api_url="http://127.0.0.1:3001";, retries=5)
+
+        printed = repr(config)
+
+        assert 'api_url="http://127.0.0.1:3001";' in printed
+        assert "retries=5" in printed
+        ast.parse(printed)
+
+    @pytest.mark.parametrize(
+        "invalid_url",
+        ["", "not-a-url", "http://127.0.0.1:0";],
+    )
+    def test_invalid_api_url_is_rejected(self, invalid_url: str):
+        """Test that a malformed API URL fails at construction, not at 
connect."""
+        with pytest.raises(ValueError):
+            HttpConfig(api_url=invalid_url)
+
+    @pytest.mark.parametrize("out_of_range", [-1, 2**32])
+    def test_out_of_range_retries_is_rejected(self, out_of_range: int):
+        """Test that a retry count outside the wire range names the argument.
+
+        The conversion pyo3 does on its own raises OverflowError, which is not 
a
+        ValueError and so escapes the handler a caller wraps construction in.
+        """
+        with pytest.raises(ValueError, match="retries"):
+            HttpConfig(retries=out_of_range)
+
+    def test_negative_heartbeat_interval_is_rejected(self):
+        """Test that a negative heartbeat interval fails at construction."""
+        with pytest.raises(ValueError, match="negative"):
+            HttpConfig(heartbeat_interval=timedelta(seconds=-3))
+
+    def test_zero_heartbeat_interval_is_rejected(self):
+        """Test that a zero heartbeat interval fails at construction.
+
+        Nothing downstream reads zero as "disabled"; it heartbeats in a
+        continuous loop for as long as the client lives.
+        """
+        with pytest.raises(ValueError, match="zero"):
+            HttpConfig(heartbeat_interval=timedelta(0))
+
+
[email protected]
+class TestClientConstruction:
+    """Test what `IggyClient.http(...)` accepts."""
+
+    def test_accepts_a_config(self):
+        """Test that a client can be built from a config object."""
+        assert IggyClient.http(HttpConfig(api_url="http://127.0.0.1:3000";)) is 
not None
+
+    def test_accepts_nothing(self):
+        """Test that the default configuration is used when no argument is 
given."""
+        assert IggyClient.http() is not None
+
+
[email protected]
+class TestHttpConfigAgainstServer:

Review Comment:
   Could you add one HTTP round trip here - login, create stream/topic, send, 
poll, compare the payload? That is the part the PR description says was 
untested.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to