Matrix protocol in OCaml, Eio specialised
at main 89 lines 2.7 kB view raw
1(** Device management operations. *) 2 3type device = { 4 device_id : string; 5 display_name : string option; 6 last_seen_ip : string option; 7 last_seen_ts : int64 option; 8} 9 10let device_jsont = 11 Jsont.Object.( 12 map (fun device_id display_name last_seen_ip last_seen_ts -> 13 { device_id; display_name; last_seen_ip; last_seen_ts }) 14 |> mem "device_id" Jsont.string 15 |> opt_mem "display_name" Jsont.string ~enc:(fun t -> t.display_name) 16 |> opt_mem "last_seen_ip" Jsont.string ~enc:(fun t -> t.last_seen_ip) 17 |> opt_mem "last_seen_ts" Jsont.int64 ~enc:(fun t -> t.last_seen_ts) 18 |> finish) 19 20type devices_response = { 21 devices : device list; 22} 23 24let devices_response_jsont = 25 Jsont.Object.( 26 map (fun devices -> { devices }) 27 |> mem "devices" (Jsont.list device_jsont) ~dec_absent:[] 28 |> finish) 29 30let get_devices client = 31 match Client.get client ~path:"/devices" () with 32 | Error e -> Error e 33 | Ok body -> 34 match Client.decode_response devices_response_jsont body with 35 | Error e -> Error e 36 | Ok resp -> Ok resp.devices 37 38let get_device client ~device_id = 39 let path = Printf.sprintf "/devices/%s" (Uri.pct_encode device_id) in 40 match Client.get client ~path () with 41 | Error e -> Error e 42 | Ok body -> Client.decode_response device_jsont body 43 44type update_device_request = { 45 display_name : string; 46} [@@warning "-69"] 47 48let update_device_request_jsont = 49 Jsont.Object.( 50 map (fun display_name -> { display_name }) 51 |> mem "display_name" Jsont.string 52 |> finish) 53 54let update_device client ~device_id ~display_name = 55 let path = Printf.sprintf "/devices/%s" (Uri.pct_encode device_id) in 56 let request = { display_name } in 57 match Client.encode_body update_device_request_jsont request with 58 | Error e -> Error e 59 | Ok body -> 60 match Client.put client ~path ~body () with 61 | Error e -> Error e 62 | Ok _ -> Ok () 63 64(* Delete device - simplified without UIAA *) 65let delete_device client ~device_id = 66 let path = Printf.sprintf "/devices/%s" (Uri.pct_encode device_id) in 67 match Client.delete client ~path () with 68 | Error e -> Error e 69 | Ok _ -> Ok () 70 71(* Delete multiple devices - simplified without UIAA *) 72type delete_devices_request = { 73 devices : string list; 74} [@@warning "-69"] 75 76let delete_devices_request_jsont = 77 Jsont.Object.( 78 map (fun devices -> { devices }) 79 |> mem "devices" (Jsont.list Jsont.string) 80 |> finish) 81 82let delete_devices client ~device_ids = 83 let request = { devices = device_ids } in 84 match Client.encode_body delete_devices_request_jsont request with 85 | Error e -> Error e 86 | Ok body -> 87 match Client.post client ~path:"/delete_devices" ~body () with 88 | Error e -> Error e 89 | Ok _ -> Ok ()