Rust
Rust
BatchModify - Add a Label to each Message in Search Results
See more GMail REST API Examples
Searchs GMail for messages meeting a criteria and adds a label to each message found.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
http.set_auth_token("GMAIL-ACCESS-TOKEN");
let user_id = "me".to_string();
let _ = http.set_url_var("userId", &user_id);
let query = "subject:questions".to_string();
let _ = http.set_url_var("query", &query);
let mut url = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages?q={$query}".to_string();
let sb = chilkat::StringBuilder::new();
if http.quick_get_sb(&url, &sb).is_err() {
println!("{}", http.last_error_text());
return;
}
let json = chilkat::JsonObject::new();
let _ = json.load_sb(&sb);
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
if http.last_status() != 200 {
println!("Failed.");
return;
}
// If successful, the received JSON looks like this:
// {
// "messages": [
// {
// "id": "166f583051d36144",
// "threadId": "166f583051d36144"
// },
// {
// "id": "166f5815e1f36144",
// "threadId": "166f5815e1f36144"
// },
// ...
// {
// "id": "166f580639e36144",
// "threadId": "166f580639e36144"
// },
// {
// "id": "15dbc2e28ec789c6",
// "threadId": "15dbc2e28ec789c6"
// }
// ],
// "nextPageToken": "13434766102274844688",
// "resultSizeEstimate": 103
// }
//
// Next, we'll be sending an HTTP POST to add the label "questions" to each message in the
// search results. The JSON to be sent for the batchModify is this:
// {
// "ids": [
// string
// ],
// "addLabelIds": [
// string
// ],
// "removeLabelIds": [
// string
// ]
// }
// We'll omit "removeLabelIds" because we're not removing any labels.
// We are parsing the JSON search results, and at the same time building the batchModify JSON.
let json2 = chilkat::JsonObject::new();
let mut i = 0;
let num_messages = json.size_of_array("messages");
while i < num_messages {
json.set_i(i);
let id = json.string_of("messages[i].id").unwrap_or_default();
json2.set_i(i);
let _ = json2.update_string("ids[i]", &id);
i = i + 1;
}
// We need the id of the label (not the name).
// I know the name of the label is "questions", but I need to know the id.
// See this example: Get Label Id by Name
// The id of my label named "questions" is "Label_43"
let label_id = "Label_43".to_string();
let _ = json2.update_string("addLabelIds[0]", &label_id);
let _ = json2.update_new_array("removeLabelIds");
json2.set_emit_compact(false);
println!("{}", json2.emit().unwrap_or_default());
// Send the batchModify
url = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/batchModify".to_string();
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", &url, &json2, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("status = {}", resp.status_code());
// A 204 response status indicate success.
if resp.status_code() != 204 {
println!("{}", resp.body_str());
println!("Failed.");
return;
}
// The 204 response has an empty response body..
println!("BatchModify success!");