Lab 1: Implement the Omni Channel API in Studio
In this first lab, you will use Anypoint Studio to create a Mule application where there will be one flow for each method of each resource (i.e. GET). Additionally you will use APIKit (as part of your Mule application) to process each REST requests, transform them to messages to be handled and processed by each flow.
The implementation will consist of a few steps
Create a new Mule Project in Anypoint Studio from the Omni Channel API RAML
Run the Mule app from Anypoint Studio
Then we will test this new API using the API Console.
In this step we will create a new Mule application in Anypoint Studio from the Omni Channel API RAML Definition. This will be the implementation of our REST API.
Start Anypoint Studio from the desktop icon.
You are presented with the Anypoint Studio 7 Welcome Screen.
Click through the "What’s new" items and click Go to Workspace.
In the Workspace Launcher, use the default workspace directory location.
From Anypoint Studio’s menu, select File > New > Mule Project to create a New Mule project. A Window will pop-up to define the details of this new application.
Give the project the name omni-channel-api-v1-0
Select the Mule Server 4.1.4 EE.
Under API Implementation settings, check Specify API definition file location or URL and choose Design Center… by clicking in the (…) button on the right of the Location textbox.
Once you clicked there, it should take you to the Browse Design Center for APIs window, if it asks you to login, use your Anypoint Platform account.
Select the workshop business group.
Select the <username> - Omni Channel Experience API that you created in Module 1 and click Ok. If you can’t find your API, click through the different Business Groups. You can also search for your API using the type filter text field. (you may have more API Definitions showing in this window)
If you don’t see the API, press Load more projects. This link only appears when there are too many projects on the API Designer.
Now click Finish. This will create a skeleton project that implements your API.
Let’s explore what was automatically generated.
At the top of the diagram you will see a flow called api-main and api-console followed by flows, one for each method defined in the RAML.
The flows below are defined by your API Design in the RAML file. Typically, there will be flows that look like this get:\resource, post:\resource, put:\resource, etc. Note that the name of the flow is very important for the APIkit router to be able to route the request to the appropriate flow - you don’t want to change these.
When APIkit detects example data in the response of a method in the RAML it inserts a Dataweave Transform Message into the flow which returns the static response specified by the example data reference.
The static response returned by the auto-generated Dataweave Transform Message allows you to test the API as a stub immediately after generation. Obviously these flows can be enhanced to provide more advanced mock services as well as evolve them into full API implementation as we will see in the next lab.
This is the main flow. It exposes an HTTP service and processes the requests using the APIKit Router.
The HTTP request will be converted to a mule message, and redirected to the requested flow by the APIKit router.
By selecting the http listener one can take a look at the HTTP configuration, you will see that its listening for requests on http://localhost:8081/api.
Inside the api-main flow also an Error-Handling block is included. In this block multiple On Error Propagate definitions are autogenerated for common API error responses such as METHOD_NOT_ALLOWED, BAD_REQUEST etc.
This flow puts an item into a users shopping cart based on the user id. This put operation returns no response and therefore the Dataweave Transform Message processor is replaced automatically by a Logger message processor.
This flow gets an order based on an order id
This flow gets a product based on a product id
This flow returns orders based on search criteria
This flow returns products based on a search criteria
This flow returns all the items in a users shopping cart based on a user id
This flow confirms the items to purchase in a users shopping cart.
To test the API, let’s run it within Studio first. Right click the application.
Select Run As > Mule Application.
The application will start running, and the console will show the Mule Runtime logs
Anypoint Studio deployed this application to an embedded Mule Runtime. There is no need to deploy to a separate Mule server environment. The developer will be able to develop and test the application locally until it’s ready to be deployed to a shared development or QA environment.
Test the application using the console. Click button Open Console on the APIkit Console tab.
A browser window opens at: http://localhost:8081/console/.
Click open the /products/search resource on the left and click GET.
Click the TRY IT button on the top right to submit the request.
Then tick the Show optional parameters and fill them in as below and click SEND.
The response will show mocked data that is defined in the imported RAML.
It is a live application deployed to the Mule runtime server but it is leveraging the RAML information to provide a “mocking” experience.
Let’s go ahead and stop the application to get ready for the next lab.
Go to the console tab and press the red squared button to stop the Mule runtime server.
In this lab, we
Downloaded the API definition directly into Anypoint Studio.
Used the RAML specification to generate the initial implementation skeleton for the API.
Auto-generated a skeleton project to implement the API.
This lab shows how APIkit quickly allows developers to import the designed REST API (RAML and supporting artifacts) to enable rapid API logic implementation in the generated MuleSoft flows. The APIkit also deploys the interactive console with the MuleSoft flows. The lab shows the extent to which a developer can deploy an on-premise and interactive API from the API specification in RAML.
It is important to note that the implementation of an API is a real MuleSoft application and allowed us to test the end-to-end API. Building integration applications and exposing APIs do not require additional knowledge making it easy for MuleSoft developers to work on both integrations and APIs.
Congratulations! You have completed Lab 1.
Please proceed to Lab 2