API is the most common practice to exchange messages in a microservices architecture world. There are actually two different approaches for API development. One is called Model First and the other is called Design First. Usually the latter, AKA Spec-Driven Development (SDD), is preferred over the former.
When is the Model First approach useful? If you are running legacy API applications, this would be a good example of using this approach. If those systems are well documented, API documents can be easily extracted by tools like Swagger which is now renamed to Open API. There are many implementations for Swagger, Swashbuckle for example, so it’s very easy to extract API spec document using those tools.
What if we are developing a new API application? Should I develop the application and extract its API spec document like above? Of course we can do like this. There’s no problem at all. If the API spec is updated, what should we do then? The application should be updated first then the updated API spec should be extracted, which might be expensive. In this case, the Design First approach would be useful because we can make all API spec clearly before the actual implementation, which we can reduce time and cost, and achieve better productivity. Here’s a rough diagram how SDD workflow looks like:
- Design API spec based on the requirements.
- Simulate the API spec.
- Gather feedback from API consumers.
- Validates the API spec if it meets the requirements.
- Publish the API spec.
There is an interesting point. How can we run/simulate the API without the actual implementation? Here we can introduce API mocking. By mocking APIs, front-end developers, mobile developers or other back-end developers who actually consume the APIs can simulate expected result, regardless that the APIs are working or not. Also by mocking those APIs, developers can feed back more easily.
In this post, we will have a look how API mocking features work in different API management tools such as MuleSoft API Manager, Azure API Management and Amazon API Gateway, and discuss their goods and bads.
MuleSoft API Manager + RAML
MuleSoft API Manager is a strong supporter of RAML(RESTful API Modelling Language) spec. RAML spec version 0.8 is the most popular but recently its Spec version 1.0 has been released. RAML basically follows the YAML format, which brings more readability to API spec developers. Here’s a simple RAML based API spec document.
According to the spec document, API has two different endpoints of /products
and /products/{productId}
and they define GET
, POST
, PATCH
, DELETE
respectively. Someone might have picked up in which part of the spec document would be used. Each response body has its example
node. These nodes are used for mocking in MuleSoft API Manager. Let’s see how we can mock both API endpoints.
First of all, login to Anypoint website. You can create a free trial account, if you want.
Navigate to API Manager
by clicking either the button or icon.
Click the Add new API
button to add a new API and fill the form fields.
After creating an API, click the Edit in API designer
link to define APIs.
We have already got the API spec in RAML, so just import it.
The designer screen consists of three sections – left, centre, and right. The centre section shows the RAML document itself while the right section displays how RAML is visualised as API document. Now, click the Mocking Service
button at the top-right corner to activate the mocking status. If mocking is enabled, the original URL is commented out and a new mocking URL is added like below:
Of course, when the mocking is de-activated, the mocking URL disappears and the original URL is uncommented. With this mocking URL, we can actually send requests through Postman for simulation. In addition to this, developers consuming this API don’t have to wait until it’s implemented but just using it. The API developer can develop in parallel with front-end/mobile developers. This is the screenshot of using Postman to send API requests.
As defined in the example
nodes of RAML spec document, the pre-populated result is popping out. Like the same way, other endpoints and/or methods return their mocked results.
So far, we have looked how MuleSoft API Manager can handle RAML and API mocking. It’s very easy to use. We just imported a RAML spec document and switched on the mocking feature, and that’s it. Mocking can’t be easier that this. It also supports Swagger 2.0 spec. When we import a Swagger document, it is automatically converted to a RAML 1.0 document. However, in the API designer, we still have to use RAML. It would be great if MuleSoft API Manager supports to edit Swagger spec documents out of the box sooner rather than later, which is a de-facto standard nowadays.
There are couple of downsides of using API Manager. We can’t have precise controls on individual endpoints and methods.
- “I want to mock only the
GET
method on this specific endpoint!” - “Nah, it’s not possible here. Try another service.”
Also, the API designer changes the base URL of API, if we activate the mocking feature. It might be critical for other developers consuming the API for their development practice. They have to change the API URL once the API is implemented.
Now, let’s move onto the example supporting Swagger natively.
Azure API Management + Swagger
Swagger has now become Open API, and spec version 2.0 is the most popular. Recently a new spec version 3.0 has been released for preview. Swagger is versatile – ie. it supports YAML and JSON format. Therefore, we can design the spec in YAML and save it into a JSON file so that Azure API Management can import the spec document natively. Here’s the same API document written in YAML based on Swagger spec.
It looks very similar to RAML, which includes a examples
node in each endpoint and/or method. Therefore, we can easily activate the mocking feature. Here’s how.
First of all, we need to create an API Management instance on Azure Portal. It takes about half an hour for provisioning. Once the instance is fulfilled, click the APIs - PREVIEW
blade at the left-hand side to see the list of APIs.
We can also see tiles for API registration. Click the Open API specification
tile to register API.
Upload a Swagger definition file in JSON format, say swagger.json
, and enter appropriate name and suffix. We just used shop
as a prefix for now.
So, that’s it! We just uploaded the swagger.json
file and it completes the API definition on API Management. Now, we need to mock an endpoint. Unlike MuleSoft API Manager, Azure API Management can handle mocking on endpoints and methods individually. Mocking can be set in the Inbound processing
tile as it intercepts the back-end processing before it hits the back-end side. It can also set mocking at a global level rather than individual level. For now, we simply setup the mocking feature only on the /products
endpoint and the GET
method.
Select /products - GET
at the left-hand side and click the pencil icon at the top-right corner of the Inbound Processing
tile. Then we’re able to see the screen below:
Click the Mocking
tab, select the Static responses
option on the Mocking behavior
item, and choose the 200 OK
option of the Sample or schema responses
item, followed by Save
. We can expect the content defined under the examples
node. Once saved, the screen will display something like this:
In order to use the API Management, we have to send a special header key in every request. Go to the Products - PREVIEW
blade and add the API that we’ve defined above.
Go to the Users - PREVIEW
blade to get the Subscription Key
.
We’re ready now. In Postman, we can see the result like:
The subscription key has been sent through the header with a key of Ocp-Apim-Subscription-Key
and the result was what we expected above, which has already been defined in the Swagger definition.
So far, we have used Azure API Management with Swagger definition for mocking. The main differences between MuleSoft API Manager and Azure API Management are:
- Azure API Management doesn’t change the mocking endpoint URL, while MuleSoft API Manager does. That’s actually very important for front-end/mobile developers because they don’t get bothered with changing the base URL – same URL with mocked result or actual result based on the setting.
- Azure API Management can mock endpoint and method individually. We only mock necessary ones.
However, the downside of using Azure API Management will be the cost. It costs more than $60/month on the developer pricing plan, which is the cheapest. MuleSoft API Manager is literally free to use, from the mocking perspective (Of course we have to pay for other services in MuleSoft).
Well, what else service can we use together with Swagger? Of course there is Amazon AWS. Let’s have a look.
Amazon API Gateway + Swagger
Amazon API Gateway also uses Swagger for its service. Login to the API Gateway Console and import the Swagger definition stated above.
Once imported, we can see all API endpoints. We choose the /products
endpoint with the GET
method. Select the Mock
option of the Integration type
item,, and click Save
.
Now, we’re at the Method Execution
screen. When we see at the rightmost-hand side of the screen, it says Mock Endpoint
where this API will hit. Click the Integration Request
tile.
We confirm this is mocked endpoint. Right below, select the When there are no templates defined (recommended)
option of the Request body passthrough
item.
Go back to the Method Execution
screen and click the Integration Response
tile.
There’s already a definition of the HTTP Status Code of 200
in Swagger, which is automatically showed upon the screen. Click the triangle icon at the left-hand side.
Open the Body Mapping Templates
section and click the application/json
item. By doing so, we’re able to see the sample data input field. We need to put a JSON object as a response example by hand.
I couldn’t find any other way to automatically populate the sample data. Especially, the array type response object can’t be auto-generated. This is questionable; why doesn’t Amazon API Gateway allow a valid object? If we want to avoid this situation, we have to update our Swagger definition, which is vendor dependent.
Once the sample data is updated, save it and go back to the Method Execution
screen again. We are ready to use the mocking feature. Wait. One more step. We need to publish for public access.
Eh oh… We found another issue here. In order to publish (or deploy) the API, we have to set up the Integration Type
of all endpoints and methods INDIVIDUALLY!!! We can’t skip one single endpoint/method. We can’t set up a global mocking feature, either. This is a simple example that only contains four endpoints/methods. But, as a real life scenario, there are hundreds of API endpoints/methods in one application. How can we set up individual endpoints/methods then? There’s no such way here.
Anyway, once deployment completes, API Gateway gives us a URL to access to API Gateway endpoints. Use Postman to see the result:
So far, we have looked Amazon API Gateway for mocking. Let’s wrap up this post.
- Global API Mocking: MueSoft API Manager provides a one-click button, while Amazon API Gateway doesn’t provide such feature.
- Individual API Mocking: Both Azure API Management and Amazon API Gateway are good for handling individual endpoints/methods, while MuleSoft API Manager can’t do it. Also Amazon API Gateway doesn’t well support to return mocked data, especially for array type response. Azure API Management perfectly supports this feature.
- Uploading Automation of API Definitions: Amazon API Gateway has to manually update several places after uploading Swagger definition files, like examples as mocked data. On the other hand, both Azure API Management and MuleSoft API Manager perfectly supports this feature. There’s no need for manual handling after uploading definitions.
- Cost of API Mocking: Azure API Management is horrible from the cost perspective. MuleSoft provides virtually a free account for mocking and Amazon API Gateway offers the first 12 months free trial period.
We have so far briefly looked how we can mock our APIs using the spec documents. As we saw above, we don’t need to code for this feature. Mocking purely relies on the spec document. We also have looked how this mocking can be done in each platform – MuleSoft API Manager, Azure API Management and Amazon API Gateway, and discuss merits and demerits of each service, from the mocking perspective.
Comments are closed.