hubcio commented on code in PR #4710:
URL: https://github.com/apache/iggy/pull/4710#discussion_r4474742733


##########
examples/python/README.md:
##########
@@ -149,6 +149,40 @@ python message-headers/typed-headers/producer.py
 python message-headers/typed-headers/consumer.py
 ```
 
+### Sink Data Producer
+
+Populates an Iggy topic with JSON records so that sink connectors have data to 
consume.
+The producer generates 100 batches of 100 to 500 records each.

Review Comment:
   nit: the documented batch range includes 500, but the producer selects 100 
through 499 records. document the actual range.



##########
examples/python/sink-data-producer/producer.py:
##########
@@ -0,0 +1,157 @@
+# 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 json
+import os
+import random
+import string
+from datetime import UTC, datetime, timedelta
+from typing import NamedTuple
+
+from apache_iggy import (
+    AutoLogin,
+    DirectProducerConfig,
+    IggyClient,
+    Partitioning,
+    TcpConfig,
+)
+from apache_iggy import SendMessage as Message
+from loguru import logger
+
+STREAM_NAME = os.getenv("IGGY_STREAM", "qw")
+TOPIC_NAME = os.getenv("IGGY_TOPIC", "records")
+
+SOURCES = ["browser", "mobile", "desktop", "email", "network", "other"]
+STATES = ["active", "inactive", "blocked", "deleted", "unknown"]
+DOMAINS = [
+    "gmail.com",
+    "yahoo.com",
+    "hotmail.com",
+    "outlook.com",
+    "aol.com",
+]
+
+MAX_BATCHES = 100
+MIN_RECORDS_PER_BATCH = 100
+MAX_RECORDS_PER_BATCH = 500
+
+
+class ArgNamespace(NamedTuple):
+    connection_string: str | None
+    address: str
+    username: str
+    password: str
+
+
+def parse_args() -> ArgNamespace:
+    address = os.getenv("IGGY_ADDRESS", "127.0.0.1:8090")
+    username = os.getenv("IGGY_USERNAME", "iggy")
+    password = os.getenv("IGGY_PASSWORD", "iggy")
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        "connection_string",
+        help=(
+            "Connection string for Iggy, for example "
+            "'iggy+tcp://iggy:[email protected]:8090'"
+        ),
+        default=None,
+        nargs="?",
+    )
+    args = parser.parse_args()
+    return ArgNamespace(
+        connection_string=args.connection_string,
+        address=address,
+        username=username,
+        password=password,
+    )
+
+
+def random_alnum(length: int) -> str:
+    alphabet = string.ascii_letters + string.digits
+    return "".join(random.choices(alphabet, k=length))  # noqa: S311
+
+
+def random_record() -> dict:
+    return {
+        "user_id": f"user_{1 + random.randrange(99)}",  # noqa: S311
+        "user_type": 1 + random.randrange(4),  # noqa: S311
+        "email": (
+            f"{random_alnum(3 + random.randrange(17))}"  # noqa: S311
+            f"@{random.choice(DOMAINS)}"  # noqa: S311
+        ),
+        "source": random.choice(SOURCES),  # noqa: S311
+        "state": random.choice(STATES),  # noqa: S311
+        "message": random_alnum(10 + random.randrange(90)),  # noqa: S311
+        "created_at": (
+            datetime.now(UTC) - timedelta(days=random.randrange(1000))  # 
noqa: S311
+        ).isoformat(),
+    }
+
+
+async def main() -> None:
+    args = parse_args()
+
+    if args.connection_string:
+        client = IggyClient.from_connection_string(args.connection_string)
+    else:
+        config = TcpConfig(
+            server_address=args.address,
+            auto_login=AutoLogin.username_password(args.username, 
args.password),
+        )
+        client = IggyClient(config)
+
+    logger.info("Starting data producer...")
+    logger.info(f"Stream: {STREAM_NAME}, Topic: {TOPIC_NAME}")
+
+    await client.connect()

Review Comment:
   warning: an `iggy+http://...` argument reaches producer initialization 
without login, so `get_stream()` fails with `Unauthenticated`. reject 
unsupported HTTP connection strings explicitly or call `login_user()` before 
producer initialization.



-- 
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