protected override void RefreshTokenAndCompleteCall(LootLockerServerRequest cacheServerRequest, Action <LootLockerResponse> OnServerResponse) { if (activeConfig != null && activeConfig.platform == LootLockerGenericConfig.platformType.Steam) { LootLockerSDKManager.DebugMessage("Token has expired, And token refresh not supported in Steam calls", true); LootLockerResponse res = new LootLockerResponse(); res.statusCode = 401; res.Error = "Token Expired"; res.hasError = true; OnServerResponse?.Invoke(res); return; } var sessionRequest = new LootLockerSessionRequest(activeConfig.deviceID); LootLockerAPIManager.Session(sessionRequest, (response) => { if (response.success) { Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("x-session-token", activeConfig.token); cacheServerRequest.extraHeaders = headers; if (cacheServerRequest.retryCount < 4) { SendRequest(cacheServerRequest, OnServerResponse); cacheServerRequest.retryCount++; } else { LootLockerSDKManager.DebugMessage("Session refresh failed", true); LootLockerResponse res = new LootLockerResponse(); res.statusCode = 401; res.Error = "Token Expired"; res.hasError = true; OnServerResponse?.Invoke(res); } } else { LootLockerSDKManager.DebugMessage("Session refresh failed", true); LootLockerResponse res = new LootLockerResponse(); res.statusCode = 401; res.Error = "Token Expired"; res.hasError = true; OnServerResponse?.Invoke(res); } }); }
public static void SubmittingACrashLog(LootLockerSubmittingACrashLogRequest data, Action <LootLockerResponse> onComplete) { EndPointClass requestEndPoint = LootLockerEndPoints.submittingACrashLog; Dictionary <string, string> formData = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(data.game_version)) { formData.Add("game_version", data.game_version); } if (!string.IsNullOrEmpty(data.type_identifier)) { formData.Add("type_identifier", data.type_identifier); } if (!string.IsNullOrEmpty(data.local_crash_time)) { formData.Add("local_crash_time", data.local_crash_time); } if (string.IsNullOrEmpty(data.logFileName)) { string[] splitFilePath = data.logFilePath.Split(new char[] { '\\', '/' }); string defaultFileName = splitFilePath[splitFilePath.Length - 1]; data.logFileName = defaultFileName; } LootLockerServerRequest.UploadFile(requestEndPoint.endPoint, requestEndPoint.httpMethod, System.IO.File.ReadAllBytes(data.logFilePath), data.logFileName, "application/zip", formData, onComplete: (serverResponse) => { LootLockerResponse response = new LootLockerResponse(); if (string.IsNullOrEmpty(serverResponse.Error)) { LootLockerSDKManager.DebugMessage(serverResponse.text); response = JsonConvert.DeserializeObject <LootLockerResponse>(serverResponse.text); onComplete?.Invoke(response); } else { response.success = serverResponse.success; response.Error = serverResponse.Error; response.statusCode = serverResponse.statusCode; onComplete?.Invoke(response); } }, useAuthToken: false); }
public static void WhiteLabelRequestAccountVerification(int userID, Action <LootLockerResponse> onComplete) { EndPointClass endPoint = LootLockerEndPoints.whiteLabelRequestAccountVerification; var json = JsonConvert.SerializeObject(new { user_id = userID }); LootLockerServerRequest.CallDomainAuthAPI(endPoint.endPoint, endPoint.httpMethod, json, (serverResponse) => { LootLockerResponse response = new LootLockerResponse { text = serverResponse.text, success = serverResponse.success, Error = serverResponse.Error, statusCode = serverResponse.statusCode }; onComplete?.Invoke(response); }); }
public static void WhiteLabelRequestPasswordReset(string email, Action <LootLockerResponse> onComplete) { EndPointClass endPoint = LootLockerEndPoints.whiteLabelRequestPasswordReset; var json = JsonConvert.SerializeObject(new { email }); LootLockerServerRequest.CallDomainAuthAPI(endPoint.endPoint, endPoint.httpMethod, json, (serverResponse) => { LootLockerResponse response = new LootLockerResponse { text = serverResponse.text, success = serverResponse.success, Error = serverResponse.Error, statusCode = serverResponse.statusCode }; onComplete?.Invoke(response); }); }
public void SendRequest(LootLockerServerRequest request, System.Action <LootLockerResponse> OnServerResponse = null) { StartCoroutine(coroutine()); IEnumerator coroutine() { //Always wait 1 frame before starting any request to the server to make sure the requesters code has exited the main thread. yield return(null); //Build the URL that we will hit based on the specified endpoint, query params, etc string url = BuildURL(request.endpoint, request.queryParams); #if UNITY_EDITOR LootLockerSDKManager.DebugMessage("ServerRequest URL: " + url); #endif using (UnityWebRequest webRequest = CreateWebRequest(url, request)) { webRequest.downloadHandler = new DownloadHandlerBuffer(); float startTime = Time.time; float maxTimeOut = 5f; yield return(webRequest.SendWebRequest()); while (!webRequest.isDone) { yield return(null); if (Time.time - startTime >= maxTimeOut) { LootLockerSDKManager.DebugMessage("ERROR: Exceeded maxTimeOut waiting for a response from " + request.httpMethod.ToString() + " " + url); yield break; } } if (!webRequest.isDone) { OnServerResponse?.Invoke(new LootLockerResponse() { hasError = true, statusCode = 408, Error = "{\"error\": \"" + request.endpoint + " Timed out.\"}" }); yield break; } try { #if UNITY_EDITOR LootLockerSDKManager.DebugMessage("Server Response: " + request.httpMethod + " " + request.endpoint + " completed in " + (Time.time - startTime).ToString("n4") + " secs.\nResponse: " + webRequest.downloadHandler.text); #endif } catch { LootLockerSDKManager.DebugMessage(request.httpMethod.ToString(), true); LootLockerSDKManager.DebugMessage(request.endpoint, true); LootLockerSDKManager.DebugMessage(webRequest.downloadHandler.text, true); } LootLockerResponse response = new LootLockerResponse(); response.statusCode = (int)webRequest.responseCode; if (webRequest.isHttpError || webRequest.isNetworkError || !string.IsNullOrEmpty(webRequest.error)) { switch (webRequest.responseCode) { case 200: response.Error = ""; break; case 400: response.Error = "Bad Request -- Your request has an error"; break; case 402: response.Error = "Payment Required -- Payment failed. Insufficient funds, etc."; break; case 401: response.Error = "Unauthroized -- Your session_token is invalid"; break; case 403: response.Error = "Forbidden -- You do not have access"; break; case 404: response.Error = "Not Found"; break; case 405: response.Error = "Method Not Allowed"; break; case 406: response.Error = "Not Acceptable -- Purchasing is disabled"; break; case 409: response.Error = "Conflict -- Your state is most likely not aligned with the servers."; break; case 429: response.Error = "Too Many Requests -- You're being limited for sending too many requests too quickly."; break; case 500: response.Error = "Internal Server Error -- We had a problem with our server. Try again later."; break; case 503: response.Error = "Service Unavailable -- We're either offline for maintenance, or an error that should be solvable by calling again later was triggered."; break; } #if UNITY_EDITOR LootLockerSDKManager.DebugMessage("Response code: " + webRequest.responseCode); #endif if (webRequest.responseCode != 401 || !activeConfig.allowTokenRefresh) { response.Error += " " + webRequest.downloadHandler.text; response.text = webRequest.downloadHandler.text; } else { RefreshTokenAndCompleteCall(request, OnServerResponse); } response.status = false; response.hasError = true; response.text = webRequest.downloadHandler.text; OnServerResponse?.Invoke(response); } else { response.status = true; response.hasError = false; response.text = webRequest.downloadHandler.text; OnServerResponse?.Invoke(response); } } } }
protected override void RefreshTokenAndCompleteCall(LootLockerServerRequest cacheServerRequest, Action <LootLockerResponse> OnServerResponse) { string platform = LootLockerSDKManager.GetCurrentPlatform(); if (platform == Platforms.Steam) { LootLockerSDKManager.DebugMessage("Token has expired and token refresh is not supported for Steam", true); LootLockerResponse res = new LootLockerResponse(); res.statusCode = 401; res.Error = "Token Expired"; res.hasError = true; OnServerResponse?.Invoke(res); return; } if (platform == Platforms.NintendoSwitch) { LootLockerSDKManager.DebugMessage("Token has expired and token refresh is not supported for Nintendo Switch", true); LootLockerResponse res = new LootLockerResponse(); res.statusCode = 401; res.Error = "Token Expired"; res.hasError = true; OnServerResponse?.Invoke(res); return; } if (platform == Platforms.Guest) { LootLockerSDKManager.StartGuestSession(response => { CompleteCall(cacheServerRequest, OnServerResponse, response); }); return; } else if (platform == Platforms.WhiteLabel) { LootLockerSDKManager.StartWhiteLabelSession(response => { CompleteCall(cacheServerRequest, OnServerResponse, response); }); return; } else { var sessionRequest = new LootLockerSessionRequest(LootLockerConfig.current.deviceID); LootLockerAPIManager.Session(sessionRequest, (response) => { CompleteCall(cacheServerRequest, OnServerResponse, response); }); } void CompleteCall(LootLockerServerRequest newcacheServerRequest, Action <LootLockerResponse> newOnServerResponse, LootLockerSessionResponse response) { if (response.success) { Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("x-session-token", LootLockerConfig.current.token); newcacheServerRequest.extraHeaders = headers; if (newcacheServerRequest.retryCount < 4) { SendRequest(newcacheServerRequest, newOnServerResponse); newcacheServerRequest.retryCount++; } else { LootLockerSDKManager.DebugMessage("Session refresh failed", true); LootLockerResponse res = new LootLockerResponse(); res.statusCode = 401; res.Error = "Token Expired"; res.hasError = true; newOnServerResponse?.Invoke(res); } } else { LootLockerSDKManager.DebugMessage("Session refresh failed", true); LootLockerResponse res = new LootLockerResponse(); res.statusCode = 401; res.Error = "Token Expired"; res.hasError = true; newOnServerResponse?.Invoke(res); } } }