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 <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkDateTimeW.h>
#include <C_CkJsonArrayW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkHttpResponseW resp;
HCkDateTimeW day;
HCkJsonArrayW days;
HCkJsonObjectW row;
int i;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = FALSE;
http = CkHttpW_Create();
CkHttpW_putRequireSslCertVerify(http,TRUE);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
CkHttpW_putAuthToken(http,L"SENDGRID_API_KEY");
resp = CkHttpResponseW_Create();
day = CkDateTimeW_Create();
CkDateTimeW_SetFromCurrentSystemTime(day);
CkDateTimeW_AddDays(day,-1);
CkHttpW_SetUrlVar(http,L"end",CkDateTimeW_getAsIso8601(day,L"YYYY-MM-DD",FALSE));
CkDateTimeW_AddDays(day,-6);
CkHttpW_SetUrlVar(http,L"start",CkDateTimeW_getAsIso8601(day,L"YYYY-MM-DD",FALSE));
success = CkHttpW_HttpNoBody(http,L"GET",L"https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day",resp);
if (success != TRUE) {
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
}
else {
wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
}
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkDateTimeW_Dispose(day);
return;
}
if (CkHttpResponseW_getStatusCode(resp) != 200) {
wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkDateTimeW_Dispose(day);
return;
}
days = CkJsonArrayW_Create();
success = CkJsonArrayW_Load(days,CkHttpResponseW_bodyStr(resp));
if (success != TRUE) {
wprintf(L"Unexpected response: expected JSON.\n");
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkDateTimeW_Dispose(day);
CkJsonArrayW_Dispose(days);
return;
}
wprintf(L"Date,Requests,Delivered,Bounces,Blocks\n");
row = CkJsonObjectW_Create();
i = 0;
while ((i < CkJsonArrayW_getSize(days))) {
CkJsonArrayW_ObjectAt2(days,i,row);
wprintf(L"%s,%d,%d,%d,%d\n",CkJsonObjectW_stringOf(row,L"date"),CkJsonObjectW_IntOf(row,L"stats[0].metrics.requests"),CkJsonObjectW_IntOf(row,L"stats[0].metrics.delivered")
,CkJsonObjectW_IntOf(row,L"stats[0].metrics.bounces"),CkJsonObjectW_IntOf(row,L"stats[0].metrics.blocks"));
i = i + 1;
}
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkDateTimeW_Dispose(day);
CkJsonArrayW_Dispose(days);
CkJsonObjectW_Dispose(row);
}