diff options
author | Daniil Baturin <daniil@baturin.org> | 2018-02-12 17:11:09 +0700 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2018-02-12 17:11:09 +0700 |
commit | a245e894c6f5473f3051366389d3f90a778dacbd (patch) | |
tree | 69f502af6a05f3e920135166ac05ab07d49626d7 | |
parent | b105c925241fb99cebcc087110b88c395e1d723d (diff) | |
download | vyconf-a245e894c6f5473f3051366389d3f90a778dacbd.tar.gz vyconf-a245e894c6f5473f3051366389d3f90a778dacbd.zip |
Add client support for the config read functions.
-rw-r--r-- | src/vyconf_client.ml | 59 | ||||
-rw-r--r-- | src/vyconf_client.mli | 14 |
2 files changed, 63 insertions, 10 deletions
diff --git a/src/vyconf_client.ml b/src/vyconf_client.ml index 267b10e..a9e7637 100644 --- a/src/vyconf_client.ml +++ b/src/vyconf_client.ml @@ -8,15 +8,17 @@ type t = { enc: Pbrt.Encoder.t; session: string option; conf_mode: bool; - closed: bool + closed: bool; + out_format: request_output_format; + conf_format: request_config_format } -let substitute_default d o= +let unwrap o = match o with - | None -> d - | Some v -> v + | Some v -> Ok v + | None -> Error "operation returned None when actual value was expected" -let create sockfile = +let create ?(token=None) sockfile out_format conf_format = let open Lwt_unix in let sock = socket PF_UNIX SOCK_STREAM 0 in let%lwt () = connect sock (ADDR_UNIX sockfile) in @@ -25,9 +27,16 @@ let create sockfile = Lwt.return { sock=sock; ic=ic; oc=oc; enc=(Pbrt.Encoder.create ()); closed=false; - session=None; conf_mode=false; + session=token; conf_mode=false; out_format=out_format; + conf_format=conf_format } +let get_token client = + let token = client.session in + match token with + | Some t -> Ok t |> Lwt.return + | None -> Error "failed to get a session token" |> Lwt.return + let shutdown client = let%lwt () = Lwt_unix.close client.sock in Lwt.return {client with closed=true} @@ -38,6 +47,7 @@ let do_request client req = let msg = Pbrt.Encoder.to_bytes enc in let%lwt () = Message.write client.oc msg in let%lwt resp = Message.read client.ic in + let%lwt () = Printf.printf "Decoding" |> (fun () -> Lwt.return_unit) in decode_response (Pbrt.Decoder.of_bytes resp) |> Lwt.return let get_status client = @@ -53,6 +63,37 @@ let setup_session ?(on_behalf_of=None) client client_app = match resp.status with | Success -> (match resp.output with - | Some token -> Lwt.return (Ok {client with session=(Some token)}) - | None -> failwith "setup_session did not return a session token!") - | _ -> Error (substitute_default "Unknown error" resp.error) |> Lwt.return + | Some token -> Ok {client with session=(Some token)} + | None -> Error "setup_session did not return a session token!") |> Lwt.return + | _ -> Error (BatOption.default "Unknown error" resp.error) |> Lwt.return + +let exists client path = + let req = Exists {path=path} in + let%lwt resp = do_request client req in + match resp.status with + | Success -> Lwt.return (Ok "") + | Fail -> Lwt.return (Error "") + | _ -> Error (BatOption.default "" resp.error) |> Lwt.return + +let get_value client path = + let req = Get_value {path=path; output_format=(Some client.out_format)} in + let%lwt resp = do_request client req in + match resp.status with + | Success -> unwrap resp.output |> Lwt.return + | _ -> Error (BatOption.default ""resp.error) |> Lwt.return + +let get_values client path = + let req = Get_values {path=path; output_format=(Some client.out_format)} in + let%lwt resp = do_request client req in + match resp.status with + | Success -> unwrap resp.output |> Lwt.return + | _ -> Error (BatOption.default "" resp.error) |> Lwt.return + +let list_children client path = + let req = List_children {path=path; output_format=(Some client.out_format)} in + let%lwt resp = do_request client req in + match resp.status with + | Success -> unwrap resp.output |> Lwt.return + | _ -> Error (BatOption.default "" resp.error) |> Lwt.return + + diff --git a/src/vyconf_client.mli b/src/vyconf_client.mli index db9489d..9749102 100644 --- a/src/vyconf_client.mli +++ b/src/vyconf_client.mli @@ -19,10 +19,22 @@ type response = { } -val create : string -> t Lwt.t +val create : ?token:(string option) -> string -> Vyconf_types.request_output_format -> Vyconf_types.request_config_format -> t Lwt.t + +val get_token : t -> (string, string) result Lwt.t val shutdown : t -> t Lwt.t val get_status : t -> response Lwt.t val setup_session : ?on_behalf_of:(int option) -> t -> string -> (t, string) result Lwt.t + +val exists : t -> string list -> (string, string) result Lwt.t + +val get_value : t -> string list -> (string, string) result Lwt.t + +val get_values : t -> string list -> (string, string) result Lwt.t + +val list_children : t -> string list -> (string, string) result Lwt.t + + |