Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporate support to convert Kafka headers to HTTP headers #1051

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions examples/kafka-hub/hub/modules/persistence/persistence.bal
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ballerina/websubhub;
import kafkaHub.config;
import kafkaHub.connections as conn;
import ballerinax/kafka;
ayeshLK marked this conversation as resolved.
Show resolved Hide resolved

public isolated function addRegsiteredTopic(websubhub:TopicRegistration message) returns error? {
check updateHubState(message);
Expand Down Expand Up @@ -49,8 +50,18 @@ public isolated function addUpdateMessage(string topicName, websubhub:UpdateMess
check produceKafkaMessage(topicName, payload);
}

isolated function produceKafkaMessage(string topicName, json payload) returns error? {
byte[] serializedContent = payload.toJsonString().toBytes();
check conn:statePersistProducer->send({ topic: topicName, value: serializedContent });
isolated function produceKafkaMessage(string topicName, json payload,
map<string|string[]> headers = {}) returns error? {
kafka:AnydataProducerRecord message = getProducerMsg(topicName, payload, headers);
check conn:statePersistProducer->send(message);
check conn:statePersistProducer->'flush();
}

isolated function getProducerMsg(string topic, json payload,
map<string|string[]> headers) returns kafka:AnydataProducerRecord {
byte[] value = payload.toJsonString().toBytes();
if headers.length() == 0 {
return { topic, value };
}
return { topic, value, headers };
ayeshLK marked this conversation as resolved.
Show resolved Hide resolved
}
15 changes: 14 additions & 1 deletion examples/kafka-hub/hub/websub_subscribers.bal
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,20 @@ isolated function deSerializeKafkaRecord(kafka:ConsumerRecord kafkaRecord) retur
json payload = check value:fromJsonString(message);
websubhub:ContentDistributionMessage distributionMsg = {
content: payload,
contentType: mime:APPLICATION_JSON
contentType: mime:APPLICATION_JSON,
headers: check getHeaders(kafkaRecord)
};
return distributionMsg;
}

isolated function getHeaders(kafka:ConsumerRecord kafkaRecord) returns map<string|string[]>|error {
map<string|string[]> headers = {};
foreach var ['key, value] in kafkaRecord.headers.entries().toArray() {
if value is string || value is string[] {
headers['key] = value;
} else if value is byte[] {
headers['key] = check string:fromBytes(value);
}
}
return headers;
}
Loading