CkPython Requires Chilkat v11.0.0+
CkPython
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 CkPython Downloads
import sys
import chilkat
success = False
# Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False
http = chilkat.CkHttp()
http.put_RequireSslCertVerify(True)
# Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.put_AuthToken("SENDGRID_API_KEY")
resp = chilkat.CkHttpResponse()
day = chilkat.CkDateTime()
day.SetFromCurrentSystemTime()
day.AddDays(-1)
http.SetUrlVar("end",day.getAsIso8601("YYYY-MM-DD",False))
day.AddDays(-6)
http.SetUrlVar("start",day.getAsIso8601("YYYY-MM-DD",False))
success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day",resp)
if (success != True):
if (resp.get_StatusCode() == 0):
print(http.lastErrorText())
else:
print("HTTP " + str(resp.get_StatusCode()) + ": " + resp.bodyStr())
sys.exit()
if (resp.get_StatusCode() != 200):
print("HTTP " + str(resp.get_StatusCode()) + ": " + resp.bodyStr())
sys.exit()
days = chilkat.CkJsonArray()
success = days.Load(resp.bodyStr())
if (success != True):
print("Unexpected response: expected JSON.")
sys.exit()
print("Date,Requests,Delivered,Bounces,Blocks")
row = chilkat.CkJsonObject()
i = 0
while (i < days.get_Size()) :
days.ObjectAt2(i,row)
print(row.stringOf("date") + "," + str(row.IntOf("stats[0].metrics.requests")) + "," + str(row.IntOf("stats[0].metrics.delivered")) + "," + str(row.IntOf("stats[0].metrics.bounces"))\
+ "," + str(row.IntOf("stats[0].metrics.blocks")))
i = i + 1