Connect Rest with Clash of clans Part 2

In the Previous Post Part 1, we saw how to get the API key from clash of clans and use it in postman to get the response.

In this Post , we will use the details from postman and create a integration in Pega.

Integration was always a difficult topic for me , there is so many topics around it (authorization, authentication, data model , integration layer ,connection time out, certificate failure) which makes it look like a nightmare. All the terms which developers use for while working integration like Parsing , converting Json/XML , Request DT , Response DT , Mapping makes it more complicated to understand the topic.

 I am going to learn one at a time and write a post about it for my future reference here. This Post will help me to understand the basic of integration and how Pega helps in creating the most of the rules required for integration.

 

Why do we need integration ?

For example : Consider two web applications which is already built in Java and Python. Now if you need data from One application to another which does not understand each other, then Integration is the way to connect to these two systems.

 

There are two types of rules in Pega which helps in integrating two applications. (another confusing terminology)

 

Connector - When you need some data , you create a connector to get the data. (Get Data - connector)

 

Service - when you need to give data , you create a service to give the data.(Give Data - Service)


Which API are gonna be used?

From the list of operations , I have selected two simple operations.

  1.  To get information about all the locations
  2.  To get the information about the player rankings for a specific location (For you to try)

 

Requirement in Pega :

Create two dropdown , one to fetch the list of location . After selecting location , pass the location to get the list of Player ranking

 

First lets create the integration rules then create a section to display to location and player list.

 

Open designer studio and Navigate to Create Rest Integration




There are 4 screens which needs to be completed.

First screen:

Provide the name : COC  ("Clash of clans") and End point URL : https://api.clashofclans.com/v1/locations
Since we are using token as authentication type , we don’t need a separate authentication profile instead we can pass the token as header (reference postman).





Second screen: Provide Resource name and select the method you want to use for this integration. (For eg Get Method , Reference from postman)


Third screen: One of the important screen, where our Data model for our integration is determined. Also we can check if our integration works too.

Two options to add the data model :1) Either you can add the REST response from postman 2) Trigger the integration and get the response .

 

I am going with option 2 , Add a REST response . In the header , Authorization field provide the token copied from postman and click run.

 




Forth Screen : Another important screen , whether the class structure is defined.

Make sure for Integration layer , Int class is selected as Parent class and  the context as Integration ruleset.

Make sure for Data layer , Data class is selected as Parent class and the context as Application ruleset.



If required Preview records and Click create . Please find the list of rules Pega has created below

 

  1. Datapage
  2. Connect Rest
  3. Class required for Integration
  4. Property required for integration
  5. Response Data transform
  6. Request Data transform

Now Provide the dropdown source as Datapage and check if results are populated.





Food for thought : Similarly , Now you can try to create the next integration ( To get the information about the player rankings for a specific location) to pass the location and get the list of players ranking.





Connect Rest with Clash of clans Part 1

 In this Post , We are gonna use API from Clash of Clans and Use Postman to check if API works.

What is clash of clans (COC) ?

Clash of Clans is an online strategy game for mobile. Well if you have no idea how this game is played, in brief, a Clasher has a village and an army. Clashers attack each other’s villages in multiplayer battles, form clans together, participate in wars against other clans, and lead their clans to victory.




How to access the clash of clans API ?

 

Registration an developer account with Clash of clans



Create an API token to access the Clash of clans API

  • Login to clash of clans
  • Navigate to Account name and Select My account
  • Under my account , Create New Key
  • Provide Name , Description and for allowed IP Address check the IP address from https://whatismyipaddress.com
  • click create key and copy the token to a notepad.





To check the list of API provided by clash of clans

From the list of operations , I have selected two simple operations.

  1.  To get information about all the locations
  2.  To get the information about the player rankings for a specific location



Using Postman To check if API works

Create new request and give the End point URL: https://api.clashofclans.com/v1/locations



In the Authorization tab, select the type as Bearer token and provide the token from notepad.


Click send to check if status is 200 (connected successfully) and the response is returned in JSON format.



Food for thought : In the same way, you can use the other API's from clash of clans and use postman to check if API response is received or not.


In the below post , we will see how pega can use the clash of Clans API and display the results.

https://welearnpega.blogspot.com/2021/01/connect-rest-with-clash-of-clans-part-2.html

Convert pfx to jks

This post talks about how to convert pfx file to jks file

There are many ways to convert pfx to jks ,

Here we are going to see two ways out of it .
1) Using Java Keytool
2)Using Jetty

For Using Java Keytool ,

We need either JRE or JDK to be Installed . You can find Keytool folder inside it.

what is Keytool ?

Keytool is a tool used by Java systems to configure and manipulate Keystores.

Generating:
  • Generate a new Java keystore and new key pair:
    • keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -keysize 2048
  • Generate a certificate signing request (CSR) for an existing Java keystore: 
    • keytool -certreq -alias mydomain -keystore keystore.jks -file mydomain.csr
  • Generate a keystore and self-signed certificate:
    • keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048

Importing:

  • Import a intermediate CA certificate to an existing Java keystore:
    • keytool -import -trustcacerts -alias intermediate -file intermediate.crt -keystore keystore.jks
  • Import a root CA certificate to an existing Java keystore:
    • keytool -import -trustcacerts -alias root -file root.crt -keystore keystore.jks
  • Import a signed SSL primary certificate to an existing Java keystore:
    • keytool -import -trustcacerts -alias mydomain -file mydomain.crt -keystore keystore.jks




Java Keytool Commands for Conversion:

If you need to change the type of keystore.
  • PFX keystore to JKS keystore:
    • keytool -importkeystore -srckeystore mypfxfile.pfx -srcstoretype pkcs12 -destkeystore newjkskeystore.jks -deststoretype JKS
  • JKS keystore to PFX keystore:
    • keytool -importkeystore -srckeystore myjksfile.jks -srcstoretype JKS -deststoretype PKCS12 -destkeystore newpfxkeystore.pfx

Java Keytool Commands for Checking:

If you need to check the information within a certificate, or Java keystore, use these commands.
  • Check a stand-alone certificate:
    • keytool -printcert -v -file mydomain.crt
  • Check which certificates are in a Java keystore:
    • keytool -list -v -keystore keystore.jks
  • Check a particular keystore entry using an alias:
    • keytool -list -v -keystore keystore.jks -alias mydomain
Other Java Keytool Commands:
  • Delete a certificate from a Java Keytool keystore:
    • keytool -delete -alias mydomain -keystore keystore.jks
  • Change a Java keystore password:
    • keytool -storepasswd -new newstorepass -keystore keystore.jks
  • Export a certificate from a keystore:
    • keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks
  • List Trusted CA Certs:
    • keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts
  • Import New CA into Trusted Certs:
    • keytool -import -trustcacerts -file /path/to/ca/ca.pem -alias mydomain -keystore $JAVA_HOME/jre/lib/security/cacerts

For Using Jetty , We need to first download the Jetty Jar.

what is Jetty ?

Eclipse Jetty provides a Web server and javax.servlet container, plus support for HTTP/2, WebSocket, OSGi, JMX, JNDI, JAAS and many other integrations. These components are open source and available for commercial use and distribution.

Where do download Jetty ?
https://jar-download.com/artifacts/org.mortbay.jetty/jetty/6.1.25/source-code

Keep the .pfx file and jetty 6.1.25 in one folder

Open command prompt and go until that folder

java -classpath jetty-6.1.25.jar org.mortbay.jetty.security.PKCS12Import <name of pfx file>.pfx <name of jks file>.jks

Once the command is executed enter the password of .pfx file in both input keystore and output keystore

Once it is done.. you can find the .jks file in the same folder that you have the pfx file.



Sample CMD Prompt Code:

C:\Users\WeLearnPega>java -classpath jetty-6.1.25.jar org.mortbay.jetty.security.PKCS12Import UAT.pfx UAT.jks

Enter input keystore passphrase: WeLearnPega
Enter output keystore passphrase: WeLearnPega
Alias 0: le-govnetuser-7729cbd5-0409-41be-b242-f78795bbe307

Adding key for alias le-govnetuser-7730cbd5-0419-42be-b342-f78795bbe307