Using C# and .NET
Sample code doing a GET request
// Call the API to get all monitors
string url = String.Format( "http://{0}/Reveille/API/Monitors/AllMonitors", serverName );
string response = DoGET( url, domain, userName, password );
// Deserialize the response from json to a string array
// using the Newtonsoft NuGet package.
var array = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>( response );
// Output the list of monitors
foreach ( var monitor in array )
{
Console.WriteLine( monitor );
}
/// <summary>
/// Sample code to do a REST API GET call.
/// </summary>
/// <param name="url"></param>
/// <param name="domain"></param>
/// <param name="username"></param>
/// <param name="password"></param>
private static string DoGET(string url, string domain, string username, string password)
{
string response = String.Empty;
try
{
// Create the request
HttpWebRequest theHttpWebRequest = ( HttpWebRequest )WebRequest.Create( url );
theHttpWebRequest.Method = "GET";
theHttpWebRequest.Accept = "application/json";
if ( username != String.Empty )
{
// Set windows credentials to the server
theHttpWebRequest.Credentials = new NetworkCredential(username, password, domain);
}
// Read the response
WebResponse webResponse = theHttpWebRequest.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader( webStream );
response = responseReader.ReadToEnd();
responseReader.Close();
}
catch ( Exception e )
{
Console.WriteLine( e.Message );
}
return response;
}
Sample code doing a POST request
// Build the filter for the API call
ApplicationDetailstFilter filter = new ApplicationDetailstFilter();
filter.Type = "L10"; // Last 10 transactions
filter.Transactions = "Login,Logout"; // Only get logins and logouts
filter.StartDateTime = DateTime.Now.AddHours( -1 ); // Last hour
filter.EndDateTime = DateTime.Now;
// Serialize the input filter to json using the Newtonsoft NuGet package.
var postData = Newtonsoft.Json.JsonConvert.SerializeObject( filter );
// Call the API to get the application transaction details
url = String.Format(
"http://{0}/ReveilleUserAnalytics/API/Application/{1}/ApplicationDetails",
serverName, applicationId );
response = DoPOST( url, postData, domain, userName, password );
// Deserialize the response from json using the Newtonsoft NuGet package.
var details = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TransactionDetails>>( response );
// Output the transaction user and name
foreach ( var detail in details )
{
Console.WriteLine( "UserId={0} TransactionName={1}",
detail.UserId, detail.TransactionName );
}
/// <summary>
/// Sample code to do a REST API POST call
/// </summary>
/// <param name="url"></param>
/// <param name="data"></param>
/// <param name="domain"></param>
/// <param name="username"></param>
/// <param name="password"></param>
private static string DoPOST( string url, string data, string domain,
string username, string password )
{
string response = String.Empty;
try
{
// Create the request
HttpWebRequest theHttpWebRequest = ( HttpWebRequest )WebRequest.Create( url );
theHttpWebRequest.Method = "POST";
theHttpWebRequest.Accept = "application/json";
theHttpWebRequest.ContentType = "application/json";
theHttpWebRequest.ContentLength = data.Length;
if ( username != String.Empty )
{
// Set the credentials
theHttpWebRequest.Credentials = new NetworkCredential(username, password, domain);
}
// Write the post data
StreamWriter requestWriter = new StreamWriter( theHttpWebRequest.GetRequestStream(),
System.Text.Encoding.ASCII );
requestWriter.Write( data );
requestWriter.Close();
// Read the response
WebResponse webResponse = theHttpWebRequest.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader( webStream );
response = responseReader.ReadToEnd();
responseReader.Close();
}
catch ( Exception e )
{
Console.WriteLine( e.Message );
}
return response;
}
/// <summary>
/// The application details filter.
/// </summary>
public class ApplicationDetailstFilter
{
public string Type = String.Empty;
public string Transactions = String.Empty;
public string UserIds = String.Empty;
public string CustomField1Value = String.Empty;
public string ClientIps = String.Empty;
public string ServerIps = String.Empty;
public bool Errors = true;
public bool NonErrors = true;
public DateTime StartDateTime = DateTime.MinValue;
public DateTime EndDateTime = DateTime.MinValue;
public string SortExpression = String.Empty;
}
/// <summary>
/// The transaction details
/// </summary>
public class TransactionDetails
{
public long RecordNumber = 0;
public int ApplicationId = 0;
public string TransactionName = String.Empty;
public DateTime StartDateTime = DateTime.MinValue;
public string UserId = String.Empty;
public string ClientIp = String.Empty;
public string ServerIp = String.Empty;
public int StatusCode = 0;
public double ResponseTime = 0;
public List<string> CustomFieldNames = new List<string>(10);
public List<string> CustomFieldValues = new List<string>(10);
}
Was this helpful?
Please tell us why:
Thank you.