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 type below) which helps to define choices in a type-safe and readable manner.

Message RouterMessage ChannelMessage EndpointMessage
Copy
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 {
    UK,
    DE
}

final http:Client dhl = check new ("http://api.dhl.com.balmock.io");

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 response.events[0].status;
            }
            _ => {
                return error("County not supported");
            }
        }
    }
}