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 my example.
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
public const string XAUTH_ACCESS_TOKEN = "/oauth/accessToken";
This oauth_access_token is required for successful authentication to service.belladati.com. It is implemented in BellaDatiConnection.cs.
You have to authenticate as a valid user. 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. GET method for REST API
After the authentication you have an access token and you can GET reports, dashboards, views etc. from the Bella Dati using REST API. For example /api/reports . All REST API you can find in this documentation 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.
I recommend surround the function connection.doGet with try catch see example.
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.
You can get some chart as image too. See example.
string data = connection.doGet("/api/reports/views/47685-XtFJ2yl9DX/image");
The response of this Rest API is png image. It will save it to C:\chart.png and than you can load it to you pictureBox see below.
var uri = new Uri(@"C:\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 is here…