private IEnumerator beginSave(Weapon weapon, LoginAuth loginAuth) { WeaponDTO dto = weapon.serialize(); string weaponJson = JsonUtility.ToJson(weapon); Debug.Log(weaponJson); byte[] weaponJsonByte = System.Text.Encoding.UTF8.GetBytes(weaponJson); //byte[] testData = System.Text.Encoding.UTF8.GetBytes("helloUnity"); UnityWebRequest www = UnityWebRequest.Put(saveWeaponUri + weapon.id, weaponJsonByte); www.SetRequestHeader("Authorization", "Token token=" + loginAuth.auth_token); // this is key for rails to know what kind of data to even think how to start parsing www.SetRequestHeader("Content-Type", "application/json"); www.downloadHandler = new DownloadHandlerBuffer(); yield return(www.SendWebRequest()); if (www.isNetworkError) { Debug.Log(www.error); } else { Debug.Log("weapon update success"); // Debug.Log (www.downloadHandler.text); NetworkResponses rd = JsonUtility.FromJson <NetworkResponses>(www.downloadHandler.text); Debug.Log(rd.success [0]); } }
/// <summary> /// Main login api. -- Remove save weapon we no longer need to save /// </summary> /// <param name="loginAuth">Login auth.</param> public void save(Weapon weapon, LoginAuth loginAuth) { if (loginAuth != null) { StartCoroutine(beginSave(weapon, loginAuth)); } else { Debug.Log("fail to save weapon, loginAuth is null"); } }
/// <summary> /// login /// </summary> /// <param name="loginAuth">Login auth.</param> public void login(LoginAuth loginAuth) { if (loginAuth != null) { StartCoroutine(beginLogin(loginAuth)); } else { Debug.Log("fail to login, loginAuth is null"); } }
public void getRandomWeapon(DungeonData dungeonData, LoginAuth loginAuth) { if (loginAuth != null) { Debug.Log("begin get weapon"); StartCoroutine(beginGetRandomWeapon(dungeonData, loginAuth)); } else { Debug.Log("fail to get random weapon, loginAuth is null"); } }
/// <summary> /// create new user using Post /// </summary> /// <returns>The new user creation.</returns> /// <param name="loginAuth">Login auth.</param> private IEnumerator beginNewUserCreation(LoginAuth loginAuth) { // WWWForm worked with creating new user, on rails side the escaped charc will be removed so that rails will work // if rails is expecting the data to be one layed nested, fix an if else on rails registeration side // by not requiring the "user" // first key param i.e. { "user": {"email": "*****@*****.**", "password": "******", "password_confirmation": "password" } } // fill the form - with Post on rail side, using the form, the escaped characters are removed which is good WWWForm form = new WWWForm(); form.AddField("email", loginAuth.email); form.AddField("password", loginAuth.pass); form.AddField("password_confirmation", loginAuth.pass); UnityWebRequest www = UnityWebRequest.Post(createUserUri, form); www.downloadHandler = new DownloadHandlerBuffer(); // no need for to set header content type // as these will only cause rails to fail to parse the string when using post calls // or use the default content type by not setting it at all // www.SetRequestHeader("Content-Type", "application/json"); //www.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded"); yield return(www.SendWebRequest()); // response - check the callback request if (www.isNetworkError) { Debug.Log(www.error); } else { string returnedJsonData = www.downloadHandler.text; // to handle the json deserialize into an object // TODO not sure how we want to handle return data yet from rails /// whether to use jsonUtility or a true jsonObject NetworkResponses rd = JsonUtility.FromJson <NetworkResponses>(returnedJsonData); // convert the response from json into real object temp auth //LoginAuth tempAuth = JsonConvert.DeserializeObject<LoginAuth>(returnedJsonData); // store the login auth for later // this.loginAuth = loginAuth; // this.loginAuth.auth_token = tempAuth.auth_token; Debug.Log("creation success"); Debug.Log(returnedJsonData); //Debug.Log (rd.email[0]); } }
/// <summary> /// Gets a deep clone copy of the login auth if exist. /// </summary> /// <returns>The login auth.</returns> public LoginAuth getLoginAuth() { LoginAuth loginClone = null; if (loginAuth != null) { loginClone = new LoginAuth(loginAuth.email, loginAuth.pass, loginAuth.user); loginClone.auth_token = loginAuth.auth_token; } else { Debug.LogWarning("Failed to get login, loginAuth is null"); } return(loginClone); }
private IEnumerator beginLogin(LoginAuth loginAuth) { Debug.Log("logging in test"); // fill the form WWWForm form = new WWWForm(); form.AddField("email", loginAuth.email); form.AddField("password", loginAuth.pass); if (loginUri == null || loginUri == "") { Debug.LogWarning("log error empty uri"); } // prepare the request UnityWebRequest www = UnityWebRequest.Post(loginUri, form); www.downloadHandler = new DownloadHandlerBuffer(); yield return(www.SendWebRequest()); // response - check the callback request if (www.isNetworkError) { Debug.Log(www.error); } else { string returnedJsonData = www.downloadHandler.text; // convert the response from json into real object temp auth LoginAuth tempAuth = JsonConvert.DeserializeObject <LoginAuth>(returnedJsonData); // store the login auth for later this.loginAuth = loginAuth; this.loginAuth.auth_token = tempAuth.auth_token; Debug.Log("login success"); Debug.Log(loginAuth.auth_token); } // after login inform a global state login is successful }
private IEnumerator beginGetRandomWeapon(DungeonData dungeonData, LoginAuth loginAuth) { // when using Post rest calls, use a form. // Note: on rails side, make sure to put an if else around the wrapper object WWWForm form = new WWWForm(); form.AddField("dungeon_level", dungeonData.dungeon_level); form.AddField("experience_points", dungeonData.experience_points); if (createRandomWeaponUri == null || createRandomWeaponUri == "") { Debug.LogError("createRandomWeaponUri is empty or null"); } else { Debug.Log("creating new weapon from server"); } UnityWebRequest www = UnityWebRequest.Post(createRandomWeaponUri, form); www.SetRequestHeader("Authorization", "Token token=" + loginAuth.auth_token); // NOte: for post, do not set the header content-type (only do it for put) // this is key for rails to know what kind of data to even think how to start parsing //www.SetRequestHeader("Content-Type", "application/json"); www.downloadHandler = new DownloadHandlerBuffer(); yield return(www.SendWebRequest()); if (www.isNetworkError) { Debug.Log(www.error); } else { Debug.Log("weapon update success: following is the server log"); Debug.Log(www.downloadHandler.text); //NetworkResponses rd = JsonUtility.FromJson<NetworkResponses>(www.downloadHandler.text); //Debug.Log (rd.success [0]); } }
public void createUser(LoginAuth loginAuth) { StartCoroutine(beginNewUserCreation(loginAuth)); }