Given example written on C# contains the following patterns:

C# Example

using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
namespace example
{
class Program
{
class OperatioResult
{
public bool Success { get; set; }
public string Result { get; set; }
public override string ToString()
{
return String.Format(“Success: {0}; Result: {1}”, Success, Result);
}
}
class ErrorResult
{
public bool Success { get; set; }
public string[] ErrorDescription { get; set; }
public override string ToString()
{
return String.Join(Environment.NewLine, ErrorDescription);
}
}
class Platform
{
public int Id { get; set; }
public string Name { get; set; }
public bool Available { get; set; }
public override string ToString()
{
return String.Format(“Id: {0}; Name: {1}; Available: {2}”, Id, Name, Available);
}
}
class Monitor
{
public int Id { get; set; }
public string Name { get; set; }
public bool Available { get; set; }
public bool IsDeleted { get; set; }
public bool IsPrivate { get; set; }
public override string ToString()
{
return String.Format(“Id: {0}; Name: {1}; Available: {2}; IsDeleted: {3}; IsPrivate: {4}”,
Id, Name, Available, IsDeleted, IsPrivate);
}
}
class WeeklyInterval
{
public string[] Days { get; set; }
public int From_Min { get; set; }
public int To_Min { get; set; }
public bool Included { get; set; }
public override string ToString()
{
return String.Format(“Days: [{0}]; From_Min: {1} (Time: {4}); To_Min: {2} (Time: {5}); Included: {3}”,
String.Join(“, “, Days), From_Min, To_Min, Included, TimeSpan.FromMinutes(From_Min), TimeSpan.FromMinutes(To_Min));
}
}
class DateTimeInterval
{
public long From { get; set; }
public long To { get; set; }
public override string ToString()
{
return String.Format(“From: {0} (Time: {2}); To: {1} (Time: {3})”,
From, To, UnixBaseTime.AddMilliseconds(From), UnixBaseTime.AddMilliseconds(To));
}
}
class AssignedToInfo
{
/// summary
/// read only
/// /summary
public int[] Devices { get; set; }
/// summary
/// read only
/// /summary
public int[] Notification_Groups { get; set; }
public override string ToString()
{
return String.Format(“Devices: [{0}]; Notification_Groups: [{1}]”,
String.Join(“, “, Devices.Select(id => id.ToString()).ToArray()),
String.Join(“, “, Notification_Groups.Select(id => id.ToString()).ToArray()));
}
}
class Scheduler
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public WeeklyInterval[] Weekly_Intervals { get; set; }
public DateTimeInterval[] Date_Time_Intervals { get; set; }
public AssignedToInfo Assigned_To { get; set; }
public override string ToString()
{
return String.Format(“Id: {0}; Name: {1}; Description: {2}; Weekly_Intervals: [{3}]; Date_Time_Intervals: [{4}]; AssignedToInfo: {5}”,
Id, Name, Description,
String.Join(“, “, Weekly_Intervals.Select(wi => wi.ToString()).ToArray()),
String.Join(“, “, Date_Time_Intervals.Select(dti => dti.ToString()).ToArray()), Assigned_To);
}
}
class SiteNotifyGroup
{
public int Id { get; set; }
public int Time_Shift_Min { get; set; }
}
class Notifications
{
public bool E_Mail_Flag { get; set; }
public string E_Mail_Address { get; set; }
public int E_Mail_TimeInterval_Min { get; set; }
public bool WL_Device_Flag { get; set; }
public string WL_Device_Email_Address { get; set; }
public int WL_Device_TimeInterval_Min { get; set; }
public bool Phone_Flag { get; set; }
public string Phone_Area_Code { get; set; }
public string Phone_Phone { get; set; }
public int Phone_TimeInterval_Min { get; set; }
public bool SMS_Flag { get; set; }
public string SMS_Phone { get; set; }
public int SMS_TimeInterval_Min { get; set; }
public bool Script_Flag { get; set; }
public string Script_Batch_File_Name { get; set; }
public int Script_TimeInterval_Min { get; set; }
public int SNMP_TimeInterval_Min { get; set; }
public SiteNotifyGroup[] Notification_Groups { get; set; }
}
class ServerViewDevice
{
public int Id { get; set; }
public int Platform_Id { get; set; }
public string Name { get; set; }
public int Number_Of_Tasks { get; set; }
public string Status_Description { get; set; }
public bool Postpone { get; set; }
public bool Send_Uptime_Alert { get; set; }
public int Owner_Device_Id { get; set; }
public int Frequency { get; set; }
public int Filter_Id { get; set; }
public int Scheduler_Id { get; set; }
public Notifications Notifications { get; set; }
public bool Avoid_Simultaneous_Checks { get; set; }
public bool False_Positive_Check { get; set; }
public int Alert_Silence_Min { get; set; }
public int[] Locations { get; set; }
}
class TaskType
{
public int Id { get; set; }
public string Name { get; set; }
}
class NameValuePair
{
public string Name { get; set; }
public string Value { get; set; }
}
class HTTPTask
{
public int Device_Id { get; set; }
public int Id { get; set; }
public int Task_Type_Id { get; set; }
public string Name { get; set; }
public int Timeout { get; set; }
public string RequestType { get; set; }
public string Url { get; set; }
public string Keyword1 { get; set; }
public string Keyword2 { get; set; }
public string Keyword3 { get; set; }
public string UserName { get; set; }
public string UserPass { get; set; }
public bool FullPageDownload { get; set; }
public bool Download_Html { get; set; }
public bool Download_Frames { get; set; }
public bool Download_StyleSheets { get; set; }
public bool Download_Scripts { get; set; }
public bool Download_Images { get; set; }
public bool Download_Objects { get; set; }
public bool Download_Applets { get; set; }
public bool Download_Additional { get; set; }
public NameValuePair[] GetParams { get; set; }
public NameValuePair[] PostParams { get; set; }
public NameValuePair[] HeaderParams { get; set; }
string PrepareScript { get; set; }
}
enum RequestMethod
{
GET,
POST
}
private static CookieContainer container;
public static readonly DateTime UnixBaseTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
// setting up request function
private static string Request(string action, RequestMethod requestType, string data)
{
WebRequest request = WebRequest.Create(“https://api.dotcom-monitor.com/config_api_v1/” + action);
request.Method = requestType.ToString();
((HttpWebRequest) request).CookieContainer = container;
if (requestType == RequestMethod.POST)
{
string postData = data;
if (postData.Length > 0)
{
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = “application/json”;
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
}
else
{
request.ContentLength = 0;
}
}
try
{
using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
if (stream != null)
{
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}
}
catch (Exception ex)
{
using (var stream = ex.Response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
throw new Exception(reader.ReadToEnd());
}
}
}
return String.Empty;
}

static void Main()
{
// ignore certificate errors
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
// initialize cookie container
container = new CookieContainer();
// initialize javascript serializer
var serializer = new JavaScriptSerializer();
// login
Console.WriteLine(“==Login==”);
string resultStr;
try
{
resultStr = Request(“login”, RequestMethod.POST,
serializer.Serialize(
new {
UserName = “1”,
Password = “1”
}
));
var result = serializer.Deserialize<OperatioResult>(resultStr);
Console.WriteLine(result.ToString());
}
catch (Exception ex)
{
var err = serializer.Deserialize<ErrorResult>(ex.Message);
Console.WriteLine(err.ToString());
}
// get platforms
Console.WriteLine(“==Get platforms==”);
IEnumerable<Platform> platforms = null;
try
{
resultStr = Request(“platforms”, RequestMethod.GET, String.Empty);
platforms = serializer.Deserialize<Platform[]>(resultStr);
}
catch(Exception ex)
{
var err = serializer.Deserialize<ErrorResult>(ex.Message);
Console.WriteLine(err.ToString());
}
foreach (var platform in platforms)
{
Console.WriteLine(platform);
}
// get devices by platform
Console.WriteLine(“==Get devices by platform==”);
int svDeviceId = 0;
foreach (var platform in platforms.Where(p => p.Available))
{
resultStr = Request(String.Format(“devices/{0}”, platform.Name), RequestMethod.GET, String.Empty);
var ds = serializer.Deserialize<int[]>(resultStr);
foreach (var id in ds)
{
if (svDeviceId == 0 && platform.Id == 1)
{
svDeviceId = id;
}
Console.WriteLine(“{0} – {1}”, platform.Name, id);
}
}
// get ServerView device by id
Console.WriteLine(“==Get device by id==”);
resultStr = Request(String.Format(“device/{0}”, svDeviceId), RequestMethod.GET, String.Empty);
var device = serializer.Deserialize<ServerViewDevice>(resultStr);
// edit device
Console.WriteLine(“==Edit device==”);
device.Name = device.Name + ” (edited)”;
try
{
resultStr = Request(String.Format(“device/{0}”, device.Id), RequestMethod.POST,
serializer.Serialize(device));
var result = serializer.Deserialize<OperatioResult>(resultStr);
Console.WriteLine(result.ToString());
}
catch (Exception ex)
{
var err = serializer.Deserialize<ErrorResult>(ex.Message);
Console.WriteLine(err.ToString());
}
// get monitors
Console.WriteLine(“==Get monitors==”);
IEnumerable<Monitor> monitors = serializer.Deserialize<Monitor[]>(Request(String.Format(“locations/{0}”, device.Platform_Id),
RequestMethod.GET, String.Empty));
foreach (var monitor in monitors)
{
Console.WriteLine(monitor);
}
// get frequencies
Console.WriteLine(“==Get frequencies==”);
var frequencies = serializer.Deserialize<int[]>(Request(String.Format(“frequencies/{0}”, device.Platform_Id),
RequestMethod.GET, String.Empty));
foreach (var frequency in frequencies)
{
Console.WriteLine(frequency);
}
// get notification groups
Console.WriteLine(“==Get notification groups==”);
var ngs = serializer.Deserialize<int[]>(Request(“groups”, RequestMethod.GET, String.Empty));
foreach (var ng in ngs)
{
Console.WriteLine(ng);
}
// create new device
Console.WriteLine(“==Create device==”);
device = new ServerViewDevice
{
Platform_Id = platforms.Single(p => p.Name.ToLower() == “serverview”).Id,
Name = “new device”,
Frequency = frequencies[0],
Postpone = true,
Locations = monitors.Where(m => m.Available && !m.IsPrivate && (m.Id%2 == 0)).Select(m => m.Id).ToArray(),
Avoid_Simultaneous_Checks = true,
Notifications = new Notifications
{
E_Mail_Flag = false,
Pager_Flag = false,
Phone_Flag = false,
SMS_Flag = false,
Script_Flag = false,
WL_Device_Flag = false,
Notification_Groups = ngs.Take(1).Select(ng => new SiteNotifyGroup {Id = ng, Time_Shift_Min = 0 /*0 = Immediate*/}).ToArray()
}
};
int newDeviceId = 0;
try
{
resultStr = Request(“devices?verb=PUT”, RequestMethod.POST,
serializer.Serialize(device));
var result = serializer.Deserialize<OperatioResult>(resultStr);
newDeviceId = int.Parse(result.Result);
Console.WriteLine(result.ToString());
}
catch (Exception ex)
{
var err = serializer.Deserialize<ErrorResult>(ex.Message);
Console.WriteLine(err.ToString());
}
// add new HTTP task
Console.WriteLine(“==Create HTTP task==”);
resultStr = Request(“tasktypes/” + device.Platform_Id, RequestMethod.GET, String.Empty);
var tts = serializer.Deserialize<TaskType[]>(resultStr);
var httpTT = tts.Single(tt => tt.Name.ToLower() == “http”);
var task = new HTTPTask
{
Device_Id = newDeviceId,
Task_Type_Id = httpTT.Id,
Name = “New HTTP Task”,
Timeout = 100500,
RequestType = “GET”,
Url = “amazon.de”
};
try
{
resultStr = Request(“tasks?verb=PUT”, RequestMethod.POST,
serializer.Serialize(task));
var result = serializer.Deserialize<OperatioResult>(resultStr);
Console.WriteLine(result.ToString());
}
catch (Exception ex)
{
var err = serializer.Deserialize<ErrorResult>(ex.Message);
Console.WriteLine(err.ToString());
}

// get schedulers
Console.WriteLine(“==Get schedulers==”);
var ss = serializer.Deserialize<int[]>(Request(“schedulers”, RequestMethod.GET, String.Empty));
foreach (var s in ss)
{
Console.WriteLine(serializer.Deserialize<Scheduler>(Request(“scheduler/” + s, RequestMethod.GET, String.Empty)));
}
// create new schedulers
Console.WriteLine(“==Create new schedulers==”);
var scheduler = new Scheduler
{
Name = “New”,
Description = “descr”,
Date_Time_Intervals = new[] {
new DateTimeInterval
{
From = (long) (new DateTime(2013, 2, 2, 2, 0, 0, 0) – UnixBaseTime).TotalMilliseconds,
To = (long) (new DateTime(2013, 2, 2, 2, 30, 0, 0) – UnixBaseTime).TotalMilliseconds,
}
},
Weekly_Intervals = new[] {
new WeeklyInterval
{
Days = new[] {“mo”, “tu”, “sa”},
From_Min = (int) new TimeSpan(7, 0, 0).TotalMinutes,
To_Min = (int) new TimeSpan(13, 0, 0).TotalMinutes,
Included = false
}
}
};
try
{
resultStr = Request(“schedulers?verb=PUT”, RequestMethod.POST,
serializer.Serialize(scheduler));
var result = serializer.Deserialize<OperatioResult>(resultStr);
Console.WriteLine(result.ToString());
}
catch (Exception ex)
{
var err = serializer.Deserialize<ErrorResult>(ex.Message);
Console.WriteLine(err.ToString());
}
Console.ReadKey();
}
}
}