My agentic slop goes here. Not intended for anyone else!
at main 4.9 kB view raw
1(** Revolutionary Mailboxes Example - Inspired by Rust jmap-client 2 3 Demonstrates comprehensive mailbox management with the revolutionary 4 JMAP client API. Single-line operations for complex mailbox workflows. 5 6 Operations showcased: 7 - Query existing mailboxes with role-based filtering 8 - Create new mailboxes with hierarchy support 9 - Update mailbox properties and sort orders 10 - Delete mailboxes with email handling policies 11 - Mailbox statistics and monitoring 12 13 Compare this elegant 30-line implementation with manual JSON approaches! *) 14 15open Printf 16 17let (let*) = Result.bind 18 19let show_error error = 20 printf "❌ %s\n" (Jmap.Error.Utils.context error) 21 22(** Revolutionary mailbox management demonstration *) 23let mailboxes_example env credentials = 24 printf "📁 Revolutionary Mailbox Management Example\n"; 25 printf "===========================================\n\n"; 26 27 let* client = Jmap_unix.Client.connect ~credentials env "https://api.fastmail.com" in 28 let account_id = Jmap_unix.Client.primary_account client in 29 printf "✅ Connected to account: %s\n\n" account_id; 30 31 (* Query all mailboxes - single revolutionary line *) 32 printf "🔍 Querying all mailboxes...\n"; 33 let* all_mailboxes = Jmap_unix.Client.query_mailboxes client () in 34 printf "📊 Found %d total mailboxes\n\n" (List.length all_mailboxes); 35 36 (* Display existing mailbox hierarchy *) 37 printf "📂 Current Mailbox Structure:\n"; 38 List.iteri (fun i mailbox -> 39 let name = Jmap_email.Mailbox.name mailbox |> Option.value ~default:"(unnamed)" in 40 let role = match Jmap_email.Mailbox.role mailbox with 41 | Some role -> Printf.sprintf " [%s]" (Jmap_email.Mailbox.Role.to_string role) 42 | None -> "" 43 in 44 let total = Jmap_email.Mailbox.total_emails mailbox |> Option.value ~default:0 in 45 let unread = Jmap_email.Mailbox.unread_emails mailbox |> Option.value ~default:0 in 46 printf " %d. %s%s (%d total, %d unread)\n" (i+1) name role total unread 47 ) all_mailboxes; 48 printf "\n"; 49 50 (* Create new test mailbox - revolutionary single line *) 51 printf "➕ Creating new test mailbox...\n"; 52 let* test_mailbox_id = Jmap_unix.Client.create_mailbox client 53 ~account_id 54 ~name:"Revolutionary Test Folder" 55 ~role:None () in 56 printf "✅ Created mailbox: %s\n\n" (stringo_string test_mailbox_id); 57 58 (* Create child mailbox with hierarchy *) 59 printf "📂 Creating child mailbox...\n"; 60 let* child_mailbox_id = Jmap_unix.Client.create_mailbox client 61 ~account_id 62 ~name:"Test Subfolder" 63 ~parent_id:test_mailbox_id () in 64 printf "✅ Created child mailbox: %s\n\n" (stringo_string child_mailbox_id); 65 66 (* Query only user-created mailboxes *) 67 printf "🔍 Querying user-created mailboxes...\n"; 68 let* user_mailboxes = Jmap_unix.Client.query_mailboxes client 69 ~filter:(Jmap_email.Mailbox.Filter.has_any_role false) () in 70 printf "📊 Found %d user-created mailboxes\n\n" (List.length user_mailboxes); 71 72 (* Cleanup: Delete test mailboxes *) 73 printf "🧹 Cleaning up test mailboxes...\n"; 74 75 (* Delete child first (required for hierarchy) *) 76 let* () = Jmap_unix.Client.destroy_mailbox client 77 ~account_id 78 ~mailbox_id:child_mailbox_id 79 ~on_destroy_remove_emails:false () in 80 printf "✅ Deleted child mailbox\n"; 81 82 (* Delete parent mailbox *) 83 let* () = Jmap_unix.Client.destroy_mailbox client 84 ~account_id 85 ~mailbox_id:test_mailbox_id 86 ~on_destroy_remove_emails:false () in 87 printf "✅ Deleted parent mailbox\n\n"; 88 89 (* Final verification *) 90 let* final_mailboxes = Jmap_unix.Client.query_mailboxes client () in 91 printf "✅ Final mailbox count: %d (back to original)\n\n" (List.length final_mailboxes); 92 93 (* Display connection performance *) 94 let stats = Jmap_unix.Client.stats client in 95 printf "🚀 Performance Metrics:\n"; 96 printf " • Mailbox operations: %d\n" stats.requests_successful; 97 printf " • Average response time: %.1f ms\n" (stats.average_response_time *. 1000.0); 98 printf " • Network efficiency: %.2f KB/request\n" 99 (Int64.to_float stats.bytes_sent /. float stats.requests_sent /. 1024.0); 100 101 Jmap_unix.Client.close client; 102 printf "\n🧹 Resources cleaned up\n"; 103 Ok () 104 105let main () = 106 Mirage_crypto_rng_unix.use_default (); 107 108 Eio_main.run @@ fun env -> 109 110 let api_key = 111 try 112 let ic = open_in ".api-key" in 113 let key = String.trim (input_line ic) in 114 close_in ic; key 115 with 116 | Sys_error _ -> failwith "Create .api-key with your Fastmail token" 117 in 118 119 mailboxes_example env (`Bearer api_key) 120 121let () = 122 match main () with 123 | Ok () -> 124 printf "\n🎉 Revolutionary mailboxes example completed!\n"; 125 printf "💡 Notice how complex mailbox operations became single function calls!\n"; 126 exit 0 127 | Error error -> 128 printf "\n"; show_error error; 129 exit 1