Saturday, February 17, 2018

How to expose REST Service from Pega

Requirement:
  • Create a data type in your application which has  10 data instances
  • This data needs to be exposed as Service-REST to the outside world. 
  • When user passes CustID via request to Pega Service, we need to parse the request and open that recod (which is an instance of below Data type) and send that record details in JSON as response to the client. 
Example: 

CustID would be passed in the request to Pega Service. In response: Customer Details to be sent from Pega Service.

/*Upcoming Post : Understanding of Pega Service Architecture : Service Listener, Service Package, Service REST, Parse Request, Process request: Service Activity, Generate response*/

Creating Data type in application with local storage of  10 records : 

Create a data class with the properties(columns) required. In this example we are creating at framework data layer so that it can be re-used across all the implementations of the framework. 

Class Name : Tata-FW-DemoFW-Data-Customer




Navigate to Data explorer, Create Add data type.




Navigate to Records and select one column as key (CustID in the below example). And proceed with the next steps. Data Pages would be created, which is not in-scope of this post. 










Below is how it looks once you are done with adding all the 10 records. Local storage option here would serve the purpose.


Now we are good with the data, and we can proceed to configuration of hosting a Pega Service. 

Step by Step analysis and design:

#1 First and foremost, we can note down the below points.

Class Name : Tata-FW-DemoFW-Int-GetCustomerDetails
RuleSet Name: DemoFWInt
RuleSet Version: 01-01-01

Note: We are creating in framework layer int class, so that this service can be used across all the applications implementing this "DemoFW" framework of Tata Organization. 

#2 Data Mapping:

Class for request:Tata-FW-DemoFW-Int-GetCustomerDetails-Request
Class for response:Tata-FW-DemoFW-Int-GetCustomerDetails-Response






















#3 Create service package:Transport

Navigate to integration class, right click >> Create >> Integration-Resources >> Service Package

Provide Service Package name, and proceed. It would take us to the below screen, where we need to enter Service access group. We can choose the Access group, which would provide service requestor, access to the rules to be run during service invocation. 
Click Save and proceed to next steps.

Note: Service Package is data instance which should be explicitly added to RAP.


#4 Create service REST rule (method):GetCustomerDetails

Navigate to our integration class rule in app explorer >> Right Click >> Create >> Integration-Services >> Service-REST 

//Input the the below details : 
 Service name: GetCustomerDetails
 Service Package name: Transport // created in step 3
 Service Version: V1  // first version
 URI Template: we can give our URI template // this example is not having any resource params


Click on "Create and Open".
Here you would need to enter the class name as per your data model. In this example, page class is : Tata-FW-DemoFW-Int-GetCustomerDetails

Endpoint UR woud take below syntax :
http(s):envdomain:portnumber/prweb/PRRestService/{ServicePackageName}/{Version}/{ServiceName}

Note: In previous versions, instead of Version, Class used to be there. Actually now, it makes more sense with the version.


#5 Create service activity:GetCustomerDetails
This is the actual place where we write the business logic to fetch the details from our "data type".
From logic, we can assign the requried values to populate on MyServicePage >> output data mapping structure. Comments to the below steps are self-explanatory.



#6 Method suiting requirement (GET/POST/PUT/DELETE) :GET
In this example, we have to do simple GET operation, which would fetch the record details of Customer. So we went with the method : GET.
Examples for POST, PUT, DELETE are included in upcoming posts. 

Provide Service Activity name from step#5 in the Methods tab of Service REST. 


#7 Request Mapping : We don't have request body in this example, we have a query string parameter.

Note: CustID is the query string parameter that we are mapping to .Request.CustID
If we have more to add in the request, better to go for POST method, with JSON itself.
Why POST would be better than GET, we would explore it in other post to keep this simple.


#8 Response Mapping

Map From : JSON //we need JSON formatting for response, so selected the same
Map From Key : Response attribute //from our data mapping.



#9 Run the service REST rule standalone to pass request and check the response.
Go to Service REST rule >> Actions menu >> Run









And then choose the method type : GET // from our example
Pass Query string parameters if you have any // CustID as 7 in our example








It would open up a new window to show the status of service with response.





While debugging, its good to do the below steps before taking other routes:
  • Trace Service Rule while running service REST rule stand-alone
  • Capture Request and Response










We got the desired output, as per the business logic:











JSON stands for JavaScript Object Notation.
Here each Customer is an Object. And JSON will be in the above format with "key":"value" pairs wrapped in an entity.

Now we have exposed our PRPC Service to the outside world. To give a try,you can use other REST clients to test our service endpoint.

We have covered the steps in deploying a service from Pega end, and exposing it step by step.

Hope this helps, feel free to comment.


Thanks!!!


10 comments:

  1. Really very good blog buddy.could you please show the activity steps in detail. Please upload the deployment(like heroku or Google cloud etc) steps also. Thanks

    ReplyDelete
    Replies
    1. Thanks Vidyasagar. I would share the activity steps in detail. And it's good idea on uploading deployment, I would explore on that.

      Delete
  2. I have created Service Rest and success. When I run to simulate the service request is success also. But when I try to access the service through Postman, I don't receive response (the response is blank). Can you teach me how to access Pega service rest from external ? Thank you.

    ReplyDelete
  3. How to create Request and response
    properties in Tata-FW-DemoFW-Int-GetCustomerDetails this class

    ReplyDelete
    Replies
    1. Right click on the class and navigate to Data Model >> Property and then click on create. While creating select the option as page format and give the page name.

      Delete
  4. Sir, I have a scenario where I have to display details of property from intended data page when I hover on that property.Which action I can use for this?

    ReplyDelete
    Replies
    1. Hi Monika,
      Once you add property to section. Navigate to Actions tab. Add an action>> On Hover => Show Smart tip and provide the details of your source property. Once you reach there, it would be easy to achieve it.

      I have added one post for this as I received similar requests from other too: https://www.howtopega.info/2020/05/hover-mouse-over-functionality-to-show.html

      You can comment there based on how it goes for you.

      Delete
  5. Hi Pavan,
    I tried the same scenario but in the out put is empty, Plese help me how to solve the issue

    ReplyDelete


HowToPega : All rights reserved and the contents are copyrighted to Pavan Kumar Naidu