Swift Requires Chilkat v11.0.0+
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
let http = CkoHttp()!
http.requireSslCertVerify = true
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.authToken = "SENDGRID_API_KEY"
let resp = CkoHttpResponse()!
let day = CkoDateTime()!
day.setFromCurrentSystemTime()
day.addDays(numDays: -1)
http.setUrlVar(name: "end", value: day.get(asIso8601: "YYYY-MM-DD", bLocal: false))
day.addDays(numDays: -6)
http.setUrlVar(name: "start", value: day.get(asIso8601: "YYYY-MM-DD", bLocal: false))
success = http.httpNoBody(verb: "GET", url: "https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day", response: resp)
if success != true {
if resp.statusCode.intValue == 0 {
print("\(http.lastErrorText!)")
}
else {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
}
return
}
if resp.statusCode.intValue != 200 {
print("HTTP \(resp.statusCode.intValue): \(resp.bodyStr!)")
return
}
let days = CkoJsonArray()!
success = days.load(jsonArray: resp.bodyStr)
if success != true {
print("Unexpected response: expected JSON.")
return
}
print("Date,Requests,Delivered,Bounces,Blocks")
let row = CkoJsonObject()!
var i: Int = 0
while (i < days.size.intValue) {
days.objectAt2(index: i, jsonObj: row)
print("\(row.string(of: "date")!),\(row.int(of: "stats[0].metrics.requests").intValue),\(row.int(of: "stats[0].metrics.delivered").intValue),\(row.int(of: "stats[0].metrics.bounces").intValue),\(row.int(of: "stats[0].metrics.blocks").intValue)")
i = i + 1
}
}