Exemplo n.º 1
0
        /// <summary>
        ///     Updates the user profile with the given id.
        /// </summary>
        /// <param name="id">The profile's id</param>
        /// <param name="preferenceTerms">The updated preference terms to save.</param>
        /// <param name="onCompletion">The completion handler returns whether the update of the profile succeeded.</param>
        internal bool UpdateProfile(string id, PreferenceTerms preferenceTerms, OnCompletion <object> onCompletion)
        {
            if (_loginResponse?.Token == null)
            {
                Debug.Log("You need to login first!");
                onCompletion(false, null);
                return(false); // TODO remove
            }

            if (!_loginResponse.IsValid)
            {
                Debug.Log("Login has expired!");
                onCompletion(false, null);
                return(false); // TODO remove
            }

            _parent.StartChildCoroutine(_UpdateProfileCoroutine(id, preferenceTerms, response => { onCompletion(true, null); }, message =>
            {
                Debug.Log("An error occured while updating user profile...");
                Debug.Log(message);
                onCompletion(false, null);
            }));

            return(true); // TODO remove
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Handles the updating of a profile as a coroutine.
        /// </summary>
        /// <param name="id">The profile's id</param>
        /// <param name="preferenceTerms">The new and updated preference terms.</param>
        /// <param name="onResponseReceived">The handler to call on success.</param>
        /// <param name="onErrorReceived">The handler to call on error.</param>
        /// <returns>An enumerator.</returns>
        private IEnumerator _UpdateProfileCoroutine(string id, PreferenceTerms preferenceTerms, OnResponseReceived onResponseReceived, OnErrorReceived onErrorReceived)
        {
            using (var req = UnityWebRequest.Put(BaseUrl + "api/user-contexts/" + id, JsonConvert.SerializeObject(preferenceTerms)))
            {
                req.SetRequestHeader("content-type", "application/json");
                req.SetRequestHeader("authorization", _loginResponse.Token);
                req.downloadHandler = new DownloadHandlerBuffer();
                yield return(req.SendWebRequest());

                if (req.isNetworkError || req.isHttpError)
                {
                    onErrorReceived(req.error);
                }
                else
                {
                    onResponseReceived(req.downloadHandler.text);
                }
            }
        }