···
is_unread_keyword || is_not_seen
67
-
(** Example function demonstrating how to use result references for chained requests *)
67
+
(** Example function demonstrating how to use higher-level library functions for JMAP requests *)
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
Printf.printf "\nResult Reference Demo:\n";
Printf.printf "=====================\n";
153
-
match response_result with
72
+
(* Step 1: Get all mailboxes *)
73
+
let* mailboxes_result = Jmap_mail.get_mailboxes conn ~account_id in
74
+
match mailboxes_result with
155
-
Printf.printf "Error executing chained request: %s\n"
76
+
Printf.printf "Error getting mailboxes: %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");
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);
85
+
(* Step 2: Get the first mailbox for this demonstration *)
86
+
match mailboxes with
88
+
Printf.printf "No mailboxes found.\n";
91
+
| first_mailbox :: _ ->
92
+
Printf.printf "Using mailbox: %s\n" first_mailbox.Mail.name;
94
+
(* Step 3: Get emails from the selected mailbox *)
95
+
let* emails_result = Jmap_mail.get_messages_in_mailbox
98
+
~mailbox_id:first_mailbox.Mail.id
103
+
match emails_result with
105
+
Printf.printf "Error getting emails: %s\n"
107
+
| Jmap.Api.Connection_error msg -> "Connection error: " ^ msg
108
+
| Jmap.Api.HTTP_error (code, body) -> Printf.sprintf "HTTP error %d: %s" code body
109
+
| Jmap.Api.Parse_error msg -> "Parse error: " ^ msg
110
+
| Jmap.Api.Authentication_error -> "Authentication error");
114
+
Printf.printf "Successfully retrieved %d emails using the high-level library API!\n"
115
+
(List.length emails);
117
+
(* Display some basic information about the emails *)
118
+
List.iteri (fun i (email:Jmap_mail.Types.email) ->
119
+
let subject = Option.value ~default:"<no subject>" email.Mail.subject in
120
+
Printf.printf " %d. %s\n" (i + 1) subject