4using System.Net.Http.Headers;
7using System.Collections.Generic;
8using Newtonsoft.Json.Linq;
34 protected string URL =
"";
61 Debug.LogWarning(
"Exception has occurred: " + exception.Message);
62 HttpResponseMessage badRequestResponse =
new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
64 return badRequestResponse;
72 Debug.Log(
"[APIHandler] Set Endpoint to " +
URL);
73 handlingClient.BaseAddress =
new Uri(
URL);
81 webHandlingClient.BaseAddress =
new Uri(
webURL);
89 apiHandlingClient.BaseAddress =
new Uri(
apiURL);
94 if (!url.StartsWith(
"https://", StringComparison.InvariantCultureIgnoreCase))
96 if (url.StartsWith(
"http:", StringComparison.InvariantCultureIgnoreCase))
99 Debug.LogWarning(
"URL must be a secured http endpoint for production.");
101 Debug.LogError(
"URL must be a securated http endpoint.");
106 url.Insert(0,
"https://");
114 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
116 HttpResponseMessage response;
132 webHandlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
133 webHandlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
136 HttpResponseMessage response = await
webHandlingClient.GetAsync(
string.Format(
"api/user/{0}/assisted-login", userId));
137 string body = await response.Content.ReadAsStringAsync();
143 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
151 Debug.Log($
"[Platform API] Logging in with token: {token}");
153 apiHandlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", token);
154 apiHandlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
156 Debug.Log($
"[Platform API] Sending login with a token.");
157 HttpResponseMessage response = await
apiHandlingClient.GetAsync(
"/v2/auth/validate-signature");
158 string body = await response.Content.ReadAsStringAsync();
159 Debug.Log($
"[Platform API] Body returned as {body}");
163 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
166 Debug.Log($
"[Platform API] Got a valid login response!");
174 Debug.Log(
"[Platform API] Calling Login.");
177 HttpContent loginRequestContent =
new StringContent(JsonUtility.ToJson(login));
178 loginRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
180 Debug.Log(
"[Platform API] Call to post api login.");
181 HttpResponseMessage response = await
handlingClient.PostAsync(
"/login", loginRequestContent);
182 string body = await response.Content.ReadAsStringAsync();
183 Debug.Log(
"[Platform API] Got response body.");
187 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
190 Debug.Log(
"[Platform API] Response content deserialized.");
197 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
198 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
200 HttpResponseMessage response = await
handlingClient.GetAsync(
string.Format(
"/user/{0}", userId));
201 string body = await response.Content.ReadAsStringAsync();
206 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
215 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
216 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
219 usersModulesRequest.
UserIds.Add(userId);
220 HttpContent loginRequestContent =
new StringContent(JsonUtility.ToJson(usersModulesRequest));
221 loginRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
223 HttpResponseMessage response = await
handlingClient.PostAsync(
"/access/users", loginRequestContent);
224 string body = await response.Content.ReadAsStringAsync();
228 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
241 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
242 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
244 HttpContent joinSessionRequestContent =
new StringContent(joinData.ToJSON());
245 joinSessionRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
247 HttpResponseMessage response = await
handlingClient.PostAsync(
"/event", joinSessionRequestContent);
248 string body = await response.Content.ReadAsStringAsync();
252 responseContent =
null;
258 responseContent = joinSessionResponse;
266 Debug.Log(
"[Platform API Handler] Get Module Access");
268 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"*/*"));
270 string optionalParameters =
"";
271 if(serialNumber.Length > 0)
273 optionalParameters =
"?serial=" + serialNumber;
276 Debug.Log($
"[{GetType().Name}] Checking module access at: " + String.Format(
"/access/user/{0}/module/{1}{2}", userId, moduleId, optionalParameters));
278 HttpResponseMessage response = await
handlingClient.GetAsync(String.Format(
"/access/user/{0}/module/{1}{2}", userId, moduleId, optionalParameters));
279 string body = await response.Content.ReadAsStringAsync();
281 Debug.Log($
"[{GetType().Name}] GetModuleAccess return body: {body}");
282 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
294 apiHandlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
295 apiHandlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
299 HttpContent heartbeatRequestContent =
new StringContent(heartbeatData.ToJSON());
300 heartbeatRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
302 HttpResponseMessage response = await
apiHandlingClient.PostAsync(
"/heartbeat/pulse", heartbeatRequestContent);
303 string body = await response.Content.ReadAsStringAsync();
304 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
307 responseContent =
null;
316 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
317 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
319 HttpContent completeSessionRequestContent =
new StringContent(completionData.ToJSON());
320 completeSessionRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
322 HttpResponseMessage response = await
handlingClient.PostAsync(
"/event", completeSessionRequestContent);
323 string body = await response.Content.ReadAsStringAsync();
324 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
327 responseContent =
null;
336 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
337 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
339 HttpContent sessionEventRequestContent =
new StringContent(sessionEvent.ToJSON());
340 sessionEventRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
342 HttpResponseMessage response = await
handlingClient.PostAsync(
"/event", sessionEventRequestContent);
343 string body = await response.Content.ReadAsStringAsync();
344 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
347 responseContent =
null;
356 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
357 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
359 string endpoint =
"/modules";
360 if (platform !=
null && platform.Length > 0)
362 endpoint += $
"?platform={platform}";
365 Debug.Log($
"GetModuleList built endpoint: {endpoint}");
367 HttpResponseMessage response = await
handlingClient.GetAsync(endpoint);
368 string body = await response.Content.ReadAsStringAsync();
372 var responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
378 Debug.LogWarning(ex);
381 List<OrgModule> orgModules =
new List<OrgModule>();
382 JArray array = JArray.Parse(body);
385 var tokens = array.Children();
386 foreach(JToken selectedToken
in tokens)
389 orgModule.
Parse(selectedToken);
390 orgModules.Add(orgModule);
394 Debug.Log(orgModules.Count.ToString());
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)
void EnsureURLHasProtocol(ref string url)
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)