• Introduction
This article explains how to implement an API-led Architecture in Mule. I have prepared this demo to explain the layer differences and their implementations. This example is mainly for beginners.
This example will NOT be a right real time example, but the process will be same in real time implementation.
GOAL : The purpose of this demo is to process a valid CSV file and store it into a database.
The Experience API lets you expose the API to the end user. The Process API lets you validate the file extension as .csv or not. A system API will carry out the system level processes, such as validating the file contents and processing them to the database.
  • Projects Definations
Before starts implement the API layes we should aware how "Domain project" works in Mule.
If we have common connector or configuration for different APIs we can club them into a "domain project". And we can share those resources in deifferent API deployed in same zones.
We can define the API names as like below :

Step 1 : Define the global definitions for the http connectors in the "import-csv-domain" project.

  • Experience API
The Experience API is to expose to end user. Ideally an Experience API should not have coplex logics. It should connet to either Process or System API based on requirement.
Experience API should maintain a good quality of security patterns to secure from outside calls.
In this Demo, the "import-csv-experience-api" (Experience API) is interacting with the Process API by sending a csv file containing data.
This import-csv-experience-api is expecting a status from process API, weather the CSV file validated and stored the data into Database successfully or not.
It doesn't mean that process API will return same status or response message which was received from System API. Based on logic, business need Process API could send response to Experience API.
Step 2 : Define the RAML in "import-csv-experience-api".

Step 3 : To generate the flow, right-click on the api.raml file and choose "Mule -> Generate Flows from REST API".
Step 4 : It generates api.xml and add flows. After generate the flows by API kit router, add flow reference to call Process API.

Step 5 : Define subflow("initiate-process-api-call") and its logic in api-impl.xml to call process API.

  • Process API
The Process API is an intermediate API. Process API sits between Experience and System API based on requirement.
Idealy Process API should the business logic to interact with System API. So, we can expact many transformation logic in this layer.
In this Demo, the "import-csv-process-api" (Process API) is interacting with the System API by sending a csv file containing data which was sent by Experience API.
This import-csv-process-api is expecting a status from System API, weather the CSV file validated and stored the data into Database or not.
Once process API received response from System API, it will respond back to caller (i.e Experience API). Process API will prepare response status and response message based on business need.
Step 6 : Define the RAML in "import-csv-process-api".

Step 7 : To generate the flow, right-click on the api.raml file and choose "Mule -> Generate Flows from REST API".
Step 8 : It generates api.xml and add flows. After generate the flows by API kit router, add flow reference to call System API.

Step 9 : Define subflow("initiate-system-api-call") and its logic in api-impl.xml to call System API.

  • System API
The System API is for low-level processes. System API sits on top of legacy system or Database or any other system which provide actual data to the API.
Idealy System API should the deal with System related functionality or low level functionality.
In this Demo, the "import-csv-system-api" (System API) received the JSON input from the Process API, and it will validate the content one by one with the Mule validator.
If any validation fails, the API will get the actual reason for the validation and reject the flow and pass the reason to the Experience API through the Process API.
If the validation of all the content succeeds, process the data for the database to insert/update the record and respond back to Process API.
Step 10 : Define the RAML in "import-csv-system-api".

Step 11 : To generate the flow, right-click on the api.raml file and choose "Mule -> Generate Flows from REST API".
Step 12 : It generates api.xml and add flows. After generate the flows by API kit router, add flow reference to process low level functionality (i.e Validate the input content and store in Database).

Step 13 : Define subflow("process-file") and its logic in api-impl.xml to perform low level logic, (i.e. Validate the input content and store in Database).
NOTE : In the System API I haven't confired the DB connector. But, if you wish you can do that and persist the JSON content.

Step 14 : CustomValidationException.java - This custom exception class lets you catch the exception thrown by the validator flow.

  • Sample CSV File
Here are the sample input csv files :
Valid sample:

Invalid sample:

  • API Response
Here are the responses from Experience API :
Valid Response:

Invalid Response:

If you got some knowledge about Mule API: Experience, Process, System, please follow with me in next topic to understand about ....

Comments