A sample of the C# Api helper class accessing the API can be downloaded here:
The API uses Swagger/OpenAPI, because of this, you can easily generate a helper class, that wraps the Http calls into a nice class with methods.
Create a console project in visual studio
Select File->New->Project:
Select the Console App and click Next:
Name your project and click Next:
Select your Framework and click Create:
You should see a hello world program like this:
In our example, we used the .NET 5 hello world template:
Create service reference to Api.
Right click the your project, select Add->Service Reference:
Select OpenAPI and click Next:
Now the service reference needs the url for our swagger.json file. It is:
(Api Url)/swagger/v1/swagger.json
You can find the url here:List of API Urls
Example:
Test API: https://testapi.diap.online/swagger/v1/swagger.json
Live API: https://api.diap.online/swagger/v1/swagger.json
You can also visit the API website and click this link:
To get the Url:
Now select Url and paste the API url with the swagger.json:
Set the namespace for the class, that will be generated (The ApiHelper)
And set the class name for the class, that will be generated (The ApiHelper)
Click Finish:
Now you class helper will be generated:
Click Close:
You can view the generated class here:
The class file is created in the obj folder of the project:
Using the ApiHelper class:
You need 3 things to use the ApiHelper:
UserName
Password
BaseUrl of API
Clear the main function in your project and insert the following code, to setup the information and create an object of the helper class:
var userName = "<username>";
var password = "<password>";
var baseUrl = "<baseUrl>";
var http = new HttpClient();
var apiHelper = new DIAP.ApiHelper(baseUrl, http);
In our Example:
Now add a try catch block:
try
{
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
In our Example:
Next you need a jwtToken to be able to use, the protected functions. Call the LoginAsync method with the username and password:
var jwtToken = apiHelper.LoginAsync(new DIAP.LoginCredentials { Username = userName, Password = password }).Result;
//Remove enclosing quotation marks from token
jwtToken = jwtToken.Replace("\"", "");
Console.WriteLine($"Login Token: {jwtToken}");
In our Example:
And you need to set the Authoriaztion header to the Bearer schema and provide the jwtToken:
//Set authorization header for following calls
http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", jwtToken);
In our Example:
Now you have access to the protected functions.
Here is an example of getting the unitList:
//Get unit list
var unitList = apiHelper.ListUnitsAsync().Result;
foreach(var unit in unitList)
{
Console.WriteLine($"Id: {unit.Id}, Name: {unit.UnitName}, Timezone: {unit.TimeZoneId}");
}
Getting the project List:
//Get product list
var productList = apiHelper.ListProductsAsync().Result;
foreach(var product in productList)
{
Console.WriteLine($"Id: {product.Id}, Name: {product.Name}, Uom: {product.Uom}");
}
Setting a order on a unit:
//Set order information
var OrderInfo = new DIAP.SetOrderInformation
{
UnitId = unitList.First().Id ?? -1, //The -1 is just for the intellise, it shouldn't happen
ProductName = productList.First().Name,
OrderId = $"{DateTime.Now.ToString("yyyyMMddHHmm")}-API"
};
apiHelper.SetOrderAsync(OrderInfo).Wait();
//If no exception is thrown, then the order is set
We have added a ReadKey at the end of the program:
Console.ReadKey();
Here is the complete code example:
using System;
using System.Net.Http;
namespace WebApiHelperTester
{
internal class Program
{
static void Main(string[] args)
{
var userName = "Apitest@diap.online";
var password = "Apitest-1234";
var baseUrl = "https://testapi.diap.online";
var http = new HttpClient();
var apiHelper = new DIAP.ApiHelper(baseUrl, http);
try
{
var jwtToken = apiHelper.LoginAsync(new DIAP.LoginCredentials { Username = userName, Password = password }).Result;
//Remove enclosing quotation marks from token
jwtToken = jwtToken.Replace("\"", "");
Console.WriteLine($"Login Token: {jwtToken}");
//Set authorization header for following calls
http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", jwtToken);
//Get unit list
var unitList = apiHelper.ListUnitsAsync().Result;
foreach(var unit in unitList)
{
Console.WriteLine($"Id: {unit.Id}, Name: {unit.UnitName}, Timezone: {unit.TimeZoneId}");
}
//Get product list
var productList = apiHelper.ListProductsAsync().Result;
foreach(var product in productList)
{
Console.WriteLine($"Id: {product.Id}, Name: {product.Name}, Uom: {product.Uom}");
}
//Set order information
var OrderInfo = new DIAP.SetOrderInformation
{
UnitId = unitList.First().Id ?? -1, //The -1 is just for the intellise, it shouldn't happen
ProductName = productList.First().Name,
OrderId = $"{DateTime.Now.ToString("yyyyMMddHHmm")}-API"
};
apiHelper.SetOrderAsync(OrderInfo).Wait();
//If no exception is thrown, then the order is set
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
In our example, we used the ApiHelper in a console project, but it could have been used in a web or liberary project.