2using System.Collections.Generic;
4using System.Net.Http.Headers;
6using Newtonsoft.Json.Linq;
36 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 apiHandlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
133 apiHandlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
136 var graphqlRequest =
new
138 operationName =
"generateAuthCode",
139 variables =
new { input =
new { } },
140 query =
"mutation generateAuthCode($input: AuthCodeInput!) { generateAuthCode(input: $input) { code expiresAt __typename }}",
143 string jsonContent = JsonConvert.SerializeObject(graphqlRequest);
144 HttpContent requestContent =
new StringContent(jsonContent, System.Text.Encoding.UTF8,
"application/json");
145 HttpResponseMessage response;
146 object responseContent;
150 string body = await response.Content.ReadAsStringAsync();
154 JObject jsonResponse = JObject.Parse(body);
156 if (jsonResponse[
"data"] !=
null && jsonResponse[
"data"][
"generateAuthCode"] !=
null)
159 string code = jsonResponse[
"data"][
"generateAuthCode"][
"code"]?.ToString();
160 string expiresAt = jsonResponse[
"data"][
"generateAuthCode"][
"expiresAt"]?.ToString();
168 responseContent = assistedLogin;
170 else if (jsonResponse[
"errors"] !=
null)
173 string errorMessage = jsonResponse[
"errors"]?[0]?[
"message"]?.ToString() ??
"Unknown GraphQL error";
174 responseContent =
new FailureResponse { Error =
"true", Message = errorMessage };
182 Message =
"Invalid response format from server",
188 Debug.LogError($
"Error generating assisted login: {ex.Message}");
189 response =
new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError);
190 responseContent =
new FailureResponse { Error =
"true", Message = ex.Message };
198 Debug.Log($
"[Platform API] Logging in with token: {token}");
200 apiHandlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", token);
201 apiHandlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
203 Debug.Log($
"[Platform API] Sending login with a token.");
204 HttpResponseMessage response = await
apiHandlingClient.GetAsync(
"/v2/auth/validate-signature");
205 string body = await response.Content.ReadAsStringAsync();
206 Debug.Log($
"[Platform API] Body returned as {body}");
210 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
213 Debug.Log($
"[Platform API] Got a valid login response!");
221 Debug.Log(
"[Platform API] Calling Login.");
224 HttpContent loginRequestContent =
new StringContent(JsonUtility.ToJson(login));
225 loginRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
227 Debug.Log(
"[Platform API] Call to post api login.");
228 HttpResponseMessage response = await
handlingClient.PostAsync(
"/login", loginRequestContent);
229 string body = await response.Content.ReadAsStringAsync();
230 Debug.Log(
"[Platform API] Got response body: " + body);
234 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
237 Debug.Log(
"[Platform API] Response content deserialized.");
244 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
245 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
247 HttpResponseMessage response = await
handlingClient.GetAsync(
string.Format(
"/user/{0}", userId));
248 string body = await response.Content.ReadAsStringAsync();
253 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
262 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
263 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
266 usersModulesRequest.
UserIds.Add(userId);
267 HttpContent loginRequestContent =
new StringContent(JsonUtility.ToJson(usersModulesRequest));
268 loginRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
270 HttpResponseMessage response = await
handlingClient.PostAsync(
"/access/users", loginRequestContent);
271 string body = await response.Content.ReadAsStringAsync();
275 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
288 apiHandlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
290 HttpResponseMessage response = await
apiHandlingClient.GetAsync(
string.Format(
"/v2/auth/quick-id/get-users?serialNumber={0}", serialNumber));
291 string body = await response.Content.ReadAsStringAsync();
293 Debug.Log($
"[Platform API] Body returned as {body}");
298 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
306 Debug.Log(
"[Platform API] Calling Quick ID login.");
309 HttpContent loginRequestContent =
new StringContent(JsonUtility.ToJson(login));
310 Debug.Log(
"[Platform API] Quick ID login request content: " + JsonUtility.ToJson(login));
311 loginRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
313 Debug.Log(
"[Platform API] Call to post api Quick ID login.");
314 HttpResponseMessage response = await
apiHandlingClient.PostAsync(
"/v2/auth/quick-id/login", loginRequestContent);
315 string body = await response.Content.ReadAsStringAsync();
316 Debug.Log(
"[Platform API] Got response body: " + body);
322 responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
328 loginResponseContent.Token = platformLoginResponse.
Token;
329 if (platformLoginResponse.User !=
null)
331 loginResponseContent.ID = platformLoginResponse.User.Id;
332 loginResponseContent.OrgId = platformLoginResponse.User.OrgId;
333 loginResponseContent.First = platformLoginResponse.User.FirstName;
334 loginResponseContent.Last = platformLoginResponse.User.LastName;
335 loginResponseContent.Email = platformLoginResponse.User.Email;
336 loginResponseContent.Role = platformLoginResponse.User.Role;
337 loginResponseContent.Org = platformLoginResponse.User.Org;
341 Debug.Log(
"[Platform API] Quick ID login response did not contain user data.");
345 Debug.Log(
"[Platform API] Response content deserialized and mapped.");
352 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
353 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
355 HttpContent joinSessionRequestContent =
new StringContent(joinData.ToJSON());
356 joinSessionRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
358 HttpResponseMessage response = await
handlingClient.PostAsync(
"/event", joinSessionRequestContent);
359 string body = await response.Content.ReadAsStringAsync();
363 responseContent =
null;
369 responseContent = joinSessionResponse;
377 Debug.Log(
"[Platform API Handler] Get Module Access");
379 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"*/*"));
381 string optionalParameters =
"";
382 Debug.Log($
"Checking for a serial number: {serialNumber}");
383 if (!
string.IsNullOrEmpty(serialNumber))
385 optionalParameters =
"?serial=" + serialNumber;
389 $
"[{GetType().Name}] Checking module access at: "
390 + String.Format(
"/access/user/{0}/module/{1}{2}", userId, moduleId, optionalParameters)
394 String.Format(
"/access/user/{0}/module/{1}{2}", userId, moduleId, optionalParameters)
396 string body = await response.Content.ReadAsStringAsync();
398 Debug.Log($
"[{GetType().Name}] GetModuleAccess return body: {body}");
399 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
410 apiHandlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
411 apiHandlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
415 HttpContent heartbeatRequestContent =
new StringContent(heartbeatData.ToJSON());
416 heartbeatRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
420 heartbeatRequestContent
422 string body = await response.Content.ReadAsStringAsync();
423 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
426 responseContent =
null;
435 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
436 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
438 HttpContent completeSessionRequestContent =
new StringContent(completionData.ToJSON());
439 completeSessionRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
441 HttpResponseMessage response = await
handlingClient.PostAsync(
"/event", completeSessionRequestContent);
442 string body = await response.Content.ReadAsStringAsync();
443 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
446 responseContent =
null;
455 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
456 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
458 HttpContent sessionEventRequestContent =
new StringContent(sessionEvent.ToJSON());
459 sessionEventRequestContent.Headers.ContentType =
new MediaTypeWithQualityHeaderValue(
"application/json");
461 HttpResponseMessage response = await
handlingClient.PostAsync(
"/event", sessionEventRequestContent);
462 string body = await response.Content.ReadAsStringAsync();
463 object responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
466 responseContent =
null;
475 handlingClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Bearer", authToken);
476 handlingClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json"));
478 string endpoint =
"/modules";
479 if (platform !=
null && platform.Length > 0)
481 endpoint += $
"?platform={platform}";
484 Debug.Log($
"GetModuleList built endpoint: {endpoint}");
486 HttpResponseMessage response = await
handlingClient.GetAsync(endpoint);
487 string body = await response.Content.ReadAsStringAsync();
491 var responseContent = JsonConvert.DeserializeObject<
FailureResponse>(body);
497 Debug.LogWarning(ex);
500 List<OrgModule> orgModules =
new List<OrgModule>();
501 JArray array = JArray.Parse(body);
504 var tokens = array.Children();
505 foreach (JToken selectedToken
in tokens)
508 orgModule.
Parse(selectedToken);
509 orgModules.Add(orgModule);
513 Debug.Log(orgModules.Count.ToString());
async void SendHeartbeat(string authToken, int sessionId)
HttpClient handlingClient
APIHandler(string endpointUrl)
async void GetUserModules(string authToken, int userId)
async void QuickIDLogin(QuickIDLoginData login)
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 GetQuickIDAuthenticationUsers(string serialNumber)
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 GenerateAssistedLogin(string authToken)
async void GetModuleList(string authToken, string platform)
@ RT_QUICK_ID_AUTH_GET_USERS