From 5187f4c8e8cfcc49892fe64feb4abe812b3b30d3 Mon Sep 17 00:00:00 2001
From: dx3mod <dx3mod@bk.ru>
Date: Thu, 31 Oct 2024 10:54:27 +0300
Subject: [PATCH 1/2] Add with_connect_to function for automatic close
 connection and initialization

---
 README.md                    | 18 +++++++++++-------
 examples/demo.ml             |  4 +++-
 examples/simple.ml           | 21 +++++++++++----------
 lwt/nats_client_lwt.ml       |  9 ++++++++-
 tests/e2e/test_lwt_client.ml | 14 ++++++++++----
 5 files changed, 43 insertions(+), 23 deletions(-)

diff --git a/README.md b/README.md
index 2eb6c47..9ec25a5 100644
--- a/README.md
+++ b/README.md
@@ -28,12 +28,17 @@ Take it from [`examples/simple.ml`](./examples/simple.ml).
 ```ocaml
 let () =
   Lwt_main.run @@
-  (* Make connection with NATS server. *)
-  let%lwt client = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in
 
-  (* Initialize connection with parameters. Required. *)
-  client#init
-    { echo = true; tls_required = false; pedantic = false; verbose = false };%lwt
+  (* Connect to a NATS server. *)
+  Nats_client_lwt.with_connect_to ~port:4222 ~host:"127.0.0.1"
+       ~init:
+         {
+           echo = true;
+           tls_required = false;
+           pedantic = false;
+           verbose = false;
+         }
+  @@ fun client ->
 
   (* Subscribe to HELLO subject. *)
   let%lwt hello_subject = client#sub ~subject:"HELLO" () in
@@ -45,8 +50,7 @@ let () =
   (* Send "Hello World" message to HELLO subject. *)
   client#pub ~subject:"HELLO" "Hello World";%lwt
 
-  Lwt_unix.sleep 0.1;%lwt
-  client#close
+  Lwt_unix.sleep 0.1
 ```
 
 ```console
diff --git a/examples/demo.ml b/examples/demo.ml
index e833044..09aac1a 100644
--- a/examples/demo.ml
+++ b/examples/demo.ml
@@ -1,5 +1,7 @@
 let main () =
-  let%lwt client = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in
+  let%lwt client =
+    Nats_client_lwt.connect_to { port = 4222; host = "127.0.0.1" }
+  in
 
   Format.printf "info %a\n" Yojson.Safe.pp client#info;
 
diff --git a/examples/simple.ml b/examples/simple.ml
index e908ff6..251f41c 100644
--- a/examples/simple.ml
+++ b/examples/simple.ml
@@ -1,13 +1,15 @@
 let () =
   Lwt_main.run
-  @@
-  (* Make connection with NATS server. *)
-  let%lwt client = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in
-
-  (* Initialize connection with parameters. Required. *)
-  client#init
-    { echo = true; tls_required = false; pedantic = false; verbose = false };%lwt
-
+  (* Connect to a NATS server. *)
+  @@ Nats_client_lwt.with_connect_to ~port:4222 ~host:"127.0.0.1"
+       ~init:
+         {
+           echo = true;
+           tls_required = false;
+           pedantic = false;
+           verbose = false;
+         }
+  @@ fun client ->
   (* Subscribe to HELLO subject. *)
   let%lwt hello_subject = client#sub ~subject:"HELLO" () in
 
@@ -18,5 +20,4 @@ let () =
   (* Send "Hello World" message to HELLO subject. *)
   client#pub ~subject:"HELLO" "Hello World";%lwt
 
-  Lwt_unix.sleep 0.1;%lwt
-  client#close
+  Lwt_unix.sleep 0.1
diff --git a/lwt/nats_client_lwt.ml b/lwt/nats_client_lwt.ml
index 48f14ec..c5ee704 100644
--- a/lwt/nats_client_lwt.ml
+++ b/lwt/nats_client_lwt.ml
@@ -64,7 +64,7 @@ class client ~(info : Yojson.Safe.t) ~(connection : Connection.t) =
   end
 
 (** @raises Connection.Connection_refused *)
-let make settings =
+let connect_to settings =
   let%lwt connection = Connection.create settings in
   let%lwt info =
     match%lwt Connection.receive connection with
@@ -72,3 +72,10 @@ let make settings =
     | _ -> raise @@ Invalid_response "INFO message"
   in
   Lwt.return @@ new client ~info ~connection
+
+(** @raises Connection.Connection_refused *)
+let with_connect_to ~port ~host ~(init : Message.Initial.t) f =
+  let%lwt client = connect_to { port; host } in
+  client#init init;%lwt
+  f client;%lwt
+  client#close
diff --git a/tests/e2e/test_lwt_client.ml b/tests/e2e/test_lwt_client.ml
index de0d28c..6a2288b 100644
--- a/tests/e2e/test_lwt_client.ml
+++ b/tests/e2e/test_lwt_client.ml
@@ -1,24 +1,30 @@
 open Alcotest
 
 let init_test_correct_address _ () =
-  let%lwt _ = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in
+  let%lwt _ = Nats_client_lwt.connect_to { port = 4222; host = "127.0.0.1" } in
   Lwt.return_unit
 
 let init_test_wrong_address _ () =
   try%lwt
-    let%lwt _ = Nats_client_lwt.make { port = 42222; host = "127.0.0.1" } in
+    let%lwt _ =
+      Nats_client_lwt.connect_to { port = 42222; host = "127.0.0.1" }
+    in
     fail "Expected Connection_refused exception, but none was raised"
   with Nats_client_lwt.Connection.Connection_refused -> Lwt.return_unit
 
 let connect_test _ () =
-  let%lwt client = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in
+  let%lwt client =
+    Nats_client_lwt.connect_to { port = 4222; host = "127.0.0.1" }
+  in
   client#init
     { echo = true; tls_required = false; pedantic = false; verbose = true };%lwt
 
   Lwt.return ()
 
 let connect_test_with_verbose_false _ () =
-  let%lwt client = Nats_client_lwt.make { port = 4222; host = "127.0.0.1" } in
+  let%lwt client =
+    Nats_client_lwt.connect_to { port = 4222; host = "127.0.0.1" }
+  in
   client#init
     { echo = true; tls_required = false; pedantic = false; verbose = false };%lwt
 

From 60db3538440aa65ab23269793afc9b7e53617d7a Mon Sep 17 00:00:00 2001
From: dx3mod <dx3mod@bk.ru>
Date: Thu, 31 Oct 2024 12:26:38 +0300
Subject: [PATCH 2/2] Fix installation section

---
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 9ec25a5..7cd5e6b 100644
--- a/README.md
+++ b/README.md
@@ -17,7 +17,7 @@
 Currently only a development version is available. You can [pin][opam-pin]
 it using the [OPAM] package manager. 
 ```console
-$ opam nats-client-lwt.pin https://github.com/romanchechyotkin/nats.ocaml.git
+$ opam pin nats-client-lwt.dev https://github.com/romanchechyotkin/nats.ocaml.git
 ```
 
 ### Simple echo example