Google distance API in Pega
Pega provides easy implementation and integrating several available API to make the development easy and faster. One such API "Distance Matrix API" to find the distance between two geological places.
To use google distance matrix API create a google cloud account using Gmail Account.
After signing up to your google cloud platform either use your existing project or create a new project.
1. Click on the hamburger icon select APIs & Services --> Dashboard. search for the distance matrix API.
2. On selecting the API you will see a brief description of the API and its overview. Click enable API to use the API for your project.
This will take a few minutes to enable the API for your project. On successful adding API to your project, you will see the landing page of the particular API with google provided analytics to the API.
You will need an API key (Secret Key) provided by the google cloud platform so that the google cloud platform can identify the request from the user. Users need to be identified so that the cost per request can be calculated.
3. To create API key specific to the particular API select APIs & Services --> Credentials
4. Credentials dashboard will show you the set of keys that you have created for the selected project. Now we need to create API Key to use it for the distance matrix API. Click Create Credentials to create a new API secret Key.
5. Copy the API key and store it in a safe place. In the future, this key will be used in our Pega environment to send/receive requests.
6. Now a unique key has been generated. for security purposes, we need to restrict the key to be used only for the "DistanceMatrix API". to restrict key click on the restrict key and select the restrict key radio button and select the enabled API from the dropdown.
This will take quite a few minutes to restrict. After this, in your API credentials dashboard, you will see the key with restriction to the distance matrix API.
7. Navigate to APIs & Services --> Dashboard search for the distance matrix API.
8. In the DistanaceMatrix API Dashboard navigate to Credentials Tab. On restricting the API key recently created key will visible under Credentials Tab. So then this API key will only be used for Distance Matrix API.
Google cloud platform will charge for the API based on the number of requests received. Google Cloud will provide you with the charge-free request up to some basic limits. Price charge quotas are available under the Quotas tab.
9. Create a billing account with the necessary payment information. To create billing account click on Hamburger Icon--> Billing.
Follow the instructions and create a billing account associated with payment information.
10. After adding the payment process your project will have the payment details. We have to map the added payment information to the project that we are currently working with the API to do that, select Billing --> Manage Billing.
11. Under My projects tab select your project and add the recently created billing account.
It's time to configure the endpoint URL for the distance matrix
Endpoint URL comes with different forms of input you can refer the above link to learn more. We have used the ULR with JSN as a format.
Units -- Imperial [ Kilometers & Meters ] / Static [Miles & Feet].
Origins - From Location.
Destination - To Location.
Key - API key generated from the console.
HIGH-LEVEL STEPS TO CONSUME REST API IN PEGA
Creating REST connector rule along with datatype to hold response
Data page sourced with REST Connector
Use Data page in UI to retrieve the result
1. Click configure --> Integrations --> Connectors --> Create REST integration.
Enter the name of the REST connector as “calculate Distance” and paste the endpoint URL.
2. Now Pega will do the magic of automatically separating the query parameter and Resource Path parameter.
3. Click Next screen to choose the URL request type.
4. Enter the resource name for the Resource methods Use the POST method, to the resource URL.
5. Click next.
In this screen, we need to provide the data type to acquire the response received from the URL.
Pega provides a setting to map response datatype by adding a sample REST response or a JSON file.
In this scenario, the REST response will take care of all the datatype & data model required to capture the response.
6. Click Add a REST response to attach sample response.
7. On entering the key with specified data in the parameter and click run. Pega will show the responses returned back by the URL.
8. Now click submit and click next.
Pega will automatically create two classes.
O2EWJQ-IAPPS-Int-CalculateDistance [Integrations configurations are stored under int class]
O2EWJQ-IAPPS-Data-API [DataModel required to capture response are stored under Data class]
The DataModel REST Wizard will get created with a data page sourcing connect REST rule.
9. After creating Connect REST navigate to the data class records to access the data pages created with sourced the connector.
10. We can call this data page anywhere in the section to fetch the responses based on the parameters passed.
11. Create a section in O2EWJQ-IApps-Data-Distance class to access the properties used for the response.
.rows(1).elements(1).distance.text --- Property referring the Distance
.rows(1).elements(1).duration.text --- Proerty referring the Duration
NOTE: If you need to know the properties holding the response data we can run the data page to get the structure of response received from the request.
12. We need to use this section in our case type so create a case type DistanceCalculation.
13. Refer the section to your desired work class with data page as source as mentioned in the screenshot.
14. Create SourceAddresss, DestinationAddress two properties to pass the Location to the data page.
15. For GetDistance Button configure the action set OnClick --> Refresh Section to trigger the data page with the necessary parameters.
Let us test it to see if it works.
Check the demo video for Results:
HAPPY LEARNING !!