]> Entropealabs - wampex_client.git/commitdiff
updated client interface
authorChristopher <chris@entropealabs.com>
Wed, 1 Apr 2020 01:59:14 +0000 (20:59 -0500)
committerChristopher <chris@entropealabs.com>
Wed, 1 Apr 2020 01:59:14 +0000 (20:59 -0500)
lib/client.ex
mix.exs
mix.lock
test/support/test_callee.ex
test/support/test_subscriber.ex
test/wampex_test.exs

index b00a29a6468a15a7e3d24f8069a3f475115aa937..e7b2ae79b0bec6a24e599a0ef6496d5a048de10d 100644 (file)
@@ -6,7 +6,14 @@ defmodule Wampex.Client do
 
   alias Wampex.Client.Session, as: Sess
   alias Wampex.Roles.{Callee, Subscriber}
+  alias Wampex.Roles.Callee.Yield
   alias Wampex.Roles.Callee.Register
+  alias Wampex.Roles.Caller
+  alias Wampex.Roles.Caller.Call
+  alias Wampex.Roles.Dealer.Result
+  alias Wampex.Roles.Peer.Error
+  alias Wampex.Roles.Publisher
+  alias Wampex.Roles.Publisher.Publish
   alias Wampex.Roles.Subscriber.Subscribe
 
   @spec start_link(name: atom(), session_data: Sess.t()) ::
@@ -61,14 +68,29 @@ defmodule Wampex.Client do
     {:ok, id}
   end
 
-  @spec send_request(name :: module(), request :: Wampex.message(), timeout :: integer()) ::
+  @spec yield(name :: module(), yield :: Yield.t()) :: :ok
+  def yield(name, yield) do
+    cast(name, Callee.yield(yield))
+  end
+
+  @spec call(name :: module(), call :: Call.t(), timeout :: integer) :: Result.t() | Error.t()
+  def call(name, call, timeout \\ 5000) do
+    sync(name, Caller.call(call), timeout)
+  end
+
+  @spec publish(name :: module, event :: Publish.t()) :: :ok
+  def publish(name, event) do
+    cast(name, Publisher.publish(event))
+  end
+
+  @spec sync(name :: module(), request :: Wampex.message(), timeout :: integer()) ::
           term()
-  def send_request(name, request, timeout \\ 5000) do
+  def sync(name, request, timeout \\ 5000) do
     Sess.send_request(session_name(name), request, timeout)
   end
 
-  @spec cast_send_request(name :: module(), Wampex.message()) :: :ok
-  def cast_send_request(name, request) do
+  @spec cast(name :: module(), Wampex.message()) :: :ok
+  def cast(name, request) do
     Sess.cast_send_request(session_name(name), request)
   end
 end
diff --git a/mix.exs b/mix.exs
index c7b1a75dd41783df300e20795f904aa10f9e494f..2d2c415ab12723ec2388ec4fa44a1a4da368d96c 100644 (file)
--- a/mix.exs
+++ b/mix.exs
@@ -47,7 +47,7 @@ defmodule Wampex.Client.MixProject do
       {:states_language, "~> 0.2"},
       {:wampex,
        git: "https://gitlab.com/entropealabs/wampex.git",
-       tag: "92648dd6f1a4a9ed24b10eec3533d29108b31852"},
+       tag: "591a03f9ed1585942afc043fe4a73beb7641e1e3"},
       {:websockex, "~> 0.4.2"}
     ]
   end
index 9f23ef2709e05186b12cbeddb5b1d3885ea7fcf9..18203db75d3bf715323f84ac28b2f1e09874621f 100644 (file)
--- a/mix.lock
+++ b/mix.lock
@@ -24,7 +24,7 @@
   "states_language": {:hex, :states_language, "0.2.8", "f9dfd3c0bd9a9d7bda25ef315f2d90944cd6b2022a7f3c403deb1d4ec451825e", [:mix], [{:elixpath, "~> 0.1.0", [hex: :elixpath, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:json_xema, "~> 0.4.0", [hex: :json_xema, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:xema, "~> 0.11.0", [hex: :xema, repo: "hexpm", optional: false]}], "hexpm", "a5231691e7cb37fe32dc7de54c2dc86d1d60e84c4f0379f3246e55be2a85ec78"},
   "telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"},
   "unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
-  "wampex": {:git, "https://gitlab.com/entropealabs/wampex.git", "92648dd6f1a4a9ed24b10eec3533d29108b31852", [tag: "92648dd6f1a4a9ed24b10eec3533d29108b31852"]},
+  "wampex": {:git, "https://gitlab.com/entropealabs/wampex.git", "591a03f9ed1585942afc043fe4a73beb7641e1e3", [tag: "591a03f9ed1585942afc043fe4a73beb7641e1e3"]},
   "websockex": {:hex, :websockex, "0.4.2", "9a3b7dc25655517ecd3f8ff7109a77fce94956096b942836cdcfbc7c86603ecc", [:mix], [], "hexpm", "803cd76e91544b56f0e655e36790be797fa6436db9224f7c303db9b9df2a3df4"},
   "xema": {:hex, :xema, "0.11.0", "7b5118418633cffc27092110d02d4faeea938149dd3f6c64299e41e747067e80", [:mix], [{:conv_case, "~> 0.2.2", [hex: :conv_case, repo: "hexpm", optional: false]}, {:decimal, "~> 1.7", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "51491c9a953d65069d4b30aa2f70bc45ff99fd1bc3345bc72ce4e644d01ea14e"},
 }
index c0866b98475d4ba1b1e70749ba77dd459a8b370d..790a607f5fe287ad0c358fb1d429123d4c34ec96 100644 (file)
@@ -18,20 +18,20 @@ defmodule TestCallee do
   end
 
   def handle_info(
-        %Invocation{request_id: id, details: dets, arg_kw: arg_kw} = invocation,
+        %Invocation{request_id: id, details: %{"procedure" => proc}, arg_kw: arg_kw} = invocation,
         {test, name, _reg} = state
       ) do
-    Logger.info("Got invocation #{inspect(dets)}")
+    Logger.info("Got invocation #{proc}")
 
     send(test, invocation)
 
-    Client.cast_send_request(
+    Client.yield(
       name,
-      Callee.yield(%Yield{
+      %Yield{
         request_id: id,
         arg_list: [:ok],
         arg_kw: %{color: Map.get(arg_kw, "color")}
-      })
+      }
     )
 
     {:noreply, state}
index e86d354e30263681e0bbcf5d479107f9a968c652..7bdd652208ed5853c559faa28ae10611eb4e2133 100644 (file)
@@ -4,7 +4,7 @@ defmodule TestSubscriber do
   require Logger
   alias Wampex.Client
   alias Wampex.Roles.Subscriber
-  alias Subscriber.{Subscribe, Unsubscribe}
+  alias Subscriber.Subscribe
 
   def start_link(test, name, topic) do
     GenServer.start_link(__MODULE__, {test, name, topic})
@@ -32,15 +32,4 @@ defmodule TestSubscriber do
     send(test, event)
     {:noreply, state}
   end
-
-  def terminate(_r, {_test, name, subs}) do
-    Enum.each(subs, fn sub ->
-      Client.send_request(
-        name,
-        Subscriber.unsubscribe(%Unsubscribe{subscription_id: sub})
-      )
-    end)
-
-    :ok
-  end
 end
index 7d393b7496951f7b92820f3b74f6daf93b2d4973..62b8456f8fbf402b35caf096050ecb488ea8cb58 100644 (file)
@@ -8,13 +8,13 @@ defmodule WampexTest do
   alias Wampex.Roles.{Callee, Caller, Peer, Publisher, Subscriber}
   alias Wampex.Roles.Caller.Call
   alias Wampex.Roles.Dealer.{Invocation, Result}
-  alias Wampex.Roles.Peer.Hello
+  alias Wampex.Roles.Peer.{Error, Hello}
   alias Wampex.Roles.Publisher.Publish
   require Logger
 
   @url "ws://localhost:4000/ws"
   @authid "admin"
-  @auth_password "bnASR5qF9y8k/sHF6S+NneCOhvVI0zFkvoKQpc2F+hA="
+  @auth_password "test1234"
   @realm_uri "admin"
   @auth %Authentication{authid: @authid, authmethods: ["wampcra"], secret: @auth_password}
   @realm %Realm{name: @realm_uri, authentication: @auth}
@@ -36,7 +36,7 @@ defmodule WampexTest do
     Process.flag(:trap_exit, true)
     callee_name = TestAbort
     {:ok, pid} = Client.start_link(name: callee_name, session: @session)
-    Client.cast_send_request(callee_name, Peer.hello(%Hello{realm: "test", roles: [Callee]}))
+    Client.cast(callee_name, Peer.hello(%Hello{realm: "test", roles: [Callee]}))
     assert_receive {:EXIT, ^pid, :shutdown}, 1000
   end
 
@@ -45,16 +45,14 @@ defmodule WampexTest do
     caller_name = TestExistCaller
     Client.start_link(name: caller_name, session: @session)
 
-    assert {:error, 48, "wamp.error.no_registration", %{"procedure" => "this.should.not.exist"},
-            [],
-            %{}} =
-             Client.send_request(
+    assert %Error{error: "wamp.error.no_registration"} =
+             Client.call(
                caller_name,
-               Caller.call(%Call{
+               %Call{
                  procedure: "this.should.not.exist",
                  arg_list: [1],
                  arg_kw: %{color: "#FFFFFF"}
-               })
+               }
              )
   end
 
@@ -63,16 +61,20 @@ defmodule WampexTest do
     caller_name = TestAdminCaller
     Client.start_link(name: caller_name, session: @session)
 
-    %Result{arg_list: [id]} =
-      Client.send_request(
-        caller_name,
-        Caller.call(%Call{
-          procedure: "admin.create_peer",
-          arg_kw: %{authid: "chris", password: "woot!", realm: @realm_uri}
-        })
-      )
-
-    assert is_binary(id)
+    assert %Result{arg_kw: %{"authid" => "chris"}} =
+             Client.call(
+               caller_name,
+               %Call{
+                 procedure: "admin.create_peer",
+                 arg_kw: %{
+                   authid: "chris",
+                   password: "woot!",
+                   realm: @realm_uri,
+                   roles: [],
+                   tenant: "org.entropealabs"
+                 }
+               }
+             )
   end
 
   @tag :client
@@ -80,13 +82,19 @@ defmodule WampexTest do
     caller_name = TestAdminErrorCaller
     Client.start_link(name: caller_name, session: @session)
 
-    assert {:error, 48, "Error registering user", %{}, [], %{}} =
-             Client.send_request(
+    assert %Error{error: "error"} =
+             Client.call(
                caller_name,
-               Caller.call(%Call{
+               %Call{
                  procedure: "admin.create_peer",
-                 arg_kw: %{authid: "chris", password: "woot!", realm: "not.real"}
-               })
+                 arg_kw: %{
+                   authid: "chris",
+                   password: "woot!",
+                   realm: "not.real",
+                   roles: [],
+                   tenant: "org.entropealabs"
+                 }
+               }
              )
   end
 
@@ -100,13 +108,13 @@ defmodule WampexTest do
     Client.start_link(name: caller_name, session: @session)
 
     %Result{arg_list: ["ok"], arg_kw: %{"color" => "#FFFFFF"}} =
-      Client.send_request(
+      Client.call(
         caller_name,
-        Caller.call(%Call{
+        %Call{
           procedure: "com.actuator.#{@device}.light",
           arg_list: [1],
           arg_kw: %{color: "#FFFFFF"}
-        })
+        }
       )
 
     assert_receive %Invocation{}
@@ -129,13 +137,13 @@ defmodule WampexTest do
 
     Client.start_link(name: TestPublisher, session: @session)
 
-    Client.cast_send_request(
+    Client.publish(
       TestPublisher,
-      Publisher.publish(%Publish{
+      %Publish{
         topic: "com.data.test.temp",
         arg_list: [12.5, 45.6, 87.5],
         arg_kw: %{loc: "60645"}
-      })
+      }
     )
 
     assert_receive %Event{}, 2000