Calling REST API with Action

The purpose of this Action example is to illustrate how to use an Action to call a web service. In this example we will query the Perfion API to get a JSON API token for further querying, see Perfion API Reference for more information about the Perfion JSON REST API. 

This example is provided as inspiration only. It must be adapted to your Perfion environment to work. Perfion Support does not assist with such adaptations. Please contact your Perfion Partner or your Perfion consultant if you need assistance.

To use this Action example you need to have the Perfion API installed and configured. You will also need to SHA256, Base64 encode your Perfion password, this can be done by creating a script to do so yourself or using an online tool. 

The example here can be execute in another action using the EXECUTE.ACTION command to retrieve the JSON API token for further querying in Perfion. 

A set of API actions for qurying Perfion can be found for importing here: 

Sample data for building Action Map

Data must be adapted to your Perfion enviroment to work.

Action Map - From

To

Command

Action

Script

Note

 

 

SET

 

 

 

 

$body

 

 

 

The JSON body if you're doing a POST request

http://localhost:8080/token?

 

 

 

 

The URL of the API you want you call

GET

$type

 

 

 

The request type

 

$params

 

 

 

URL params of the request. In this example:

username - Perfion user ID

password - SHA256, Base64 encoded Perfion password

 

 

EXECUTE.SCRIPT

 

string url = Make( "{=$API}" ) + Make( "{=$params}" ); string requestType = Make ( "{=$type}" ); Sources["url"] = url ; string postData = Make("{=$body}"); string ret = ""; StreamWriter requestWriter; var webRequest = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest; if (webRequest != null){ webRequest.Method = requestType ; webRequest.ServicePoint.Expect100Continue = false; webRequest.Timeout = 20000; webRequest.ContentType = "application/json"; //POST the data. if(webRequest.Method.Equals("POST")) { using (requestWriter = new StreamWriter(webRequest.GetRequestStream())) { requestWriter.Write(postData); } } } System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)webRequest.GetResponse(); Stream resStream = resp.GetResponseStream(); StreamReader reader = new StreamReader(resStream); ret = reader.ReadToEnd(); Sources["_response"] = ret;

Uses C#'s internal System.Net library to do a HTTP request at the specified API. 

If type = POST the $body will be used as request body.

Return the JSON response as a new data source "_response"

_response

resp_table

SELECT.JSON

 

 

Selects the JSON response and transforms it into a Perfion neutral data table. 

Depending on the API and it's response you might need to configure this Command, see Perfion Action Mapping

resp_table

resp_table

SELECT.UNIQUE

 

 

Perfions a select unique on the formatted response to make it possible to query in a calling action using 

Sources.FindValue("token","key","token");

 

key

 

 

Value = "Key";

 

access_tokebn

token

 

 

 

 

expiry_date

exp_date