How to use Dummy Upload API?
When to use Dummy Application API?
Dummy Application API keeps a copy of your uploaded data in the Dummy web app so that you can return to it at any time and analyze the results visually. You should use this API if you want the data to be preserved in the web app.
How to work with the API?
This code snippet is written in C# and it is used to upload data to the Dummy Application API and get results from the API. The program uses the following libraries:
IdentityModel.Client
Newtonsoft.Json
API_Demo_Client.Models
System.Text
Here are the steps to follow.
Step 1: Generate a token
Generate a Bearer token to use with this endpoint. Follow the instructions given in How to generate a Bearer token? tutorial to generate a bearer token.
Step 2: Upload the data
Follow the instructions below to upload the data. This code sends HTTP POST
request to a web API endpoint.
Step 1: Set the variable values
Set the values of apiKey
, accessToken
, CSV
, and clientId
variables.
var apiKey = "[Your_APIKey]";
var apiUrl = "https://eu.example.com";
var accessToken = "[Your_Accesstoken]";
var csv = File.ReadAllText("Samples\\ExampleUpload.csv");
var clientId = "[Your_ClientId]";
You can get apiKey
and clientId
from the web app, and you can get the accessToken
from the previous step.
The csv
variable is set to read the data from a CSV file. Set the parameter value of the File.ReadAllText()
function to the location of the CSV file with the data.
Step 2: Initiate request
Create an HTTP client and set the base address to the API URL. Then, create an HTTP request message with the HTTP method set to POST and the endpoint set to /api/UploadData
. The request message should also include the authorization
header with the access token.
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(apiUrl);
var request = new HttpRequestMessage(HttpMethod.Post, "/api/UploadData");
request.Version = new Version(2, 0);
request.Headers.Add("authorization", $"Bearer {accessToken}");
Step 3: Prepare request body
Create an instance of the UploadRequest
class and sets its properties.
var uploadRequest = new UploadRequest
{
Name = "Sample Dummy Application API post",
Data = csv,
Username = clientId,
Key = apiKey,
Overwrite = false
};
The UploadRequest
class has four properties.
Name
Data
Username
Key
Provide a name for the through the Name
property. In the following code example, the Name
property is set to Sample Dummy Application API post
.
Set the Data property to refer to Csv
and the Username
property to clientId
variables. The Key
property must refer to apiKey
.
Leave the Overwrite
property as false
, if you want to create a new Dum Dum. To overwrite an existing Dum Dum with the same name, set the value of the Overwrite
property to true
.
Step 4: Make the request
Serialize the UploadRequest
object to JSON and set the request message content to the serialized JSON. Send the request message to the API and wait for it to return a Dum Dum token.
request.Content = new StringContent(JsonConvert.SerializeObject(uploadRequest), Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
Step 3: Generate result
Follow the instructions below to upload the Dum Dum data. This code sends HTTP GET
request to a web API endpoint.
Step 1: Set the variable values
Set the values of DumDumToken
and dataKey
variables.
You can get DumDumToken
from Step 4: Make the request. The dataKey variable is set to status
initially to ensure that the data you uploaded previously was completed successfully. See Step 4: Retry with different dataKey
Step 2: Initiate request
Create a query string using the variables set in Step 1: Set the variable values, including the apiKey
variable. Then, create an HTTP request message with the HTTP method set to GET and the endpoint set to /api/GetData
. The request message should also include the authorization
header with the access token.
Set the Version property of the HttpRequestMessage
object is set to 2.0
to indicate that the request should use HTTP/2.0
.
var queryString = string.Format("?token={0}&key={1}&dataKey={2}", DumDumToken, apiKey, dataKey);
var request = new HttpRequestMessage(HttpMethod.Get, "/api/GetResults" + queryString);
request.Version = new Version(2, 0);
request.Headers.Add("authorization", $"Bearer {accessToken}");
Step 3: Make the request
Use the SendAsync
method of the HttpClient
object to send the request, and the ReadAsStringAsync
method of the HttpContent
object to read the response content as a string.
Step 4: Retry with different dataKey
There are two workflows for this step depending on the response you receive from the previous step.
If the response from the previous step is not completedsuccessfully
, set the value of dataKey
parameter to retry
in Step 1: Set the variable values and retry sending the request to learn more about the problem.
Resolve any error in the data file. Then, return to Step 2: Upload the data. Keep the Name
same and set the value of Overwrite
field to true
to resubmit the data.
If the status is completedsuccessfully
, set the value of dataKey
parameter to the type of result you want to receive and submit. The following values are supported for the dataKey parameter. Send only one value at a time.
- status
- retry
- schedule
- timeabovethreshold
- tableofDumDumdetailsids
Complete Code
using IdentityModel.Client;
using Newtonsoft.Json;
using Dummy_API_Demo_Client.Models;
using System.Text;
namespace Dummy_API_Demo_Client
{
public static class UploadApi
{
public static async void Run()
{
var identityUrl = "https://identity-eu.example.com";
var apiUrl = @"https://eu.example.com";
var apiKey = "[Your_ApKey]";
var clientId = "[Your_ClientId]";
var clientSecret = "[your_ClientSecret]";
var csv = File.ReadAllText("SampleDumDum\\ExampleDumDumUpload.csv");
var accessToken = await Authenticate(identityUrl, clientId, clientSecret, "Dummy_upload_api");
if (accessToken == null) { return; }
var DumDumToken = await Upload(apiUrl, apiKey, accessToken, csv, clientId);
Console.WriteLine("Acquired token: " + DumDumToken);
while (await GetResults(apiUrl, apiKey, accessToken, DumDumToken, "Status") == "processing")
{
Console.WriteLine("DumDum still being processed....");
Thread.Sleep(2000);
}
var feedback = await GetResults(apiUrl, apiKey, accessToken, DumDumToken, "Retry");
Console.WriteLine("Retry: {0}", feedback);
var schedules = await GetResults(apiUrl, apiKey, accessToken, DumDumToken, "TableOfDumDumDetailsIds");
Console.WriteLine("Schedules: {0}", schedules);
Console.WriteLine("Downloading schedule data");
var schedule = await GetResults(apiUrl, apiKey, accessToken, DumDumToken, "Schedule");
File.WriteAllText("Output\\scheduleData.csv", schedule);
Console.WriteLine("Downloading time above threshold data");
var timeAboveThreshold = await GetResults(apiUrl, apiKey, accessToken, DumDumToken, "TimeAboveThreshold");
File.WriteAllText("Output\\timeAboveThresholdData.csv", timeAboveThreshold);
}
static async Task<string> Upload(string apiUrl, string apiKey, string accessToken, string csv, string clientId)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(apiUrl);
var request = new HttpRequestMessage(HttpMethod.Post, "/api/Upload");
request.Version = new Version(2, 0);
request.Headers.Add("authorization", $"Bearer {accessToken}");
var uploadRequest = new UploadRequest
{
Name = "Sample DumDum API post",
Data = csv,
Username = clientId,
Key = apiKey,
Overwrite = false
};
request.Content = new StringContent(JsonConvert.SerializeObject(uploadRequest), Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
static async Task<string> Authenticate(string identityUrl, string clientId, string clientSecret, string scope)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(identityUrl);
var openIdDiscoveryDocument = client.GetDiscoveryDocumentAsync(identityUrl).GetAwaiter().GetResult();
var request = new ClientCredentialsTokenRequest
{
Address = openIdDiscoveryDocument.TokenEndpoint,
ClientId = clientId,
ClientSecret = clientSecret,
Scope = scope
};
var tokenResponse = await client.RequestClientCredentialsTokenAsync(request);
if (tokenResponse != null)
{
return tokenResponse.AccessToken;
}
else
{
Console.WriteLine("Authentication failed");
throw new Exception("Authentication failed");
}
}
static async Task<string> GetResults(string apiUrl, string apiKey, string accessToken, string DumDumToken, string dataKey) {
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(apiUrl);
var queryString = string.Format("?token={0}&key={1}&dataKey={2}", DumDumToken, apiKey, dataKey);
var request = new HttpRequestMessage(HttpMethod.Get, "/api/GetResults" + queryString);
request.Version = new Version(2, 0);
request.Headers.Add("authorization", $"Bearer {accessToken}");
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
}
}