Ejemplo n.º 1
0
        internal virtual void OnAuthorizationFailure(BuddyUnauthorizedException exception)
        {
            var count = Interlocked.Increment(ref _processingAuthFailure);

            if (count > 1)
            {
                return;
            }

            bool showLoginDialog = exception == null, exceptionCaught = false;

            try
            {
                if (exception != null)
                {
                    switch (exception.Error)
                    {
                    case "AuthAppCredentialsInvalid":
                    case "AuthAccessTokenInvalid":
                        ClearCredentials(false, true);
                        break;

                    case "AuthUserAccessTokenRequired":
                        ClearCredentials(true, false);
                        showLoginDialog = true;
                        break;
                    }
                }

                if (showLoginDialog && this.AuthorizationNeedsUserLogin != null)
                {
                    PlatformAccess.Current.InvokeOnUiThread(() =>
                    {
                        this.AuthorizationNeedsUserLogin(this, new EventArgs());

                        Interlocked.Exchange(ref _processingAuthFailure, 0);
                    });
                }
            }
            catch
            {
                exceptionCaught = true;

                throw;
            }
            finally
            {
                if (exceptionCaught || !showLoginDialog || this.AuthorizationNeedsUserLogin == null)
                {
                    Interlocked.Exchange(ref _processingAuthFailure, 0);
                }
            }
        }
Ejemplo n.º 2
0
        internal async Task <BuddyResult <T> > HandleServiceResult <T>(BuddyCallResult <T> serviceResult, bool allowThrow = false)
        {
            var result = new BuddyResult <T> ();

            result.RequestID = serviceResult.RequestID;
            if (serviceResult.Error != null)
            {
                BuddyServiceException buddyException = null;
                switch (serviceResult.StatusCode)
                {
                case 0:
                    buddyException = new BuddyNoInternetException(serviceResult.Error);
                    break;

                case 401:
                case 403:
                    buddyException = new BuddyUnauthorizedException(serviceResult.Error, serviceResult.Message, serviceResult.ErrorNumber);
                    break;

                default:
                    buddyException = new BuddySDK.BuddyServiceException(serviceResult.Error, serviceResult.Message, serviceResult.ErrorNumber);
                    break;
                }
                TaskCompletionSource <bool> uiPromise = new TaskCompletionSource <bool> ();
                PlatformAccess.Current.InvokeOnUiThread(async() => {
                    var r = false;
                    if (await OnServiceException(this, buddyException))
                    {
                        r = true;
                    }
                    uiPromise.TrySetResult(r);
                });
                if (await uiPromise.Task && allowThrow)
                {
                    throw buddyException;
                }
                buddyException.StatusCode = serviceResult.StatusCode;
                result.Error = buddyException;
            }
            else
            {
                result.Value = serviceResult.Result;
            }
            return(result);
        }