Skip to content

Instantly share code, notes, and snippets.

@romatthe
Created July 28, 2015 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romatthe/bdfde6b9641dd11b4758 to your computer and use it in GitHub Desktop.
Save romatthe/bdfde6b9641dd11b4758 to your computer and use it in GitHub Desktop.
BigFix - Quick Computer scrape!
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Xml.Linq;
using RestSharp;
using RestSharp.Deserializers;
namespace WPMGMGT.BigFixData
{
class Program
{
static void Main(string[] args)
{
BigFixAPI floep = new BigFixAPI("https://DIEMV201.BelgianRail.be:52311/api/", "iemadmin", "bigfix");
floep.Test();
Console.Read();
}
}
public class Computer
{
public Computer()
{
// Empty constructor used by RestSharp
}
[DeserializeAs(Name = "IgnoreID")]
public int ID { get; set; } // Identity ID assigned by DB
[DeserializeAs(Name = "ID")]
public int ComputerID { get; set; } // Identity ID assigned by API
public string ComputerName { get; set; }
public DateTime LastReportTime { get; set; }
}
public class BigFixAPI
{
// Fields
private Uri baseURL;
private HttpBasicAuthenticator authenticator;
//private Logger logger = LogManager.GetCurrentClassLogger();
// Properties
public Uri BaseURL
{
get { return this.baseURL; }
private set { this.baseURL = value; }
}
public HttpBasicAuthenticator Authenticator
{
get { return this.authenticator; }
private set { this.authenticator = value; }
}
// Constructors
public BigFixAPI(string aBaseURL, string aUsername, string aPassword)
{
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
this.BaseURL = new Uri(aBaseURL);
this.authenticator = new HttpBasicAuthenticator(aUsername, aPassword);
}
public void Test()
{
RestClient client = new RestClient(this.BaseURL);
client.Authenticator = this.Authenticator;
RestRequest request = new RestRequest("computers", Method.GET);
List<Computer> computers = Execute<List<Computer>>(request);
foreach (Computer computer in computers)
{
request = new RestRequest("computer/{id}", Method.GET);
request.AddUrlSegment("id", computer.ComputerID.ToString());
XDocument response = Execute(request);
string hostName = response.Element("BESAPI").Element("Computer").Elements("Property")
.Where(e => e.Attribute("Name").Value.ToString() == "Computer Name").Single().Value.ToString();
computer.ComputerName = hostName;
}
foreach (Computer computer in computers)
{
Console.WriteLine("Voila sé: {0} - {1} - {2}", computer.ComputerID, computer.LastReportTime, computer.ComputerName);
}
//return computers;
}
public XDocument Execute(RestRequest request)
{
RestClient client = new RestClient();
client.BaseUrl = this.BaseURL;
client.Authenticator = this.Authenticator;
IRestResponse response = client.Execute(request);
try
{
if (response.ErrorException != null)
{
//logger.ErrorException(response.ErrorException.Message, response.ErrorException);
throw new Exception(response.ErrorMessage);
}
// Return non-deserialized XML document
return XDocument.Parse(response.Content, LoadOptions.None);
}
catch (Exception ex)
{
Console.WriteLine("Error encountered: {0}", ex.Message);
return null;
}
}
public T Execute<T>(RestRequest request) where T : new()
{
RestClient client = new RestClient();
client.BaseUrl = this.BaseURL;
client.Authenticator = this.Authenticator;
var response = client.Execute<T>(request);
try
{
if (response.ErrorException != null)
{
throw new Exception(response.ErrorMessage);
}
}
catch (Exception ex)
{
Console.WriteLine("Error encountered: {0}", ex.Message);
}
return response.Data;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment