3using System.Text.RegularExpressions;
5using System.Net.Http.Headers;
8using System.Collections.Generic;
9using Newtonsoft.Json.Linq;
35 protected string URL =
"";
62 Debug.LogWarning(
"Exception has occurred: " + exception.Message);
63 HttpResponseMessage badRequestResponse =
new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
65 return badRequestResponse;
70 if (!endpointUrl.StartsWith(
"https://", StringComparison.InvariantCultureIgnoreCase))
72 if (endpointUrl.StartsWith(
"http:", StringComparison.InvariantCultureIgnoreCase))
74 Debug.LogWarning(
"Endpoint must be a secured http endpoint.");
75 Regex expression =
new Regex(Regex.Escape(
"http"));
76 endpointUrl = expression.Replace(endpointUrl,
"https", 1);
80 endpointUrl.Insert(0,
"https://");
85 Debug.Log(
"[APIHandler] Set Endpoint to " +
URL);
86 handlingClient.BaseAddress =
new Uri(
URL);
91 if (!endpointUrl.StartsWith(
"https://", StringComparison.InvariantCultureIgnoreCase))
93 if (endpointUrl.StartsWith(
"http:", StringComparison.InvariantCultureIgnoreCase))
95 Debug.LogWarning(
"Endpoint must be a secured http endpoint.");
96 Regex expression =
new Regex(Regex.Escape(
"http"));
97 endpointUrl = expression.Replace(endpointUrl,
"https", 1);
101 endpointUrl.Insert(0,
"https://");
106 webHandlingClient.BaseAddress =
new Uri(
webURL);
111 if (!endpointUrl.StartsWith(
"https://", StringComparison.InvariantCultureIgnoreCase))
113 if (endpointUrl.StartsWith(
"http:", StringComparison.InvariantCultureIgnoreCase))
115 Debug.LogWarning(
"Endpoint must be a secured http endpoint.");
116 Regex expression =
new Regex(Regex.Escape(
"http"));
117 endpointUrl = expression.Replace(endpointUrl,
"https", 1);
121 endpointUrl.Insert(0,
"https://");
126 apiHandlingClient.BaseAddress =
new Uri(
apiURL);
132 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
134 HttpResponseMessage response;
150 webHandlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
151 webHandlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
154 HttpResponseMessage response = await
webHandlingClient.GetAsync(
string.Format(
"api/user/{0}/assisted-login", userId));
155 string body = await response.Content.ReadAsStringAsync();
161 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
170 apiHandlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", token);
171 apiHandlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
173 HttpResponseMessage response = await
apiHandlingClient.GetAsync(
"/v2/auth/validate-signature");
174 string body = await response.Content.ReadAsStringAsync();
178 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
189 Debug.Log(
"[Platform API] Calling Login.");
192 HttpContent loginRequestContent =
new StringContent(JsonUtility.ToJson(login));
193 loginRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
195 Debug.Log(
"[Platform API] Call to post api login.");
196 HttpResponseMessage response = await
handlingClient.PostAsync(
"/login", loginRequestContent);
197 string body = await response.Content.ReadAsStringAsync();
198 Debug.Log(
"[Platform API] Got response body.");
202 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
205 Debug.Log(
"[Platform API] Response content deserialized.");
212 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
213 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
215 HttpResponseMessage response = await
handlingClient.GetAsync(
string.Format(
"/user/{0}", userId));
216 string body = await response.Content.ReadAsStringAsync();
221 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
230 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
231 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
234 usersModulesRequest.
UserIds.Add(userId);
235 HttpContent loginRequestContent =
new StringContent(JsonUtility.ToJson(usersModulesRequest));
236 loginRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
238 HttpResponseMessage response = await
handlingClient.PostAsync(
"/access/users", loginRequestContent);
239 string body = await response.Content.ReadAsStringAsync();
243 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
256 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
257 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
259 HttpContent joinSessionRequestContent =
new StringContent(joinData.ToJSON());
260 joinSessionRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
262 HttpResponseMessage response = await
handlingClient.PostAsync(
"/event", joinSessionRequestContent);
263 string body = await response.Content.ReadAsStringAsync();
267 responseContent =
null;
273 responseContent = joinSessionResponse;
281 Debug.Log(
"[Platform API Handler] Get Module Access");
283 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"*/*"));
285 string optionalParameters =
"";
286 if(serialNumber.Length > 0)
288 optionalParameters =
"?serial=" + serialNumber;
291 Debug.Log(
"Checkingm module access at: " + String.Format(
"/access/user/{0}/module/{1}{2}", userId, moduleId, optionalParameters));
293 HttpResponseMessage response = await
handlingClient.GetAsync(String.Format(
"/access/user/{0}/module/{1}{2}", userId, moduleId, optionalParameters));
294 string body = await response.Content.ReadAsStringAsync();
296 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
308 apiHandlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
309 apiHandlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
313 HttpContent heartbeatRequestContent =
new StringContent(heartbeatData.ToJSON());
314 heartbeatRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
316 HttpResponseMessage response = await
apiHandlingClient.PostAsync(
"/heartbeat/pulse", heartbeatRequestContent);
317 string body = await response.Content.ReadAsStringAsync();
318 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
321 responseContent =
null;
330 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
331 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
333 HttpContent completeSessionRequestContent =
new StringContent(completionData.ToJSON());
334 completeSessionRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
336 HttpResponseMessage response = await
handlingClient.PostAsync(
"/event", completeSessionRequestContent);
337 string body = await response.Content.ReadAsStringAsync();
338 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
341 responseContent =
null;
350 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
351 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
353 HttpContent sessionEventRequestContent =
new StringContent(sessionEvent.ToJSON());
354 sessionEventRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
356 HttpResponseMessage response = await
handlingClient.PostAsync(
"/event", sessionEventRequestContent);
357 string body = await response.Content.ReadAsStringAsync();
358 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
361 responseContent =
null;
370 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
371 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
373 HttpResponseMessage response = await
handlingClient.GetAsync(
string.Format(
"/modules?platform={0}", platform));
374 string body = await response.Content.ReadAsStringAsync();
375 object responseContent =
null;
376 List<OrgModule> orgModules =
new List<OrgModule>();
377 JArray array = JArray.Parse(body);
380 var tokens = array.Children();
381 foreach(JToken selectedToken
in tokens)
384 orgModules.Add(orgModule);
388 Debug.Log(orgModules.Count.ToString());
390 if(orgModules.Count <= 0)
392 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
396 responseContent = orgModules;
async void SendHeartbeat(string authToken, int sessionId)
HttpClient handlingClient
async void GenerateAssistedLogin(string authToken, int userId)
APIHandler(string endpointUrl)
async void GetUserModules(string authToken, int userId)
HttpResponseMessage HandleException(Exception exception)
async void JoinSession(string authToken, JoinSessionData joinData)
async void LoginWithToken(string token)
async void CompleteSession(string authToken, CompleteSessionData completionData)
HttpClient apiHandlingClient
async void Login(LoginData login)
async void SendSessionEvent(string authToken, SessionEventData sessionEvent)
void SetPlatformEndpoint(string endpointUrl)
HttpClient webHandlingClient
async void GetUserData(string authToken, int userId)
delegate void APIResponse(ResponseType type, HttpResponseMessage message, object responseData)
void SetEndpoint(string endpointUrl)
void SetWebEndpoint(string endpointUrl)
APIResponse OnAPIResponse
async void GetModuleAccess(int moduleId, int userId, string serialNumber)
async void GetModuleList(string authToken, string platform)