From 99608caffb3479fd086ecd1280a1f45ada34a03d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 13 May 2023 15:57:03 +0200 Subject: [PATCH] update! --- lib/json.ml | 8 ++++++++ lib/net.ml | 27 +++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/json.ml b/lib/json.ml index ee91cd6..fc4888a 100644 --- a/lib/json.ml +++ b/lib/json.ml @@ -1,3 +1,5 @@ +open Utils + let connection_payload = {| { @@ -11,3 +13,9 @@ let connection_payload = } |} ;; + +let navigate_payload url = fmt {| + { + "url": "%s" + } + |} url diff --git a/lib/net.ml b/lib/net.ml index 1ead188..b7b5354 100644 --- a/lib/net.ml +++ b/lib/net.ml @@ -1,28 +1,27 @@ open Cohttp_lwt_unix open Utils -let url = "http://127.0.0.1:4444" +let driver_url = "http://127.0.0.1:4444/session" let send_post_request url json = - let uri = Uri.of_string url in let headers = Cohttp.Header.init_with "Content-Type" "application/json" in let body = Cohttp_lwt.Body.of_string json in - Lwt.bind (Client.post ~headers ~body uri) (fun (_response, body) -> - Cohttp_lwt.Body.to_string body) + Lwt.bind + (Client.post ~headers ~body (Uri.of_string url)) + (fun (_response, body) -> Cohttp_lwt.Body.to_string body) ;; let send_delete_request url = - let uri = Uri.of_string url in - Lwt.bind (Client.delete uri) (fun (_response, body) -> Cohttp_lwt.Body.to_string body) + Lwt.bind + (Client.delete (Uri.of_string url)) + (fun (_response, body) -> Cohttp_lwt.Body.to_string body) ;; -let execute_request url json = - let body = send_post_request url json in - Lwt_main.run body -;; +let execute_request url json = Lwt_main.run (send_post_request url json) +(* Server MUST be started already *) let get_session () = - let response = execute_request (fmt "%s/session" url) Json.connection_payload in + let response = execute_request (fmt "%s" driver_url) Json.connection_payload in match Yojson.Safe.from_string response with | `Assoc fields -> let value = List.assoc "value" fields in @@ -36,5 +35,9 @@ let get_session () = ;; let close_session id = - Lwt_main.run (send_delete_request (fmt "%s/session/%s" url id)) = "{\"value\":null}" + Lwt_main.run (send_delete_request (fmt "%s/%s" driver_url id)) = "{\"value\":null}" +;; + +let navigate url session_id = + execute_request (fmt "%s/%s/url" driver_url session_id) (Json.navigate_payload url) ;;