Last active
January 19, 2018 18:32
-
-
Save jamesmontemagno/bb6ffdc727b1c5acda03af1ff2341cb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<T> GetAsync<T>(string url, int days = 7, bool forceRefresh = false) | |
{ | |
var json = string.Empty; | |
//check if we are connected, else check to see if we have valid data | |
if (!CrossConnectivity.Current.IsConnected) | |
json = Barrel.Current.Get(url); | |
else if (!forceRefresh && !Barrel.Current.IsExpired(url)) | |
json = Barrel.Current.Get(url); | |
try | |
{ | |
//skip web request because we are using cached data | |
if (string.IsNullOrWhiteSpace(json)) | |
{ | |
json = await client.GetStringAsync(url); | |
Barrel.Current.Add(url, json, TimeSpan.FromDays(days)); | |
} | |
return await Task.Run(() => JsonConvert.DeserializeObject<T>(json)); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Unable to get information from server {ex}"); | |
//probably re-throw here :) | |
} | |
return default(T); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment