Skip to content

AdminService

faststream.kafka.helpers.AdminService #

AdminService()
Source code in faststream/kafka/helpers/admin.py
26
27
def __init__(self) -> None:
    self.admin_client: AIOKafkaAdminClient | None = None

admin_client instance-attribute #

admin_client: AIOKafkaAdminClient | None = None

client property #

client: AIOKafkaAdminClient

connect async #

connect(**connection_kwargs: Any) -> None
Source code in faststream/kafka/helpers/admin.py
29
30
31
32
33
34
35
36
37
38
async def connect(self, **connection_kwargs: Any) -> None:
    if self.admin_client is not None:
        return

    admin_options, _ = filter_by_dict(
        AdminClientConnectionParams,
        connection_kwargs,
    )
    self.admin_client = aiokafka.admin.client.AIOKafkaAdminClient(**admin_options)
    await self.admin_client.start()

disconnect async #

disconnect() -> None
Source code in faststream/kafka/helpers/admin.py
40
41
42
43
async def disconnect(self) -> None:
    if self.admin_client is not None:
        await self.admin_client.close()
        self.admin_client = None

create_topics async #

create_topics(topics: list[Topic]) -> list[CreateResult]

Create topics through the admin client.

Already-exists is success. Any other per-topic error is collected rather than raised, so a subscriber still starts against a topic the cluster refused to create.

Source code in faststream/kafka/helpers/admin.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
async def create_topics(self, topics: list["Topic"]) -> list[CreateResult]:
    """Create topics through the admin client.

    Already-exists is success. Any other per-topic error is collected rather
    than raised, so a subscriber still starts against a topic the cluster
    refused to create.
    """
    if not topics:
        return []

    try:
        response = await self.client.create_topics(
            [topic.to_aiokafka() for topic in topics],
        )
    except Exception as e:
        return [CreateResult(topic.name, e) for topic in topics]

    results = []
    for topic_error in response.to_object()["topic_errors"]:
        topic = topic_error["topic"]
        error_code = topic_error["error_code"]
        if not error_code:
            results.append(CreateResult(topic, None))
            continue

        error = for_code(error_code)(
            topic_error.get("error_message") or f"error code {error_code}",
        )
        if isinstance(error, TopicAlreadyExistsError):
            results.append(CreateResult(topic, None))
        else:
            results.append(CreateResult(topic, error))

    return results