internal void send(KerbalXAPI api, RequestCallback callback, bool authenticate = true) { if (String.IsNullOrEmpty(api.client_version) || String.IsNullOrEmpty(api.client)) { KXAPI.log("client info has not been set"); return; } if (authenticate) { if (api.logged_in) { set_header("token", KerbalXAPI.token); } else { KerbalXLoginUI.add_login_callback(api, (login_succsessful) => { if (login_succsessful) { this.send(api, callback, authenticate); } }); KerbalXLoginUI.open(); return; } } set_header("MODCLIENT", api.client); set_header("MODCLIENTVERSION", api.client_version); set_header("MODCLIENTSIGNITURE", api.client_signiture); set_header("KSPVERSION", Versioning.GetVersionString()); if (RequestHandler.instance == null) { KerbalXAPIHelper.instance.start_request_handler(); } RequestHandler.instance.send_request(api, request, callback); }
//Public Authentication methods //Login //usage: //KerbalXAPI api = new KerbalXAPI("mod name", "mod version"); //api.login((login_successful) => { // if(login_successful){ // //some action to run once logged in // } //}); //Can also be called without a callback: //api.login() // //If the API is already logged in, then the callback is called instantly with the argument given as True //If the API is not yet logged in, but a KerbalX.key (token) file exists, it authenticates the token with KerbalX and if it's valid the callback is called with True //If either the token is invalid or not present it opens the login UI. Once the user has logged in the callback is called with True. //The only time the callback will be called with False as the argument is if the user cancels the login process. public void login(AfterLoginCallback callback) { if (KerbalXAPIHelper.instance.on_main_menu) { if (logged_in) { callback(true); } else { KerbalXLoginUI.add_login_callback(this, callback); //callback is stashed in a Dictionary on the loginUI and will be called once login has completed or been canclled. } check_api_helper_state(); KerbalXLoginUI.open(); } else { if (logged_in) { callback(true); //call the callback instantly if the API is already logged in } else { KerbalXAPI kxapi = null; if (instances.ContainsKey("KerbalXAPI")) { kxapi = instances["KerbalXAPI"]; } else { kxapi = new KerbalXAPI("KerbalXAPI", KXAPI.version); } kxapi.login((resp, code) => { //validate the user's authentication token with KerbalX if (code == 200) { callback(true); //If the token is valid then call the callback } else { //If the token is either invalid or not present, trigger the LoginUI check_api_helper_state(); KerbalXLoginUI.add_login_callback(this, callback); //callback is stashed in a Dictionary on the loginUI and will be called once login has completed or been canclled. KerbalXLoginUI.open(); //Open the LoginUI (request made via the APIHelper which needs to have been started before this point). } }); } } }