Back to EIP

PatternContent enricher adds data that was not sent by the original sender to the message.
How Ballerina helps

Ballerina enables additional lookups to enrich the message, such as database lookups, REST API calls, etc. Spread operator (...) helps to create new records out of existing records while enriching them with additional data.

Content EnricherMessage ChannelMessage EndpointMessage
Copy
import ballerina/http;

type BankAccountReq record {|
    string name;
    string accountNumber;
    string routingNumber;
    string? country;
|};

type IbanRequest record {|
    "json"|"xml" format = "json";
    string country_iso;
    string nid;
|};

type IbanResponse record {
    string bank_code;
};

type BankAccount record {
    *BankAccountReq;
    string id;
    string? bankCode;
};

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

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

    resource function post customers/[int id]/accounts(BankAccountReq req) returns BankAccount|error {
        IbanRequest ibanReq = {country_iso: req.country ?: "US", nid: req.accountNumber};
        IbanResponse ibanRes = check iban->/clients/api/banksuite/nid.post(ibanReq);
        return check intuit->/quickbooks/v4/customers/[id]/bank\-accounts.post({...req, bankCode: ibanRes.bank_code});
    }
}