Connecting to Insights via 3rd Party Tools

Overview

Insights is protected by the Viare permission system, which requires specific configuration to be easily used via 3rd party tools.

It is recommended that a specific user is setup in Viare to grant permission to the Insights API.

Connecting to Insights programmatically is generally specific to the language or tools being used, some examples are provided here for convenience.

ℹ️

Note: The eStar eCommerce Connector for Power BI Desktop does this for you - allowing you to simply provide your website URL, with suitable credentials via the Power BI user interface.

PowerShell Example

# Login.
$Body = @{
    Username = '[username]'
    Password = '[password]'
}
$LoginResponse = Invoke-WebRequest 'https://{client}.viare.io/manage/security/login' -SessionVariable 'Session' -Body $Body -Method 'POST' -ContentType 'application/x-www-form-urlencoded'

# Retrieve Data.
$Metric = Invoke-WebRequest 'https://{client}.viare.io/manage/insights/metric/duration' -WebSession $Session

# Output.
$Metric.Content 

.NET Example

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // Define your credentials and client name
        var username = "[username]";
        var password = "[password]";
        var client = "[clientName]"; // Replace with your client name

        // Create a CookieContainer to automatically handle cookies between requests
        var cookieContainer = new CookieContainer();
        var handler = new HttpClientHandler { CookieContainer = cookieContainer };

        // Create an HttpClient instance with the handler
        using (var httpClient = new HttpClient(handler))
        {
            try
            {
                // 1. Login to the API
                var loginUrl = $"https://{client}.viare.io/manage/security/login";
                var loginBody = new StringContent($"Username={username}&Password={password}", System.Text.Encoding.UTF8, "application/x-www-form-urlencoded");

                var loginResponse = await httpClient.PostAsync(loginUrl, loginBody);
                loginResponse.EnsureSuccessStatusCode();

                // 2. Retrieve Data
                var metricUrl = $"https://{client}.viare.io/manage/insights/metric/duration";
                var metricResponse = await httpClient.GetAsync(metricUrl);
                metricResponse.EnsureSuccessStatusCode();

                // Read and output the content
                var metricContent = await metricResponse.Content.ReadAsStringAsync();
                Console.WriteLine(metricContent);
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"An error occurred: {e.Message}");
            }
        }
    }
}

Direct via HTTPS

For other systems to connect, the first step is to ensure a valid Viare Security Context can be sent via an HTTP Cookie for all requests.

This cookie value needs to be obtained by making a call to the security API;

A POST needs to be made to https://{client}.viare.io/manage/security/login

With the following details; Content-type: application/x-www-form-urlencoded Content: Username=[username]&Password=[password]

A JSON message will be sent back containing detail on the result of the call.

If successful, a cookie named Context will be sent back, and needs to be stored and passed on any request to the Insights API.

Some tools (E.g. PowerShell - as above) allow you to retain the session or context between requests, simplifying the process.

If session persistence is available, then it is highly recommended that this is used, as this reduces the number of authentication sessions required, and simplifies the process.

Whilst some tools will do this automatically, others may need specific setup or scripting to facilitate.