Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
SendGrid: Report daily requests, deliveries, bounces, and blocks
See more SendGrid Examples
Retrieve seven completed UTC dates and print one row per date. The counts are account-level aggregates; a day’s delivered count does not identify which messages sent that day were delivered.
Chilkat Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkDateTimeW.h>
#include <CkJsonArrayW.h>
#include <CkJsonObjectW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkHttpW http;
http.put_RequireSslCertVerify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.put_AuthToken(L"SENDGRID_API_KEY");
CkHttpResponseW resp;
CkDateTimeW day;
day.SetFromCurrentSystemTime();
day.AddDays(-1);
http.SetUrlVar(L"end",day.getAsIso8601(L"YYYY-MM-DD",false));
day.AddDays(-6);
http.SetUrlVar(L"start",day.getAsIso8601(L"YYYY-MM-DD",false));
success = http.HttpNoBody(L"GET",L"https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
wprintf(L"%s\n",http.lastErrorText());
}
else {
wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 200) {
wprintf(L"HTTP %d: %s\n",resp.get_StatusCode(),resp.bodyStr());
return;
}
CkJsonArrayW days;
success = days.Load(resp.bodyStr());
if (success != true) {
wprintf(L"Unexpected response: expected JSON.\n");
return;
}
wprintf(L"Date,Requests,Delivered,Bounces,Blocks\n");
CkJsonObjectW row;
int i = 0;
while ((i < days.get_Size())) {
days.ObjectAt2(i,row);
wprintf(L"%s,%d,%d,%d,%d\n",row.stringOf(L"date"),row.IntOf(L"stats[0].metrics.requests"),row.IntOf(L"stats[0].metrics.delivered"),row.IntOf(L"stats[0].metrics.bounces")
,row.IntOf(L"stats[0].metrics.blocks"));
i = i + 1;
}
}