this repo has no description

Refactor fastmail_list to use higher-level API functions

Replace the manual construction of JMAP requests with proper higher-level
library calls from Jmap_mail. This improves code clarity and ensures that
the example demonstrates the recommended way to use the library.

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>

Changed files
+45 -108
bin
+45 -108
bin/fastmail_list.ml
···
in
is_unread_keyword || is_not_seen
-
(** Example function demonstrating how to use result references for chained requests *)
let demo_result_references conn account_id =
-
let open Jmap.Types in
-
-
(* Create a request that chains the following operations:
-
1. Get mailboxes
-
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 = {
-
name = "Mailbox/get";
-
arguments = `O [
-
("accountId", `String account_id);
-
];
-
method_call_id = mailbox_get_id;
-
} in
-
-
(* 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
-
~name:"Mailbox/get"
-
~path:"/list/0/id" in
-
-
(* 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 = {
-
name = "Email/query";
-
arguments = `O [
-
("accountId", `String account_id);
-
("filter", `O [
-
(mailbox_id_ref_key, mailbox_id_ref_value)
-
]);
-
("limit", `Float 10.0);
-
];
-
method_call_id = email_query_id;
-
} in
-
-
(* 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
-
~name:"Email/query"
-
~path:"/ids" in
-
-
(* 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
-
-
let email_get_call = {
-
name = "Email/get";
-
arguments = `O [
-
("accountId", `String account_id);
-
(email_ids_ref_key, email_ids_ref_value)
-
];
-
method_call_id = email_get_id;
-
} in
-
-
(* Create the complete request with all three method calls *)
-
let request = {
-
using = [
-
Jmap.Capability.to_string Jmap.Capability.Core;
-
Jmap_mail.Capability.to_string Jmap_mail.Capability.Mail
-
];
-
method_calls = [
-
mailbox_get_call;
-
email_query_call;
-
email_get_call
-
];
-
created_ids = None;
-
} in
-
-
(* Make the request *)
-
let* response_result = Jmap.Api.make_request conn.config request in
Printf.printf "\nResult Reference Demo:\n";
Printf.printf "=====================\n";
-
match response_result with
| Error err ->
-
Printf.printf "Error executing chained request: %s\n"
(match err with
| 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");
Lwt.return_unit
-
| Ok response ->
-
(* Process the response *)
-
try
-
(* Look for the Email/get method response *)
-
let email_get_result = List.find (fun (inv : Ezjsonm.value invocation) ->
-
inv.name = "Email/get"
-
) response.method_responses in
-
-
(* Extract the email list from the response *)
-
let list = Ezjsonm.find email_get_result.arguments ["list"] in
-
match list with
-
| `A emails ->
-
Printf.printf "Successfully retrieved %d emails using chained result references!\n"
-
(List.length emails);
-
Lwt.return_unit
-
| _ ->
-
Printf.printf "Unexpected email list format in response.\n";
-
Lwt.return_unit
-
with
-
| Not_found ->
-
Printf.printf "No Email/get result found in response.\n";
-
Lwt.return_unit
-
| e ->
-
Printf.printf "Error processing response: %s\n" (Printexc.to_string e);
Lwt.return_unit
(** Main function *)
let main () =
···
in
is_unread_keyword || is_not_seen
+
(** Example function demonstrating how to use higher-level library functions for JMAP requests *)
let demo_result_references conn account_id =
Printf.printf "\nResult Reference Demo:\n";
Printf.printf "=====================\n";
+
(* Step 1: Get all mailboxes *)
+
let* mailboxes_result = Jmap_mail.get_mailboxes conn ~account_id in
+
match mailboxes_result with
| Error err ->
+
Printf.printf "Error getting mailboxes: %s\n"
(match err with
| 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");
Lwt.return_unit
+
+
| Ok mailboxes ->
+
(* Step 2: Get the first mailbox for this demonstration *)
+
match mailboxes with
+
| [] ->
+
Printf.printf "No mailboxes found.\n";
Lwt.return_unit
+
+
| first_mailbox :: _ ->
+
Printf.printf "Using mailbox: %s\n" first_mailbox.Mail.name;
+
+
(* Step 3: Get emails from the selected mailbox *)
+
let* emails_result = Jmap_mail.get_messages_in_mailbox
+
conn
+
~account_id
+
~mailbox_id:first_mailbox.Mail.id
+
~limit:10
+
()
+
in
+
+
match emails_result with
+
| Error err ->
+
Printf.printf "Error getting emails: %s\n"
+
(match err with
+
| 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");
+
Lwt.return_unit
+
+
| Ok emails ->
+
Printf.printf "Successfully retrieved %d emails using the high-level library API!\n"
+
(List.length emails);
+
+
(* Display some basic information about the emails *)
+
List.iteri (fun i (email:Jmap_mail.Types.email) ->
+
let subject = Option.value ~default:"<no subject>" email.Mail.subject in
+
Printf.printf " %d. %s\n" (i + 1) subject
+
) emails;
+
+
Lwt.return_unit
(** Main function *)
let main () =