diff --git a/lib/app/Fetch.ml b/lib/app/Fetch.ml new file mode 100644 index 0000000..1f81733 --- /dev/null +++ b/lib/app/Fetch.ml @@ -0,0 +1,25 @@ +(* + * SPDX-FileCopyrightText: 2024 The Forester Project Contributors + * + * SPDX-License-Identifier: GPL-3.0-or-later + *) + +open Brr +open Brr_io.Fetch + +let text (url : Jstr.t) : (string, Jv.Error.t) result Fut.t = + let open Fut.Result_syntax in + let* res = request (Request.v ~init:(Request.init ()) url) in + let* text = Body.text (Response.as_body res) in + Fut.return (Ok (Jstr.to_string text)) + +let json (url : Jstr.t) (codec : 'a Jsont.t) : ('a, Jv.Error.t) result Fut.t = + let open Fut.Result_syntax in + let* res = request (Request.v ~init:(Request.init ()) url) in + let* body = Body.json (Response.as_body res) in + Fut.return (Jsont_brr.decode_jv codec body) + +let rec poll ~(ms : int) (f : unit -> bool Fut.t) : unit = + ignore + @@ G.set_timeout ~ms (fun () -> + Fut.await (f ()) (fun continue -> if continue then poll ~ms f))