Back to EIP

PatternNormalizer routes each message type through a custom message translator so that the resulting messages match a common format.
How Ballerina helps

Ballerina's type test (is keyword) and match statement can both be used to test the structure of incoming data. Then the data can be transformed into the required format to construct new data structures. Ballerina's data-oriented design helps with complex transformations with features such as destructuring, query expressions, spread operator, etc.

NormalizerMessage ChannelMessage EndpointMessage TranslatorMessage RouterMessage
Copy
import ballerina/http;

type ZendeskResponse record {
    record {|
        string url;
        int id;
        string subject;
    |} ticket;
};

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

service /api/v1 on new http:Listener(8080) {

    resource function post ticket(@http:Payload json|xml request) returns string|error {
        json normalizedRequest;
        if request is json {
            normalizedRequest = normalize(check request.subject, check request.comment);
        } else {
            normalizedRequest = normalize((request/<subject>).data(), (request/<comment>).data());
        }
        ZendeskResponse zendeskResponse = check zendeskClient->/api/v2/tickets.post(normalizedRequest);
        return zendeskResponse.ticket.url;
    }
}

function normalize(string subject, string comment) returns json {
    return {
        ticket: {
            subject,
            comment: {
                body: comment
            }
        }
    };
}