/// <summary> /// Fetches list data with support for cached data and timeouts /// </summary> protected virtual Task <bool> FetchListAsync <T>(RequestToken requestToken, bool allowStale, string cachePrefixKey, string cacheSpecificKey, int staleLimit, FetchedRequestDelegate <ListResult <T> > onFetched, Action <bool> onFetching, Func <Task <ListResult <T> > > fetchMethod) { try { return(this.DataCache.WithTimedRefreshForPrefixAsync <ListResult <T> >(requestToken, allowStale, cachePrefixKey, cacheSpecificKey, staleLimit, onFetched, onFetching, async delegate() { return await fetchMethod(); })); } catch (Exception ex) { EndpointException endpointException = ex.FirstExceptionOfType <EndpointException>(); if (endpointException != null) { switch (endpointException.StatusCode) { case HttpStatusCode.BadRequest: case HttpStatusCode.Unauthorized: case HttpStatusCode.Forbidden: case HttpStatusCode.NotFound: case HttpStatusCode.RequestTimeout: case HttpStatusCode.InternalServerError: case HttpStatusCode.NotImplemented: case HttpStatusCode.ServiceUnavailable: this.ViewPlatform.ShowToast("Error connecting to server. Please check connection."); break; default: break; } } this.ProcessExecuteException(ex, "FetchListAsync"); throw; } }
protected virtual bool IsUnauthorized(Exception ex) { AggregateException aggregate = ex as AggregateException; if (aggregate != null) { foreach (var item in aggregate.InnerExceptions) { if (IsUnauthorized(item)) { return(true); } } } EndpointException endpointException = ex as EndpointException; if (endpointException != null) { if (endpointException.StatusCode == System.Net.HttpStatusCode.Unauthorized) { return(true); } } return(false); }
/// <summary> /// Does Not Throw Exceptions. /// </summary> public virtual Task <ItemResult <AccountInfo> > LoginAsyncSafe(AuthLoginInput info, Action <bool> onProcessing = null) { return(base.ExecuteFunctionAsync("LoginAsyncSafe", async delegate() { ItemResult <AccountInfo> result = new ItemResult <AccountInfo>() { success = false }; bool isOutdated = false; try { ItemResult <AccountInfo> loginResult = null; this.LogOff(false, false); loginResult = await this.PostItemUnSafeAsync(onProcessing, async delegate() { StencilSDK client = this.GetSDK(false); return await client.Auth.LoginAsync(info); }); if (loginResult.IsSuccess()) { result = loginResult; } else { result.success = false; result.meta = loginResult.meta; result.message = loginResult.GetMessage(); } } catch (Exception ex) { ex = ex.FirstNonAggregateException(); base.LogError(ex, "LoginAsyncSafe"); result.success = false; isOutdated = this.IsOutdated(ex); if (isOutdated) { this.Outdated(string.Empty); } result.message = ex.Message; EndpointException endpointException = ex as EndpointException; if (endpointException != null) { result.meta = ((int)endpointException.StatusCode).ToString(); } } if (result.IsSuccess()) { this.CacheHost.CachedUserSet(result.item); this.CurrentAccount = result.item; this.UserCacheClear(); AppPreferences prefs = new AppPreferences(); this.CacheHost.PersistentDataSet(false, CACHE_FILENAME_APP_PREFS, prefs); this.AppPreferences = prefs; this.ViewPlatform.OnLoggedOn(); } else { this.LogOff(!isOutdated, false); if (string.IsNullOrEmpty(result.message)) { result.message = "Could not log in to your account."; } } return result; })); }