Sample API script in C#: Get reservations list (read JSON response)
This is an example code to demonstrate in general how to call REST API in C# and read the JSON response.
Here in particular, using the REST API call for retrieving the reservations of specific user: Legacy | Get Reservation List
Adapted from https://github.com/PerfectoCode/Reporting-Samples/tree/master/CSharp/export-api-sample
Reference relevant libraries
Copy
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.IO;
using Newtonsoft.Json.Linq;
Provide cloud and authentication details
Note:
Replace
<cloud_name>
and <security_token>
with, respectively, your cloud's name and your security token for it (Security Token)
Copy
private static string CloudName = "<cloud_name>";
private static string ServerURL = "https://" + CloudName + ".perfectomobile.com";
private static string SecurityToken = "<security_token>";
Get JSON response from REST API call retrieving reservations list of specific user
Note:
It is possible to alter the method for other REST API operations as well by changing the operation string and the parameters needed for that operation.
(Respectively, change the method name and the parameters it would use.)
The response of the REST API operation should be in JSON format.
Copy
private static JObject RetrieveReservationsList(String reservedTo)
{
String operation = "/services/reservations?operation=list";
Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters.Add("reservedTo", reservedTo);
parameters.Add("securityToken", SecurityToken);
String response = GetResponse(operation, parameters);
JObject reservations = JObject.Parse(response);
return reservations;
}
Implementation of the GetResponse() method
Copy
private static String GetResponse(String operation, Dictionary<String, String> parameters)
{
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(BuildRequestURI(operation, parameters));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
String responseString = reader.ReadToEnd();
return responseString;
}
private static string BuildRequestURI(string suffix, Dictionary<string, string> parameters)
{
string uri = ServerURL + suffix + "&";
if (parameters != null)
{
foreach (var keyValuePair in parameters)
{
uri += keyValuePair.Key + "=" + keyValuePair.Value + "&";
}
uri = uri.Substring(0, uri.Length - 1);
}
return uri;
}
Sample usage of RetrieveReservationsList() method showing how to 'Read' JSON response string
Note:
Here, based on the JSON response format expected as per Legacy | Get Reservation List
Replace <cloud_user>
with the cloud user you need to get reservations list of. Only admin users can view the reservations of other users.
(It is possible to also provide empty string.)
Copy
public static void Main(string[] args)
{
String user = "<cloud_user>";
JObject reservationsJson = retrieveReservationsList(user);
JArray reservationsList = (JArray)reservationsJson["reservations"];
if (reservationsList == null | reservationsList.Count == 0)
{
Console.WriteLine("No reservations for user " + user);
}
else
{
String reservationId, reservationStatus, reservedDevice;
Console.WriteLine("Reservations for user " + user + " :");
Console.WriteLine("***");
Console.WriteLine("");
foreach (JToken reservation in reservationsList)
{
reservationId = reservation["id"].ToString();
reservationStatus = reservation["status"].ToString();
reservedDevice = reservation["resourceId"].ToString();
Console.WriteLine("Reservation # " + reservationId + " with status " + reservationStatus);
Console.WriteLine("Reserved device is " + reservedDevice);
Console.WriteLine("");
}
}
}
Related Articles
- Download an STR via REST API
- Legacy | Test Script Templates for Visual Studio
- Legacy | Building the Perfecto Project in VSTS
- Legacy | Perfecto plugin for Visual Studio