Thursday, December 2, 2021

Invoking a REST Connector Using Datapage: Pega How-to Guide

Technically, we could also use activity to invoke the REST service for testing. However, it is easier and better to use Datapage. Datapage provides options for you to cache the data, it also makes it much easier for testers and new users of Pega to use.

 

Datapage Configuration

The following shows the created datapage:


Figure 74: Datapage Used For Testing REST Service

Since the REST service that we created earlier is taking in a “name” parameter, let’s configure this datapage to take in the “name” parameter too.

 

Figure 75: Parameters Tab of the Datapage Used for Testing REST Service

Under the “Definition” tab, scroll down to configure the “Data sources” as follows:

 

Figure 76: Configured the Data Sources as a REST Connector

In the above, we had selected as follows:

·       Source: Connector

·       Type: REST

·       Name: DBRestConnector (which is the REST connector that we created earlier.)

·       Request Data Transform: DBRestConnectorPOSTRequest

·       Response Data Transform: DBRestConnectorPOSTResponse

·       Method: Post

 

💡

Note: For both the “Request Data Transform” and “Response Data Transform”, after you have given it a name, you can click on the icon on the right. Pega will guide you to create those rules, with some sample steps provided for you.

 

Also, for each of the “Parameters” links, as shown below, click it one-by-one and tick the “Pass current parameter page”.

 

Figure 77: Propagating the Parameters to the Downstream Rules

💡

Note: Ticking the “Pass current parameter page” saves the trouble of manually entering the values for each of the parameters, however, you need to ensure that all parameter names that are downstream must have exactly the same name.

 

Request Mapping Configuration

With the “Request Data Transform” field entered as: “DBRestConnectorPOSTRequest”, click on the icon on its right.

 

This will create the given rule for you. In this “DBRestConnectorPOSTRequest” mapping rule, we need to set the request that we would be passing over to the REST service via the “Param.JSON” parameter, using the JSON format.

 

Parameters Tab

Firstly, in the “Parameter” tab, define the “name” parameter as follows:

 

Figure 78: Defining the Parameters for the Request Data Transform

Definition Tab

In the definition tab, we just need to do 2 steps:

1)    Set the “name” to be the parameter being passed in

2)    Convert the request page containing the “name” parameter to “Param.JSON

 

The following shows the completed form:


Figure 79: Preparing the Request JSON for the Test Datapage

💡

Note: The function that is called in the 2nd line is as follows:

               @pxConvertPageToString(tools, .MyRequest, "json").

 

💡

Note: Recall that the “.MyRequest” was defined earlier as a ‘member variable’ of the “DB-DBRest-Int” class? That is why we could use that temporarily for data manipulation and using it for converting the information to “Param.JSON” for sending.

 

Response Mapping Configuration

Back in the “D_DBRestConnectorPOSTdatapage, click on the icon on the right of the “Response Data Transform” field, this will create the given rule for you.

 

💡

Note: The name entered above is “DBRestConnectorPOSTResponse”.

 

💡

Note: By default, Pega creates some boilerplate codes for you. These include a “Datasource” page (as defined in the Parameters tab, that contains the returned info, as well as some codes to show you how to initiate a mapping. It also contains some error handling templates.

As we are using “Param.JSON”, we do not need to use the “Datasource” page. For the error handling, we will discuss it in the Master Beaver Version.

 

 

 

 

 

Definition Tab

The following shows the mapping in the response data transform:


Figure 80: Preparing the Response JSON for the Test Datapage

Basically there is only 1 step, which is as follows:

@pxConvertStringToPage(tools, .MyResponse, Param.JSON,”json”)

 

This line basically converts the “Param.JSON” that is passed to us from the REST service, into the “.MyResponse” page.

 

💡

Note: The Step 2 above is actually disabled by default. Again, we will talk about that in the Master Beaver Version later.

 

Running the Datapage

OK, that is all that is required! Ensure that you have saved all the rules, and let’s do a test now!

 

Back in the “D_DBRestConnectorPOSTdatapage, click on “Actions”, followed by the “Run” menu item.

 

Figure 81: Invoking the REST Testing Datapage

In the popup dialog box, enter “Debunkum Beaver :-)”, and then click the “Run” button.

 

In the output, expand the “MyResponse” element and you will see that the output “WelcomeMsg” and the “YourName” are populated accordingly!

Figure 82: Output from the Testing of REST Service Using Datapage

Wow, it is cool and simple, isn’t it? 😎

 

In the Master Beaver Version, we will drill into more details!