Introduction
This is the continuation of my previous
blog, where I started with the concepts of event driven architecture and integrated event mesh with CAP project. In this blog I continue the journey by creating a consumer API and integrating an end to end use case starting from CAP project (REST API) - SAP Event Mesh - SAP Integration suite (REST API) - TWILIO
Let us break down the tasks which we are going to perform.
- Design thinking and a quick recap of Part-1
- Create a consumer application using SAP Integration Suite
- Configure SAP Integration Suite REST API as Web hook in Event Mesh
- Test the complete flow: CAP (REST API) - SAP Event Mesh - SAP Integration Suite (REST API) - TWILIO
Sounds cool right? Lets start the journey.
Task-1: Design thinking and a Quick Recap of Part-1
In
Part-1 of the blog, we completed below tasks -
- Create Event Mesh message client
- Publish message from CAP application to Event Mesh.
In this blog (Part-2), we will create a REST API using SAP Integration suite and configure it as a consumer for consuming the messages from Event Mesh. A quick recap can be elaborated with the help of below diagram -
Figure-1
We are going to focus on Part-B and Part-C.
Task-2: Create a Consumer API using SAP Integration Suite
The goal of this task is to
- Set up SAP Integration Suite
- Create REST API
- Integrate with TWILIO leveraging open connectors
All the above tasks are very well documented in my blog -
here
I recommend you to go through the
blog. Feel free to like, share and comment to improve it further.
Task-3: Configure SAP Integration Suite REST API as a web hook in Event Mesh
In this task, we are going to set up a consumer API as a web hook in SAP Event Mesh.
Let's get started.
- Open the event mesh portal and go the message client.
- Go to the web hook tab as shown in the below screenshot
- Click - Create Webhook
- Set up the properties as mentioned below
- Subscription Name - A name for your web hook
- Queue Name - The queue for which you are creating the web hook and configuring it as a consumer
- Exempt Handshake - Select as YES.
- Web hook URL - The REST API url for SAP Integration Suite
- Default Content-Type - application/json
- Authentication - OAuth2ClientCredentials
- Client ID - Client ID for SAP Integration Suite. This you will get it from the SAP Integration Suite Service Key.
- Client Secret - Client secret for SAP Integration Suite. This you will get it from the SAP Integration Suite Service Key.
- Token URL - OAuth token url for SAP Integration Suite. This you will get it from the SAP Integration Suite Service Key.
- Once done, you should see the Subscription Status as ACTIVE.
All set. You have successfully completed this task.
Task-4: Test the complete flow: CAP - SAP Event Mesh - SAP Integration Suite - TWILIO
In this section,
- We will send a message to the CAP microservice API for new user creation.
- The CAP event handler will compile a welcome message and push it to event mesh topic.
- The event mesh topic will push the message to the queue through queue subscription.
- The message will be then consumed by the SAP Integration Suite REST API through the web hook.
- If all goes well, you should receive a text message on your phone from TWILIO.
Let us see all that in action using POSTMAN.
- Open the POSTMAN collection which you might have created as per the guidelines in the task-5 of my previous blog
- Sample request to create a new user with role Admin
{
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@remindme.com",
"phone": "XXXXXXXXXX",
"userRole_name": "Admin"
}
- I am providing the final CAP event handler in action. In this event handler, I am using
- @Before event to generate a random number and save it in db as OTP
- @after event to create message and send it to the SAP Event mesh topic
package com.sap.cap.taskmanager.handlers;
import java.util.Objects;
import com.nimbusds.jose.shaded.json.JSONObject;
import com.sap.cap.taskmanager.util.TaskManagerUtil;
import com.sap.cds.services.cds.CqnService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.After;
import com.sap.cds.services.handler.annotations.Before;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.messaging.MessagingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import cds.gen.adminservice.User;
import cds.gen.adminservice.User_;
@Component
@ServiceName("AdminService")
public class AdminService implements EventHandler {
Logger logger = LoggerFactory.getLogger(AdminService.class);
private static final String WELCOME_STRING = "Welcome! Start to manager your tasks efficiently";
private static final String PASSWORD_PREFIX = "capm";
@Autowired
@Qualifier("taskmanager-events")
MessagingService messagingService;
@Before(event = CqnService.EVENT_CREATE , entity = User_.CDS_NAME)
public void beforeCreate(User userData) {
String otp = PASSWORD_PREFIX + String.valueOf(TaskManagerUtil.generateRandomNumber()) ;
userData.setOtp(otp);
logger.info("Generated default otp for {}", userData.getFirstName());
}
@After(event = CqnService.EVENT_CREATE , entity = User_.CDS_NAME)
public void afterCreate(User userData) {
JSONObject payload = new JSONObject();
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", WELCOME_STRING);
jsonObject.put("fromPhone", "+1XXXXXXXXXX");
if(Objects.nonNull(userData.getPhone()) && userData.getPhone().startsWith("+1")){
jsonObject.put("toPhone",userData.getPhone());
} else {
jsonObject.put("toPhone","+1" + userData.getPhone());
}
payload.put("data", jsonObject);
logger.info("Sending message to the topic in SAP Event Mesh");
messagingService.emit("sap/taskmanager-events/event-mesh/user-registration-topic", payload);
}
}
- The message is pushed to SAP Integration Suite REST API and finally you should receive a text message in your phone .
Conclusion
I tried to bring all the pieces of the puzzle together in this blog to complete a seamless integration using SAP
Business
Technology
Platform .
As always, there are lot of areas to explore and learn. So, Stay curious! Keep learning!
To get more updates about this topic follow the below pages
- Blogs
- Q&A
Feel free to “like“, “Share“, “Add a Comment” and to get more updates about my next blogs follow me – avinash.vaidya