Skip to content

Instantly share code, notes, and snippets.

@thinkingserious
Last active August 29, 2015 14:05
Show Gist options
  • Save thinkingserious/b4247cd6e0ca5e1129a4 to your computer and use it in GitHub Desktop.
Save thinkingserious/b4247cd6e0ca5e1129a4 to your computer and use it in GitHub Desktop.
GetStats - a function that retrieves data from the SendGrid Web API stats endpoint from within a Windows Phone 8.1 app
public async Task<ObservableCollection<EmailStats>> GetStats(uint days = 1, uint aggregate = 0)
{
if (days < 1)
throw new ArgumentException("The days argument must be larger than 0.", "days");
if (days > 1095)
throw new ArgumentException("The days argument must be less than 1096.", "days");
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(
new Uri(String.Format("{0}stats.get.json?api_user={1}&api_key={2}&date=1&days={3}&aggregate={4}",
SendGridApiBase, SendGridUsername, SendGridPassword, days, aggregate)));
string responseBody = await response.Content.ReadAsStringAsync();
if (aggregate == 1)
responseBody = "[" + responseBody + "]";
return JsonConvert.DeserializeObject<ObservableCollection<EmailStats>>(responseBody);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment