- Purpose of the article: This Blog provides a solution to use different HTTP methods of a single resource type with similar functionality in different endpoints of RAML.
- Intended Audience: MuleSoft Developers, API Design using RAML.
- Tools and Technology: RAML, MuleSoft, Anypoint Platform.
- Keywords: Resource Types, Methods, Multiple Resources, Multiple Endpoints, optional, RAML
Before starting, let us know briefly about resource types.
Resource Types:
Resource type is a protocol, which can specify security schemes, descriptions, methods, and parameters.
A Resource inherits the properties of the Resource type. A Resource type can also inherit properties from another resource type.
By using Resource types, we are reducing Code Complexity and Duplication.
Now, to understand our topic let us consider one scenario.
Consider two companies “ABC” and “XYZ”. We are exposing two different endpoints for these two companies.
Company ABC needs two endpoints for its operations i.e., one endpoint used for creating a new employee detail and another endpoint is for retrieving current employee details.
Company XYZ needs one endpoint for its operations with, one endpoint for retrieving the current employee details based on their employee ID.
So, in our scenario, we need two endpoints
Post: – This method will be used to create a new employee detail.
Get: – This method will be used to fetch a current employee’s detail.
Step 1: Open the Design centre in Anypoint Platform and create a new API Specification.
Step 2: Create a resource type as shown below.
In this Resource type, we have defined two methods: post and get. These methods will be used to create and retrieve employee data respectively.
Step 3: For our scenario, we have exposed two resources.
- employeeData: This endpoint is exposed to Company ABC. With this endpoint, the user can create new employee data and retrieve current employee data.
- getEmployeeData: This endpoint is exposed to Company XYZ. With this endpoint, the user can retrieve current employee data based on employee ID.
Below is the Complete RAML
We can observe that for the getEmployeeData Resource, the post method is also populated.
As per our requirement, we only need the get method. This can be done by creating a separate Resource type for getEmployeeData but this increases the code as we are defining the same method multiple times. However, it is a time-consuming process.
To overcome this issue, we need to make a small change in our existing Resource type. Just put a ‘?’ symbol for the two methods defined in the Resource type as shown below.
Usage of the ‘?’ symbol in RAML means that it is an optional parameter.
We are making the two methods in resource type optional.
Step 4:
For the employeeData resource, call the post and get methods as shown in the figure, as we require two methods for this resource.
For the getEmployeeData resource, call the get method as shown in the figure, as we require only the get method for this resource.
Now, we can see that for the getEmployeeData resource, only the get method is populated.
In this way, we can use different methods of single resource type in different resources which have the same functionality.
Author Bio:
Raja Shekhar TOGITI
Technical Consultant
I am a MuleSoft Developer and have about 2 years of work experience. In MOURI Tech, I have been involved in multiple projects where I have designed API specifications using RAML, have used different MuleSoft connectors, and have overseen end to end development with Munit testing and Cloudhub deployments.