My agentic slop goes here. Not intended for anyone else!
at main 4.4 kB view raw
1(** Email response parsing using core JMAP parsers *) 2 3open Jmap.Methods 4 5(** Parse Email/get response using core parsers *) 6let parse_get_response ~from_json json = 7 Get_response.of_json ~from_json json 8 9(** Parse Email/query response using core parsers *) 10let parse_query_response json = 11 Query_response.of_json json 12 13(** Parse Email/changes response using core parsers *) 14let parse_changes_response json = 15 Changes_response.of_json json 16 17(** Parse Email/set response using core parsers *) 18let parse_set_response json = 19 (* For Email/set, we need to handle the created and updated info *) 20 let from_created_json json = 21 (* Email creation returns the server-assigned properties *) 22 json (* Return the JSON as-is for created info *) 23 in 24 let from_updated_json json = 25 (* Email updates may return computed properties *) 26 json (* Return as-is for now, can be enhanced later *) 27 in 28 Set_response.of_json ~from_created_json ~from_updated_json json 29 30(** Extract email list from a Get_response *) 31let emails_from_get_response response = 32 Get_response.list response 33 34(** Extract IDs from a Query_response *) 35let ids_from_query_response response = 36 List.map (fun s -> match Jmap.Id.of_string s with Ok id -> id | Error e -> failwith e) (Query_response.ids response) 37 38(** Check if there are more changes in a Changes_response *) 39let has_more_changes response = 40 Changes_response.has_more_changes response 41 42(** Get created IDs from a Changes_response *) 43let created_ids response = 44 List.map (fun s -> match Jmap.Id.of_string s with Ok id -> id | Error e -> failwith e) (Changes_response.created response) 45 46(** Get updated IDs from a Changes_response *) 47let updated_ids response = 48 List.map (fun s -> match Jmap.Id.of_string s with Ok id -> id | Error e -> failwith e) (Changes_response.updated response) 49 50(** Get destroyed IDs from a Changes_response *) 51let destroyed_ids response = 52 List.map (fun s -> match Jmap.Id.of_string s with Ok id -> id | Error e -> failwith e) (Changes_response.destroyed response) 53 54(** Response builder for batched requests *) 55module Batch = struct 56 type 'email batch_response = { 57 get_responses : (string * 'email Get_response.t) list; 58 query_responses : (string * Query_response.t) list; 59 set_responses : (string * (Yojson.Safe.t, Yojson.Safe.t) Set_response.t) list; 60 changes_responses : (string * Changes_response.t) list; 61 } 62 63 let empty = { 64 get_responses = []; 65 query_responses = []; 66 set_responses = []; 67 changes_responses = []; 68 } 69 70 let add_get_response ~method_call_id response batch = 71 { batch with get_responses = (method_call_id, response) :: batch.get_responses } 72 73 let add_query_response ~method_call_id response batch = 74 { batch with query_responses = (method_call_id, response) :: batch.query_responses } 75 76 let add_set_response ~method_call_id response batch = 77 { batch with set_responses = (method_call_id, response) :: batch.set_responses } 78 79 let add_changes_response ~method_call_id response batch = 80 { batch with changes_responses = (method_call_id, response) :: batch.changes_responses } 81 82 (** Parse a full JMAP response with multiple method calls *) 83 let parse_response ~from_json json = 84 let open Yojson.Safe.Util in 85 let method_responses = json |> member "methodResponses" |> to_list in 86 87 List.fold_left (fun batch response_item -> 88 let response_array = to_list response_item in 89 match response_array with 90 | [`String method_name; response_json; `String method_call_id] -> 91 (match method_name with 92 | "Email/get" -> 93 (match parse_get_response ~from_json response_json with 94 | Ok resp -> add_get_response ~method_call_id resp batch 95 | Error _ -> batch) 96 | "Email/query" -> 97 (match parse_query_response response_json with 98 | Ok resp -> add_query_response ~method_call_id resp batch 99 | Error _ -> batch) 100 | "Email/set" -> 101 (match parse_set_response response_json with 102 | Ok resp -> add_set_response ~method_call_id resp batch 103 | Error _ -> batch) 104 | "Email/changes" -> 105 (match parse_changes_response response_json with 106 | Ok resp -> add_changes_response ~method_call_id resp batch 107 | Error _ -> batch) 108 | _ -> batch) 109 | _ -> batch 110 ) empty method_responses 111end