Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Jul 04, 2019 14:27

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: Get Reservation List

Adapted from https://github.com/PerfectoCode/Reporting-Samples/tree/master/CSharp/export-api-sample 


Reference relevant libraries:

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 )

        private static string CloudName = "<cloud_name>";
        private static string ServerURL = "https://" + CloudName + ".perfectomobile.com";
        private static string SecurityToken = "<security_token>";


Get the JSON response from the REST API call retrieving the 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.

        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

        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 above RetrieveReservationsList() method showing how to 'read' the JSON response string

Note: Here, based on the JSON response format expected as per 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.)

        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("");

                }
            }
        }