public override async Task Execute() { await Authorize(); IPlatformResponse <IAuthorization> result = null; if (SimpleClient?.Authorization?.UserName == Username && SimpleClient?.Authorization?.Password == Password) { result = await SimpleClient.Login(SimpleClient.Authorization); } else { var auth = DIContainer.Current.Resolve <IAuthorization>(); auth.UserName = Username; auth.Password = Password; result = await SimpleClient.Login(auth); } if (!string.IsNullOrEmpty(AuthorizationFile) && result.Success && !string.IsNullOrEmpty(result?.Response?.AccessToken)) { File.WriteAllText(AuthorizationFile, _serialize.SerializeToString(result.Response)); result.Response.Refreshed = false; } Log.Debug(result); UpdateAuthorization(); }
private async Task <IPlatformResponse <T> > CacheHitOrMiss <T>(string key, Func <Task <IPlatformResponse <T> > > callback, TimeSpan?expiryTimeSpan = null) { if (expiryTimeSpan == null) { expiryTimeSpan = TimeSpan.FromMinutes(DefaultCacheExpiry); } var hitService = true; IPlatformResponse <T> response = null; if (_cache != null && await _cache.Exists(key)) { try { var cachedContent = await _cache.Get <IPlatformResponse <T> >(key); if (cachedContent != null) { cachedContent.Item.CacheHit = true; var dateDiff = DateTime.Now - cachedContent.StoredDateTime; if (expiryTimeSpan.Value.TotalMilliseconds <= 0 || dateDiff.TotalMilliseconds < expiryTimeSpan.Value.TotalMilliseconds) { hitService = false; response = cachedContent.Item; } } } catch (Exception e) { _log.Error(e, key); } } if (hitService) { response = await callback(); if (response.Success) { await _cache.Set(key, response); } } return(response); }
public async Task <IPlatformResponse <IVehiclesResponse> > Vehicles(int skip = 0, int top = 10, string filter = null, string select = null, string orderby = null, CancellationToken?cancellationToken = null, IProgress <ISDKProgress> progress = null, bool skipCache = false) { var tokenP = IssueNewTokenAndProgressContainer(cancellationToken, progress); if ((await Login(Authorization, cancellationToken, progress)).Success) { IPlatformResponse <IVehiclesResponse> vehicles = null; string path = $"v2/vehicles?{RandomQueryString()}"; if (skip > 0) { path = path + $"&skip={skip}"; } if (top > 0) { path = path + $"&top={top}"; } if (!string.IsNullOrEmpty(filter)) { path = path + $"&filter={WebUtility.UrlEncode(filter)}"; } if (!string.IsNullOrEmpty(select)) { path = path + $"&select={WebUtility.UrlEncode(select)}"; } if (!string.IsNullOrEmpty(orderby)) { path = path + $"&orderby={WebUtility.UrlEncode(orderby)}"; } if (!skipCache) { vehicles = await CacheHitOrMiss($"Vehicles.{Authorization.UserName}", () => _clientBuilder.Request <IVehiclesResponse>(ApiEndpoint.Api, path, tokenP.CancellationToken, tokenP.Progress), TimeSpan.FromMinutes(1)); } else { vehicles = await _clientBuilder.Request <IVehiclesResponse>(ApiEndpoint.Api, path, tokenP.CancellationToken, tokenP.Progress); } //if (vehicles != null && vehicles.Response != null && vehicles.Response.Data != null) //{ // var noImageVehicles = from v in vehicles.Response.Data where v.Image == null select v; // if (noImageVehicles != null) // { // foreach (var v in noImageVehicles) // { // var result = await VehicleImage(v.Id, tokenP.CancellationToken, tokenP.Progress); // if (result.Success && result.Response != null) // { // v.Image = result.Response; // } // } // } //} return(vehicles); } _log.Fatal(new Exception("Authorization Failed")); return(await Task.FromResult <IPlatformResponse <IVehiclesResponse> >(null)); }