Пример #1
0
        /// <summary>
        /// Called when sign in finishes.
        /// </summary>
        /// <param name="wasInteractive">If true, this was the interactive (browser-based) sign-in flow.</param>
        /// <param name="status">The result of the sign in process.</param>
        private void OnSignInFinished(bool wasInteractive, PolyStatus status)
        {
            if (status.ok)
            {
                string tok = PolyApi.AccessToken;
                PtDebug.LogFormat("ABM: Sign in success. Access token: {0}",
                                  (tok != null && tok.Length > 6) ? tok.Substring(0, 6) + "..." : "INVALID");
                PtAnalytics.SendEvent(PtAnalytics.Action.ACCOUNT_SIGN_IN_SUCCESS);
            }
            else if (wasInteractive)
            {
                Debug.LogErrorFormat("Failed to sign in. Please try again: " + status);
                PtAnalytics.SendEvent(PtAnalytics.Action.ACCOUNT_SIGN_IN_FAILURE, status.ToString());
            }
            if (null != refreshCallback)
            {
                refreshCallback();
            }

            // If we had a deferred request that was waiting for auth, send it now.
            if (requestToSendAfterAuth != null)
            {
                PtDebug.Log("Sending deferred request that was waiting for auth.");
                PolyRequest request = requestToSendAfterAuth;
                requestToSendAfterAuth = null;
                StartRequest(request);
            }
        }
Пример #2
0
 /// <summary>
 /// Fetches a list of Poly assets together with metadata, using the given request params.
 /// </summary>
 /// <param name="request">The request to send; can be either a ListAssetsRequest, a ListUserAssetsRequest, or
 /// a ListLikedAssetsRequest.</param>
 /// <param name="callback">The callback to call when the request is complete.</param>
 /// <param name="maxCacheAge">The maximum cache age to use.</param>
 /// <param name="isRecursion"> If true, this is a recursive call to this function, and no
 /// further retries should be attempted.</param>
 public void SendRequest(PolyRequest request, Action <PolyStatus, PolyListAssetsResult> callback,
                         long maxCacheAge = DEFAULT_QUERY_CACHE_MAX_AGE_MILLIS, bool isRecursion = false)
 {
     PolyMainInternal.Instance.webRequestManager.EnqueueRequest(
         () => { return(GetRequest(MakeSearchUrl(request), "text/text")); },
         (PolyStatus status, int responseCode, byte[] response) =>
     {
         // Retry the request if this was the first failure. The failure may be a server blip, or may indicate
         // an authentication token has become stale and must be refreshed.
         if (responseCode == 401 || !status.ok)
         {
             if (isRecursion /* || !Authenticator.IsInitialized || !Authenticator.Instance.IsAuthenticated*/)
             {
                 callback(PolyStatus.Error(status, "Query error ({0})", responseCode), null);
                 return;
             }
             //else
             //{
             //    Authenticator.Instance.Reauthorize((PolyStatus reauthStatus) =>
             //    {
             //        if (reauthStatus.ok)
             //        {
             //            SendRequest(request, callback, maxCacheAge: maxCacheAge, isRecursion: true);
             //        }
             //        else
             //        {
             //            callback(PolyStatus.Error(reauthStatus, "Failed to reauthorize."), null);
             //        }
             //    });
             //}
         }
         else
         {
             string text = Encoding.UTF8.GetString(response);
             PolyStatus responseStatus = CheckResponseForError(text);
             if (!responseStatus.ok)
             {
                 callback(responseStatus, null);
                 return;
             }
             PolyMainInternal.Instance.DoBackgroundWork(new ParseAssetsBackgroundWork(
                                                            text, callback));
         }
     }, maxCacheAge);
 }
Пример #3
0
        /// <summary>
        /// Starts a new request. If there is already an existing request in progress, it will be cancelled.
        /// </summary>
        /// <param name="request">The request parameters; can be either a ListAssetsRequest or
        /// a PolyListUserAssetsRequest.</param>
        /// <param name="callback"> The callback to invoke when the request finishes.</param>
        private void StartRequest(PolyRequest request, Action <PolyStatusOr <PolyListAssetsResult> > callback)
        {
            int thisQueryId = PrepareForNewQuery(); // for the closure below.

            currentRequest = request;

            if (request is PolyListAssetsRequest)
            {
                PolyListAssetsRequest listAssetsRequest = request as PolyListAssetsRequest;
                PolyApi.ListAssets(listAssetsRequest, (PolyStatusOr <PolyListAssetsResult> result) => {
                    // Only process result if this is indeed the most recent query that we issued.
                    // If we have issued another query since (in which case thisQueryId < queryId),
                    // then ignore the result.
                    if (thisQueryId == queryId && callback != null)
                    {
                        callback(result);
                    }
                });
            }
            else if (request is PolyListUserAssetsRequest)
            {
                PolyListUserAssetsRequest listUserAssetsRequest = request as PolyListUserAssetsRequest;
                PolyApi.ListUserAssets(listUserAssetsRequest, (PolyStatusOr <PolyListAssetsResult> result) => {
                    if (thisQueryId == queryId && callback != null)
                    {
                        callback(result);
                    }
                });
            }
            else if (request is PolyListLikedAssetsRequest)
            {
                PolyListLikedAssetsRequest listLikedAssetsRequest = request as PolyListLikedAssetsRequest;
                PolyApi.ListLikedAssets(listLikedAssetsRequest, (PolyStatusOr <PolyListAssetsResult> result) => {
                    if (thisQueryId == queryId && callback != null)
                    {
                        callback(result);
                    }
                });
            }
            else
            {
                Debug.LogError("Request failed. Must be either a PolyListAssetsRequest or PolyListUserAssetsRequest");
            }
        }
Пример #4
0
        public AssetBrowserManager(PolyRequest request)
        {
            PtDebug.Log("ABM initializing...");
            EnsurePolyIsReady();

            // If this is a request that needs authentication and we are in the process of authenticating,
            // wait until we're finished.
            bool needAuth = request is PolyListLikedAssetsRequest || request is PolyListUserAssetsRequest;

            if (needAuth && waitingForSilentAuth)
            {
                // Defer the request. Wait until auth is complete.
                PtDebug.Log("ABM: Deferring request until after auth.");
                requestToSendAfterAuth = request;
                return;
            }

            StartRequest(request);
        }
Пример #5
0
 private static string MakeSearchUrl(PolyRequest request)
 {
     if (request is PolyListAssetsRequest)
     {
         return(MakeSearchUrl(request as PolyListAssetsRequest));
     }
     else if (request is PolyListUserAssetsRequest)
     {
         return(MakeSearchUrl(request as PolyListUserAssetsRequest));
     }
     else if (request is PolyListLikedAssetsRequest)
     {
         return(MakeSearchUrl(request as PolyListLikedAssetsRequest));
     }
     else
     {
         throw new Exception("Must be a valid request type.");
     }
 }
Пример #6
0
 /// <summary>
 /// Starts a new request. If there is already an existing request in progress, it will be cancelled.
 /// </summary>
 /// <param name="request">The request parameters; can be either a ListAssetsRequest or
 /// a PolyListUserAssetsRequest.</param>
 public void StartRequest(PolyRequest request)
 {
     StartRequest(request, OnRequestResult);
 }