internal void logout() { api.logout((resp, code) => { enable_login = true; login_successful = false; username = ""; password = ""; }); }
//Used in all interacton with KerbalX, called from a Coroutine and handles the response error codes from the site private IEnumerator transmit(KerbalXAPI api, UnityWebRequest request, RequestCallback callback) { // last_request = null; // last_callback = null; // api_instance = api; api.server_error_message = null; api.failed_to_connect = false; api.upgrade_required = false; KXAPI.log("sending request to: " + request.url); yield return(request.Send()); if (request.isNetworkError) //Request Failed, most likely due to being unable to get a response, therefore no status code { api.failed_to_connect = true; KXAPI.log("request failed: " + request.error); // last_request = new UnityWebRequest(request.url, request.method); // \ create a copy of the request which is about to be sent // if(request.method != "GET"){ // | if the request fails because of inability to connect to site // last_request.uploadHandler = new UploadHandlerRaw(request.uploadHandler.data); // < then try_again() can be used to fire the copied request // } // | and the user can carry on from where they were when connection was lost. // last_request.downloadHandler = request.downloadHandler; // | upload and download handlers have to be duplicated too // last_callback = callback; // / and the callback is also stuffed into a var for reuse. } else { int status_code = (int)request.responseCode; //server responded - get status code KXAPI.log("request returned " + status_code + " " + status_codes[status_code.ToString()]); if (status_code == 500) //KerbalX server error { string error_message = "KerbalX server error!!\n" + //default error message incase server doesn't come back with something more helpful "An error has occurred on KerbalX (it was probably Jebs fault)"; var resp_data = JSON.Parse(request.downloadHandler.text); //read response message and assuming there is one change the error_message if (!(resp_data["error"] == null || resp_data["error"] == "")) { error_message = "KerbalX server error!!\n" + resp_data["error"]; } KXAPI.log(error_message); api.server_error_message = error_message; //Set the error_message on KerbalX, any open window will pick this up and render error dialog callback(request.downloadHandler.text, status_code); //Still call the callback, assumption is all callbacks will test status code for 200 before proceeding, this allows for further handling if needed } else if (status_code == 426) //426 - Upgrade Required, only for a major version change that makes past versions incompatible with the site's API { api.upgrade_required = true; var resp_data = JSON.Parse(request.downloadHandler.text); api.upgrade_required_message = resp_data["message"]; callback("", status_code); } else if (status_code == 401) //401s (Unauthorized) - response to the user's token not being recognized. { if (RequestHandler.show_401_message == true) //In the case of login/authenticate, the 401 message is not shown (handled by login dialog) { api.server_error_message = "Login to KerbalX.com failed"; api.logout((resp, code) => {}); } callback(request.downloadHandler.text, status_code); } else if (status_code == 200 || status_code == 400 || status_code == 422) //Error codes returned for OK and failed validations which are handled by the requesting method { callback(request.downloadHandler.text, status_code); } else //Unhandled error codes - All other error codes. { api.server_error_message = "Error " + status_code + "\n"; var resp_data = JSON.Parse(request.downloadHandler.text); if (!(resp_data["error"] == null || resp_data["error"] == "")) { api.server_error_message += resp_data["error"]; } else if (!(resp_data["message"] == null || resp_data["message"] == "")) { api.server_error_message += resp_data["message"]; } else { api.server_error_message += request.downloadHandler.text; } callback(request.downloadHandler.text, status_code); } request.Dispose(); RequestHandler.show_401_message = true; } }