-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVTM.cs
49 lines (41 loc) · 1.54 KB
/
VTM.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace Org.BeyondComputing.NewRelic.Brocade.VTM
{
class VTM
{
private HttpClient client;
public VTM(string host, int port, string username, string password)
{
// HTTP Client settings
var credentials = new NetworkCredential(username, password);
var handler = new HttpClientHandler { Credentials = credentials };
var url = string.Format("https://{0}:{1}", host, port);
// Create HTTP Client for all future requests to API
this.client = new HttpClient(handler);
this.client.BaseAddress = new Uri(url);
// Add an Accept header for JSON format.
this.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public T fetchVTMObject<T>(string href)
{
HttpResponseMessage response = client.GetAsync(href).Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
// Parse the response body. Blocking!
return response.Content.ReadAsAsync<T>().Result;
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
throw new ApplicationException("Error retrieving data from VTM");
}
}
}
}