···
is_unread_keyword || is_not_seen
+
(** Example function demonstrating how to use result references for chained requests *)
+
let demo_result_references conn account_id =
+
(* Create a request that chains the following operations:
+
2. Query emails in the first mailbox found
+
3. Get the full email objects for those IDs
+
(* Create method call IDs *)
+
let mailbox_get_id = "mailboxGet" in
+
let email_query_id = "emailQuery" in
+
let email_get_id = "emailGet" in
+
(* First call: Get mailboxes *)
+
let mailbox_get_call = {
+
("accountId", `String account_id);
+
method_call_id = mailbox_get_id;
+
(* Second call: Query emails in the first mailbox using result reference *)
+
(* Create reference to the first mailbox ID from the previous result *)
+
let mailbox_id_ref = Jmap.ResultReference.create
+
~result_of:mailbox_get_id
+
(* Use the reference to create the query arguments *)
+
let (mailbox_id_ref_key, mailbox_id_ref_value) =
+
Jmap.ResultReference.reference_arg "inMailbox" mailbox_id_ref in
+
let email_query_call = {
+
("accountId", `String account_id);
+
(mailbox_id_ref_key, mailbox_id_ref_value)
+
("limit", `Float 10.0);
+
method_call_id = email_query_id;
+
(* Third call: Get full email objects using the query result *)
+
(* Create reference to the email IDs from the query result *)
+
let email_ids_ref = Jmap.ResultReference.create
+
~result_of:email_query_id
+
(* Use the reference to create the get arguments *)
+
let (email_ids_ref_key, email_ids_ref_value) =
+
Jmap.ResultReference.reference_arg "ids" email_ids_ref in
+
("accountId", `String account_id);
+
(email_ids_ref_key, email_ids_ref_value)
+
method_call_id = email_get_id;
+
(* Create the complete request with all three method calls *)
+
Jmap.Capability.to_string Jmap.Capability.Core;
+
Jmap_mail.Capability.to_string Jmap_mail.Capability.Mail
+
let* response_result = Jmap.Api.make_request conn.config request in
+
Printf.printf "\nResult Reference Demo:\n";
+
Printf.printf "=====================\n";
+
match response_result with
+
Printf.printf "Error executing chained request: %s\n"
+
| Jmap.Api.Connection_error msg -> "Connection error: " ^ msg
+
| Jmap.Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body
+
| Jmap.Api.Parse_error msg -> "Parse error: " ^ msg
+
| Jmap.Api.Authentication_error -> "Authentication error");
+
(* Process the response *)
+
(* Look for the Email/get method response *)
+
let email_get_result = List.find (fun (inv : Ezjsonm.value invocation) ->
+
) response.method_responses in
+
(* Extract the email list from the response *)
+
let list = Ezjsonm.find email_get_result.arguments ["list"] in
+
Printf.printf "Successfully retrieved %d emails using chained result references!\n"
+
Printf.printf "Unexpected email list format in response.\n";
+
Printf.printf "No Email/get result found in response.\n";
+
Printf.printf "Error processing response: %s\n" (Printexc.to_string e);
(* Parse command-line arguments *)
let unread_only = ref false in
let show_labels = ref false in
let debug_level = ref 0 in
+
let demo_refs = ref false in
("-unread", Arg.Set unread_only, "List only unread messages");
("-labels", Arg.Set show_labels, "Show labels/keywords associated with messages");
("-debug", Arg.Int (fun level -> debug_level := level), "Set debug level (0-4, where 4 is most verbose)");
+
("-demo-refs", Arg.Set demo_refs, "Demonstrate result references");
let usage_msg = "Usage: JMAP_API_TOKEN=your_token fastmail_list [options]" in
···
Printf.eprintf " -unread List only unread messages\n";
Printf.eprintf " -labels Show labels/keywords associated with messages\n";
Printf.eprintf " -debug LEVEL Set debug level (0-4, where 4 is most verbose)\n";
+
Printf.eprintf " -demo-refs Demonstrate result references\n";
(* Only print token info at Info level or higher *)
···
Printf.eprintf "No accounts found\n";
+
(* Run result references demo if requested *)
+
demo_result_references conn primary_account_id
(* Get the Inbox mailbox *)