Exemple #1
0
        /// <summary>
        /// Obtain token using itSeez3D username and password. Not for production use!
        /// </summary>
        private AsyncWebRequest <AccessData> AuthorizePasswordGrantTypeAsync(
            string clientId,
            string clientSecret,
            string username,
            string password
            )
        {
            Debug.LogWarning("Don't use this auth method in production, use other grant types!");
            var request = new AsyncWebRequest <AccessData> (AvatarSdkMgr.Str(Strings.RequestingApiToken));

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(clientId))
            {
                request.SetError("itSeez3D credentials not provided");
                Debug.LogError(request.ErrorMessage);
                return(request);
            }

            var form = new Dictionary <string, string> ()
            {
                { "grant_type", "password" },
                { "username", username },
                { "password", password },
                { "client_id", clientId },
                { "client_secret", clientSecret },
            };
            Func <UnityWebRequest> webRequestFactory = () => HttpPost(GetUrl("o", "token"), form);

            AvatarSdkMgr.SpawnCoroutine(AwaitJsonWebRequest(webRequestFactory, request));
            return(request);
        }
Exemple #2
0
        /// <summary>
        /// Send the request, wait till it finishes and process the result.
        /// </summary>
        /// <returns>The web request func.</returns>
        /// <param name="webRequestFactory">Function that returns the new copy of UnityWebRequest
        /// (needed for retry).</param>
        /// <param name="request">"Parent" async request object to report progress to.</param>
        /// <param name="parseDataFunc">Function that takes care of parsing data (usually JSON parsing).</param>
        private IEnumerator AwaitWebRequestFunc <T> (
            Func <UnityWebRequest> webRequestFactory,
            AsyncWebRequest <T> request,
            Func <UnityWebRequest, T> parseDataFunc
            )
        {
            UnityWebRequest webRequest = null;

            StatusCode status = new StatusCode();
            string     error  = string.Empty;

            int  numAttempts = 2, lastAttempt = numAttempts - 1;
            bool goodResponse = false;

            for (int attempt = 0; attempt < numAttempts; ++attempt)
            {
                webRequest = webRequestFactory();
                yield return(AwaitAndTrackProgress(webRequest, request));

                if (goodResponse = IsGoodResponse(webRequest, out status, out error))
                {
                    break;
                }

                // all API requests have Authorization header, except for authorization requests
                bool isAuthRequest = webRequest.GetRequestHeader("Authorization") == null;

                Debug.LogWarningFormat("Server error: {0}, request: {1}", error, webRequest.url);
                if (status.Value != (long)StatusCode.Code.UNAUTHORIZED || isAuthRequest)
                {
                    // cannot recover, request has failed
                    break;
                }

                if (attempt == lastAttempt)
                {
                    Debug.LogError("No more retries left");
                    break;
                }

                Debug.LogWarning("Auth issue, let's try one more time after refreshing access token");
                yield return(AuthorizeAsync());
            }

            if (!goodResponse)
            {
                Debug.LogErrorFormat("Could not send the request, status: {0}, error: {1}", status, error);
                request.Status = status;
                request.SetError(error);
                yield break;
            }

            T data = default(T);

            try {
                data = parseDataFunc(webRequest);
            } catch (Exception ex) {
                Debug.LogException(ex);
            }

            if (data == null)
            {
                request.SetError("Could not parse request data");
                yield break;
            }
            else
            {
                request.Result = data;
            }

            request.IsDone = true;
        }