-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvolutionConnection.cs
195 lines (167 loc) · 6.13 KB
/
EvolutionConnection.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;
namespace PermissionViewer.Services
{
//TODO: Support Impersonation
/// <summary>
/// Helper class for using the Telligent Evolution Platform REST API
/// </summary>
/// <remarks>Used the Cretae method to create a new EvolutionService object</remarks>
public class EvolutionConnection
{
/// <summary>
/// The name of the Telligent Evolution platform product being used by the community
/// </summary>
public string Product { get; private set; }
/// <summary>
/// The version number of the Telligent Evolution Platform
/// </summary>
public Version PlatformVersion { get; private set; }
/// <summary>
/// The version number of the Telligent Evolution Platform REST API.
/// </summary>
/// <remarks>Should never be older than the PlatformVersion</remarks>
public Version WebservicesVersion { get; private set; }
/// <summary>
/// The Url of the Telligent Evolution Platform community
/// </summary>
public string Url { get; private set; }
/// <summary>
/// The Username being used to connect to the Telligent Evolution REST API
/// </summary>
public string Username { get; private set; }
private WebClient client = new WebClient();
private void AddRestUserToken(string username, string apiKey)
{
var adminKey = String.Format("{0}:{1}", apiKey, username);
var restUserToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(adminKey));
client.Headers.Add("Rest-User-Token", restUserToken);
}
/// <summary>
/// Creates a new EvolutionConnection for the given community url
/// </summary>
/// <param name="url">The url to the root of the Telligent Evolution platform community</param>
public EvolutionConnection(string url)
{
TryConnect(url);
}
/// <summary>
/// Creates a new EvolutionConnection for the given community url
/// using the given username and api key
/// </summary>
/// <param name="url">The url to the root of the Telligent Evolution platform community</param>
/// <param name="username">The user to connect as</param>
/// <param name="apiKey">The API Key to use for authentication</param>
public EvolutionConnection(string url, string username, string apiKey)
{
if (String.IsNullOrEmpty(username))
throw new InvalidOperationException("Username must not be null or empty");
if (String.IsNullOrEmpty(apiKey))
throw new InvalidOperationException("API Key must not be null or empty");
AddRestUserToken(username, apiKey);
TryConnect(url);
}
/// <summary>
/// Creates a new EvolutionConnection for the given community url using the given credentials
/// </summary>
/// <param name="url">The url to the root of the Telligent Evolution platform community</param>
/// <param name="credentials">The Credentials to connect with</param>
/// <param name="apiKey">The API Key to connect with</param>
public EvolutionConnection(string url, NetworkCredential credentials, string apiKey)
{
if (credentials == null)
throw new InvalidOperationException("Credentials must not be null");
if (String.IsNullOrEmpty(apiKey))
throw new InvalidOperationException("API Key must not be null or empty");
AddRestUserToken(credentials.UserName, apiKey);
client.Credentials = credentials;
TryConnect(url);
}
/// <summary>
/// Helper method to try and make a connection
/// </summary>
/// <param name="url">The url to the root of the Telligent Evolution platform community to connect to</param>
private void TryConnect(string url)
{
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
throw new InvalidOperationException("Url is not in a valid format");
Url = url;
try
{
LoadVersionInfo();
}
catch (WebException ex)
{
var response = ex.Response as HttpWebResponse;
//If we got a 401 response, try reconnecting with default credentials
if (response == null || response.StatusCode != HttpStatusCode.Unauthorized)
throw;
//Try and connect using default windows credentials
client.UseDefaultCredentials = true;
try
{
LoadVersionInfo();
}
catch
{
//Throw the original exception
throw ex;
}
}
}
/// <summary>
/// Loads version information using the Site Information endpoint
/// </summary>
private void LoadVersionInfo()
{
var response = GetResponse("/api.ashx/v2/info.xml");
//Populate product and version information
Product = response.Descendants(XName.Get("Product")).First().Value;
PlatformVersion = new Version(response.Descendants(XName.Get("Platform")).First().Value);
WebservicesVersion = new Version(response.Descendants(XName.Get("Webservice")).First().Value);
}
/// <summary>
/// Creates a new EvolutionService object, validating that the credentials
/// are valid
/// </summary>
/// <param name="url">The url of the Telligent Evolution platform community</param>
/// <param name="username">The username to connect as</param>
/// <param name="apiKey">The API Key to connect with</param>
/// <returns>An EvolutionService holding the credentials if valid, otherwise throws an exception.</returns>
public static EvolutionConnection Create(string url, string username, string apiKey)
{
return new EvolutionConnection(url, username, apiKey);
}
/// <summary>
/// Converts a relative endpoint url to an absolute url
/// </summary>
/// <param name="endpoint">An endpoint with a relative url</param>
/// <returns>The absolute path for the endpoint</returns>
public string GetFullEndpointUrl(string endpoint)
{
if (!endpoint.StartsWith("/"))
endpoint = "/" + endpoint;
return Url + endpoint;
}
/// <summary>
/// Gets the Response from the given endpoint
/// </summary>
/// <param name="endpoint">The relative url for the endpoint to hit</param>
/// <returns>An XDocument containing the response from the endpoint</returns>
public XDocument GetResponse(string endpoint)
{
var requestUrl = GetFullEndpointUrl(endpoint);
using (var response = client.OpenRead(requestUrl))
{
using (var reader = new StreamReader(response))
{
return XDocument.Load(reader);
}
}
}
}
}