Swift Requires Chilkat v11.0.0+
Swift
Inspect the MIME parts of a Gmail message
See more GMail REST API Examples
Supply a Gmail message ID and request format=full. Walk the root payload and every nested parts array, including a single-part root. For each part, show its MIME type, filename, and size. A body.attachmentId can be passed to the attachment download example. Inline body.data is already included as base64url.
Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let token = CkoJsonObject()!
success = token.loadFile(path: "gmail-token.json")
if success != true {
print("Could not read gmail-token.json from the private working directory.")
return
}
if (token.jsonType(of: "access_token").intValue != 1) || (token.string(ofEquals: "access_token", value: "", caseSensitive: true) == true) {
print("An OAuth access token is required.")
return
}
let http = CkoHttp()!
http.requireSslCertVerify = true
http.authToken = token.string(of: "access_token")
let resp = CkoHttpResponse()!
let reply = CkoJsonObject()!
let url = CkoStringBuilder()!
url.append(value: "https://gmail.googleapis.com/gmail/v1/users/me/messages/")
url.append(value: "GMAIL_MESSAGE_ID")
url.append(value: "?format=full")
success = http.httpNoBody(verb: "GET", url: url.getAsString(), response: resp)
if resp.statusCode.intValue == 0 {
print("Gmail did not return an HTTP response.")
return
}
if (success != true) || (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
print("Gmail HTTP \(resp.statusCode.intValue)")
success = reply.load(json: resp.bodyStr)
if success == true {
print("\(reply.string(of: "error.message")!)")
}
return
}
success = reply.load(json: resp.bodyStr)
if success != true {
print("Expected a Gmail JSON response.")
return
}
if reply.jsonType(of: "payload").intValue != 3 {
print("Expected a MIME payload.")
return
}
// Queue paths into the original JSON document so any MIME nesting depth is supported.
let paths = CkoStringTable()!
success = paths.append(value: "payload")
if success != true {
print("Could not queue the root part.")
return
}
let part = CkoJsonObject()!
let childPath = CkoStringBuilder()!
var p: Int = 0
while (p < paths.count.intValue) {
success = reply.objectOf2(jsonPath: paths.string(at: p), jsonObj: part)
if success != true {
print("Could not read a MIME part.")
return
}
print("Part: \(part.string(of: "mimeType")!) bytes: \(part.int(of: "body.size").intValue)")
if part.jsonType(of: "filename").intValue == 1 {
if part.string(ofEquals: "filename", value: "", caseSensitive: true) != true {
print("Filename: \(part.string(of: "filename")!)")
}
}
if part.jsonType(of: "body.attachmentId").intValue == 1 {
print("Attachment ID: \(part.string(of: "body.attachmentId")!)")
}
else {
if part.jsonType(of: "body.data").intValue == 1 {
print("Body data is available in this part.")
}
}
var j: Int = 0
while (j < part.size(ofArray: "parts").intValue) {
childPath.setString(value: paths.string(at: p))
childPath.append(value: ".parts[")
childPath.appendInt(value: j)
childPath.append(value: "]")
success = paths.append(value: childPath.getAsString())
if success != true {
print("Could not queue a child part.")
return
}
j = j + 1
}
p = p + 1
}
}