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
|