summaryrefslogtreecommitdiff
path: root/src/vyconf_client_session.ml
blob: 70a2a131a45dbc8e1b6bd18c09549cc978710e84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
open Vyconf_connect.Vyconf_pbt

type op_t =
    | OpSetupSession
    | OpExists
    | OpTeardownSession
    | OpShowConfig
    | OpValidate

let config_format_of_string s =
    match s with
    | "curly" -> Curly
    | "json" -> Json
    | _ -> failwith (Printf.sprintf "Unknown config format %s, should be curly or json" s)

let output_format_of_string s =
    match s with
    | "plain" -> Out_plain
    | "json" ->	Out_json
    | _	-> failwith (Printf.sprintf "Unknown output format %s, should be plain or json" s)

let call_op ?(out_format="plain") ?(config_format="curly") socket token op path =
    let config_format = config_format_of_string config_format in
    let out_format = output_format_of_string out_format in
    let run =
        let%lwt client =
            Vyconf_client.create ~token:token socket out_format config_format
        in
        let%lwt result = match op with
        | None -> Error "Operation required" |> Lwt.return
        | Some o ->
            begin
            match o with
            | OpSetupSession ->
                let%lwt resp = Vyconf_client.setup_session client "vyconf_client_session" in
                begin
                    match resp with
                    | Ok c -> Vyconf_client.get_token c
                    | Error e -> Error e |> Lwt.return
                end
            | OpExists -> Vyconf_client.exists client path
            | OpTeardownSession -> Vyconf_client.teardown_session client
            | OpShowConfig -> Vyconf_client.show_config client path
            | OpValidate -> Vyconf_client.validate client path
            end
        in
        Lwt.return result
    in
    Lwt_main.run run

let session_init ?(out_format="plain") ?(config_format="curly") socket =
    call_op ~out_format:out_format ~config_format:config_format socket None (Some OpSetupSession) []

let session_free socket token =
    call_op socket (Some token) (Some OpTeardownSession) []

let session_validate_path socket token path =
    call_op socket (Some token) (Some OpValidate) path

let session_show_config socket token path =
    call_op socket (Some token) (Some OpShowConfig) path

let session_path_exists socket token path =
    call_op socket (Some token) (Some OpExists) path