···
is_unread_keyword || is_not_seen
67
+
(** Example function demonstrating how to use result references for chained requests *)
68
+
let demo_result_references conn account_id =
69
+
let open Jmap.Types in
71
+
(* Create a request that chains the following operations:
73
+
2. Query emails in the first mailbox found
74
+
3. Get the full email objects for those IDs
77
+
(* Create method call IDs *)
78
+
let mailbox_get_id = "mailboxGet" in
79
+
let email_query_id = "emailQuery" in
80
+
let email_get_id = "emailGet" in
82
+
(* First call: Get mailboxes *)
83
+
let mailbox_get_call = {
84
+
name = "Mailbox/get";
86
+
("accountId", `String account_id);
88
+
method_call_id = mailbox_get_id;
91
+
(* Second call: Query emails in the first mailbox using result reference *)
92
+
(* Create reference to the first mailbox ID from the previous result *)
93
+
let mailbox_id_ref = Jmap.ResultReference.create
94
+
~result_of:mailbox_get_id
96
+
~path:"/list/0/id" in
98
+
(* Use the reference to create the query arguments *)
99
+
let (mailbox_id_ref_key, mailbox_id_ref_value) =
100
+
Jmap.ResultReference.reference_arg "inMailbox" mailbox_id_ref in
102
+
let email_query_call = {
103
+
name = "Email/query";
105
+
("accountId", `String account_id);
107
+
(mailbox_id_ref_key, mailbox_id_ref_value)
109
+
("limit", `Float 10.0);
111
+
method_call_id = email_query_id;
114
+
(* Third call: Get full email objects using the query result *)
115
+
(* Create reference to the email IDs from the query result *)
116
+
let email_ids_ref = Jmap.ResultReference.create
117
+
~result_of:email_query_id
118
+
~name:"Email/query"
121
+
(* Use the reference to create the get arguments *)
122
+
let (email_ids_ref_key, email_ids_ref_value) =
123
+
Jmap.ResultReference.reference_arg "ids" email_ids_ref in
125
+
let email_get_call = {
126
+
name = "Email/get";
128
+
("accountId", `String account_id);
129
+
(email_ids_ref_key, email_ids_ref_value)
131
+
method_call_id = email_get_id;
134
+
(* Create the complete request with all three method calls *)
137
+
Jmap.Capability.to_string Jmap.Capability.Core;
138
+
Jmap_mail.Capability.to_string Jmap_mail.Capability.Mail
145
+
created_ids = None;
148
+
(* Make the request *)
149
+
let* response_result = Jmap.Api.make_request conn.config request in
150
+
Printf.printf "\nResult Reference Demo:\n";
151
+
Printf.printf "=====================\n";
153
+
match response_result with
155
+
Printf.printf "Error executing chained request: %s\n"
157
+
| Jmap.Api.Connection_error msg -> "Connection error: " ^ msg
158
+
| Jmap.Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body
159
+
| Jmap.Api.Parse_error msg -> "Parse error: " ^ msg
160
+
| Jmap.Api.Authentication_error -> "Authentication error");
163
+
(* Process the response *)
165
+
(* Look for the Email/get method response *)
166
+
let email_get_result = List.find (fun (inv : Ezjsonm.value invocation) ->
167
+
inv.name = "Email/get"
168
+
) response.method_responses in
170
+
(* Extract the email list from the response *)
171
+
let list = Ezjsonm.find email_get_result.arguments ["list"] in
174
+
Printf.printf "Successfully retrieved %d emails using chained result references!\n"
175
+
(List.length emails);
178
+
Printf.printf "Unexpected email list format in response.\n";
182
+
Printf.printf "No Email/get result found in response.\n";
185
+
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
194
+
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)");
200
+
("-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";
217
+
Printf.eprintf " -demo-refs Demonstrate result references\n";
(* Only print token info at Info level or higher *)
···
Printf.eprintf "No accounts found\n";
272
+
(* Run result references demo if requested *)
275
+
demo_result_references conn primary_account_id
(* Get the Inbox mailbox *)