Back to EIP

PatternMessage router consumes a message from one channel and republishes it to a different channel depending on a set of conditions.
How Ballerina helps

Ballerina supports conditional logic with if-else and match statements. This can be used to route messages based on message content, header, or any custom logic. Ballerina type system supports union types (e.g. Country enum below is a union) which helps to define choices in a type-safe and readable manner.

Message RouterMessage ChannelMessage EndpointMessage
import ballerina/http;

type DhlUkResponse record {|
    string url;
    record {|
        string id;
        Status status;
    |}[] shipments;

type DhlDpiResponse record {|
    Status[] events;
    string publicUrl;
    string barcode;

type Status record {|
    string statusCode;
    string status;

enum Country {

final http:Client dhl = check new ("");

service /shipments on new http:Listener(8080) {

    resource function get [Country country]/[string trackingNumber]/status() returns string|error {
        match country {
            UK => {
                DhlUkResponse response = check dhl->/parceluk/tracking/v1/shipments(trackingNumber = trackingNumber);
                return response.shipments[0].status.status;
            DE => {
                DhlDpiResponse response = check dhl->/dpi/tracking/v1/trackings/[trackingNumber];
            _ => {
                return error("County not supported");