With this tutorial we aim to show you how to build .NET application using BellaDati analytics features through REST API. Main focus is on

      • OAuth authentication with BellaDati using xAuth method
      • BellaDati REST API calls
      • Processing of REST API (JSON and non-JSON format) 
      • Handling of BellaDati HTTP errors

We will use C# programming language to allow us to experiment lavishly with our code. 

Tutorial pre-requisites are

      • BellaDati application installed on your machine or cloud (min.version 2.7.15.3)
      • REST API enabled in your BellaDati application's domain
      • BellaDati data warehouse should contain at least 3 datasets and reports in your domain
      • Visual Studio for programming in C#

 

Firstly, we need to create a project in Visual Studio. It is WPF app in ourexample.

 

Paste the BellaDatiBase.cs and BellaDatiConnection.cs to your solution.

After that, you can start with writing a code to the MainWindow.xaml.cs. And you can create your xaml, base to your fantasy.

1. Obtain accessToken from BellaDati REST API

Appropriate BellaDati endpoint for getting the oauth_access_token is defined as follows:

public const string XAUTH_ACCESS_TOKEN = "/oauth/accessToken";

 

This oauth_access_token is required for all consequent calls against BellaDati Platform. It is implemented in BellaDatiConnection.cs.

In this example, let's assume that we have set up a domain with a consumer key mykey and a consumer secret apisecret. These keys set up to your App.config see below.

<appSettings>
    <add key="consumerKey" value="mykey"/>
    <add key="consumerSecret" value="apisecret"/>
</appSettings>

Now you can connect to our server and authenticate:

BellaDatiConnection connection = new BellaDatiConnection("https://service.belladati.com");
connection.xAuthGetAccessToken(textBoxName.Text, pswdBox.Password);

2. Access REST API using HTTP GET method

After the authentication you have an oauth_access_token and you can access all available protected REST API resources, e.g. GET reports, dashboards, views etc. Here is an example for GET Reports. All REST API you can find under REST API Resources.

 

Call function doGet(url) from your created instance connection.
string data = connection.doGet("/api/reports");

 

If you have some created report, this function will return data from REST API in JSON see below.

 

We recommend to surround the function connection.doGet with try catch clause as shown in example below.

 	try
    {
      connection.xAuthGetAccessToken(textBoxName.Text, pswdBox.Password);
      string data = connection.doGet("/api/reports/47685");
      textBlockResponse.Text = data;
    }
	catch (Exception ee)
    {
      textBlockResponse.Text = ee.Message;
    }

 

So If there is some error you will see it in some error box.

As a next step, you can display some chart as image:

string data = connection.doGet("/api/reports/views/47685-XtFJ2yl9DX/image");

The response of this REST API is png image. It will save it to chart.png and than you can load it to you pictureBox see below.

  var uri = new Uri(@"chart.png");
  var bitmap = new BitmapImage(uri);
  pictureBox.Source = bitmap;
    

If you want to change the path of image, you can do it in class BellaDatiConnection.

 

 

 

The whole example includes the classes you need...

  • No labels