Matrix protocol in OCaml, Eio specialised
at main 69 lines 2.2 kB view raw
1(** Eio-idiomatic Matrix client. 2 3 This module wraps the base matrix_client with Eio idioms: 4 - Uses switches for resource management 5 - Raises Eio.Io exceptions instead of returning Results 6 - Provides cancellation support via Eio switches 7 - Supports structured concurrency *) 8 9type config = { 10 homeserver : Uri.t; 11 user_agent : string option; 12} 13 14type t = { 15 base : Matrix_client.Client.t; 16 sw : Eio.Switch.t; 17 env : Eio_unix.Stdenv.base; 18} 19 20(** Create a new Matrix client. 21 22 The client is bound to the provided switch and will be cleaned up 23 when the switch completes. 24 25 @param sw The Eio switch for resource management 26 @param env The Eio environment 27 @param homeserver The Matrix homeserver URL 28 @param user_agent Optional user agent string *) 29let create ~sw ~env ~homeserver ?user_agent () = 30 let config : Matrix_client.Client.config = { homeserver; user_agent } in 31 let base = Matrix_client.Client.create ~sw ~config env in 32 { base; sw; env } 33 34(** Get the underlying base client *) 35let base t = t.base 36 37(** Get the Eio switch *) 38let switch t = t.sw 39 40(** Get the homeserver URL *) 41let homeserver t = Matrix_client.Client.homeserver t.base 42 43(** Check if the client is logged in *) 44let is_logged_in t = Matrix_client.Client.is_logged_in t.base 45 46(** Get the current user ID (raises if not logged in) *) 47let user_id t = 48 match Matrix_client.Client.user_id t.base with 49 | Some id -> id 50 | None -> raise (Eio.Exn.create (Error.E Error.Not_logged_in)) 51 52(** Get the current device ID (raises if not logged in) *) 53let device_id t = 54 match Matrix_client.Client.device_id t.base with 55 | Some id -> id 56 | None -> raise (Eio.Exn.create (Error.E Error.Not_logged_in)) 57 58(** Get the access token (raises if not logged in) *) 59let access_token t = 60 match Matrix_client.Client.access_token t.base with 61 | Some token -> token 62 | None -> raise (Eio.Exn.create (Error.E Error.Not_logged_in)) 63 64(** Get the session if logged in *) 65let session t = Matrix_client.Client.session t.base 66 67(** Update the client with a new session *) 68let with_session t session = 69 { t with base = Matrix_client.Client.with_session t.base session }