Skip to content

Instantly share code, notes, and snippets.

@jamesmontemagno
Last active January 19, 2018 18:32
Show Gist options
  • Save jamesmontemagno/bb6ffdc727b1c5acda03af1ff2341cb1 to your computer and use it in GitHub Desktop.
Save jamesmontemagno/bb6ffdc727b1c5acda03af1ff2341cb1 to your computer and use it in GitHub Desktop.
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