From 685412114c954adf59c218744582f5bf1fb8bcee Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 13 May 2023 13:33:46 +0200 Subject: [PATCH] retrieve sessionId --- bin/main.ml | 7 +++---- lib/dune | 2 +- lib/net.ml | 21 ++++++++++++++------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 0bea756..57e75d5 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -9,15 +9,14 @@ let start driver = ;; let stop (driver_process, session_id) = - let data = close_session session_id in - print_endline data; + if not (close_session session_id) then print_endline "Can't close the session"; stop_process driver_process ;; -let main () = () +let main session_id = print_endline session_id let () = let data = start (Gecko "0.33.0") in - main (); + main (snd data); stop data ;; diff --git a/lib/dune b/lib/dune index 06f86ac..d3a429f 100644 --- a/lib/dune +++ b/lib/dune @@ -1,4 +1,4 @@ (library (name pusk) (modules utils drivers net json) - (libraries cohttp-lwt-unix)) + (libraries cohttp-lwt-unix yojson)) diff --git a/lib/net.ml b/lib/net.ml index ce52d7c..1ead188 100644 --- a/lib/net.ml +++ b/lib/net.ml @@ -22,12 +22,19 @@ let execute_request url json = ;; let get_session () = - let body = execute_request (fmt "%s/session" url) Json.connection_payload in - print_endline body; - body + let response = execute_request (fmt "%s/session" url) Json.connection_payload in + match Yojson.Safe.from_string response with + | `Assoc fields -> + let value = List.assoc "value" fields in + let rec find_session_id = function + | ("sessionId", `String session_id) :: _ -> session_id + | _ :: rest -> find_session_id rest + | [] -> failwith "Session ID not found" + in + find_session_id (Yojson.Safe.Util.to_assoc value) + | _ -> failwith "Invalid JSON" ;; -(* match Yojson.Safe.from_string body with - | _ as e -> print_endline e *) - -let close_session id = Lwt_main.run (send_delete_request (fmt "%s/session/%s" url id)) +let close_session id = + Lwt_main.run (send_delete_request (fmt "%s/session/%s" url id)) = "{\"value\":null}" +;;