123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117(*****************************************************************************)(* Open Source License *)(* Copyright (c) 2021-present Étienne Marais <etienne@maiste.fr> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openUtilsopenLwt.SyntaxmoduleClient=Http_lwt_client(**** Type definitions ****)typet={address:string;token:string}lettokent=t.tokenletaddresst=t.address(**** Default values ****)letbuild_headertoken=lettoken=iftoken=""then[]else[("X-Auth-Token",token)]intoken@[("Content-Type","application/json")]letequinoxe_default_path=Sys.path_from_home_dir".config/equinoxe/"(***** Helpers *****)letconvert_to_jsonresp=matchrespwith|Ok(_,Some"")->Json.of_string"{ }"|Ok(_,Somes)->Json.of_strings|Ok(_,None)->Json.error"{ }"|Error(`Msge)->Json.errorelettoken_from_pathtoken_path=matchReader.read_token_opttoken_pathwith|None->raiseNot_found|Sometoken->tokenletdefault_token()=lettoken_path=Filename.concatequinoxe_default_path"token"intoken_from_pathtoken_pathletget_token=function|`Default->default_token()|`Pathtoken_path->token_from_pathtoken_path|`Strtoken->token(**** Http methode ****)letget_fromtpath=letheaders=build_headert.tokeninleturl=Filename.concatt.addresspathinClient.one_request~meth:`GET~headersurlletpost_fromt~pathbody=letheaders=build_headert.tokeninleturl=Filename.concatt.addresspathinClient.one_request~meth:`POST~headers~bodyurlletput_fromt~pathbody=letheaders=build_headert.tokeninleturl=Filename.concatt.addresspathinClient.one_request~meth:`PUT~headers~bodyurlletdelete_fromtpath=letheaders=build_headert.tokeninleturl=Filename.concatt.addresspathinClient.one_request~meth:`DELETE~headersurl(**** API ****)letcreate~address?(token=`Default)()=lettoken=get_tokentokenin{address;token}letgett~path()=let+resp=get_fromtpathinconvert_to_jsonrespletpostt~pathjson=matchJson.exportjsonwith|Okbody->let+resp=post_fromt~pathbodyinconvert_to_jsonresp|Error(`Msge)->Lwt.return@@Json.erroreletputt~pathjson=matchJson.exportjsonwith|Okbody->let+resp=put_fromt~pathbodyinconvert_to_jsonresp|Error(`Msge)->Lwt.return@@Json.erroreletdeletet~path()=let+resp=delete_fromtpathinconvert_to_jsonrespletrunjson=Lwt_main.runjson