From d76110a5ac8abf363c881380092cfb696b8dbc00 Mon Sep 17 00:00:00 2001 From: Pierre de Lacroix Date: Sun, 27 Dec 2020 00:24:43 +0100 Subject: define client behaviour --- lib/matrix_app_service/client.ex | 8 +------- lib/matrix_app_service/client_behaviour.ex | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 lib/matrix_app_service/client_behaviour.ex diff --git a/lib/matrix_app_service/client.ex b/lib/matrix_app_service/client.ex index 08407dc..8fbdc35 100644 --- a/lib/matrix_app_service/client.ex +++ b/lib/matrix_app_service/client.ex @@ -5,6 +5,7 @@ defmodule MatrixAppService.Client do Library users can use the wrapped functions or call `user/1` and pass the returned struct to Polyjuice functions. """ + @behaviour MatrixAppService.ClientBehaviour @type client_options :: {:base_url, String.t()} | MatrixAppService.Client.LowLevel.create_opts() @@ -20,8 +21,6 @@ defmodule MatrixAppService.Client do * `:user_id`: user ID * `:storage`: a `t:Polyjuice.Client.Storage.t/0` """ - @spec client([client_options()]) :: - Polyjuice.Client.LowLevel.t() def client(opts \\ []) do base_url = Keyword.get(opts, :base_url) || @@ -54,7 +53,6 @@ defmodule MatrixAppService.Client do 1. `options`: see `Polyjuice.Client.Room.create_room/2` 2. `client_options`: see `client/1` """ - @spec create_room(Keyword.t()) :: {:ok, String.t()} | Any def create_room(options \\ [], client_options \\ []) do client(client_options) |> Polyjuice.Client.Room.create_room(options) @@ -68,7 +66,6 @@ defmodule MatrixAppService.Client do 2. `room_alias`: room alias 3. `client_options`: see `client/1` """ - @spec create_alias(String.t(), String.t(), client_options()) :: {:ok, String.t()} | Any def create_alias(room_id, room_alias, client_options \\ []) do client(client_options) |> Polyjuice.Client.Room.create_alias(room_id, room_alias) @@ -82,7 +79,6 @@ defmodule MatrixAppService.Client do 2. `msg`: see `Polyjuice.Client.Room.send_message/3` 3. `client_options`: see `client/1` """ - @spec send_message(String.t(), String.t(), client_options()) :: {:ok, String.t()} | Any def send_message(room_id, msg, client_options \\ []) do client(client_options) |> Polyjuice.Client.Room.send_message(room_id, msg) @@ -101,8 +97,6 @@ defmodule MatrixAppService.Client do `"guest"` 2. `client_options`: see `client/1` """ - @spec register(Polyjuice.Client.LowLevel.register_opts(), client_options()) :: - {:ok, String.t()} | Any def register(opts \\ [], client_options \\ []) do default_opts = [ inhibit_login: true, diff --git a/lib/matrix_app_service/client_behaviour.ex b/lib/matrix_app_service/client_behaviour.ex new file mode 100644 index 0000000..cf56286 --- /dev/null +++ b/lib/matrix_app_service/client_behaviour.ex @@ -0,0 +1,20 @@ +defmodule MatrixAppService.ClientBehaviour do + @moduledoc """ + Behaviour defining callbacks implemented in `MatrixAppService.Client`. Can be used to mock the client, for instance with `mox`: + + TODO: example + """ + + @type client_options :: {:base_url, String.t()} | MatrixAppService.Client.LowLevel.create_opts() + + @callback client() :: + Polyjuice.Client.LowLevel.t() + @callback client([client_options()]) :: + Polyjuice.Client.LowLevel.t() + @callback create_room(Keyword.t()) :: {:ok, String.t()} | Any + @callback create_alias(String.t(), String.t(), client_options()) :: {:ok, String.t()} | Any + @callback send_message(String.t(), String.t(), client_options()) :: {:ok, String.t()} | Any + @callback register(Polyjuice.Client.LowLevel.register_opts(), client_options()) :: + {:ok, String.t()} | Any + @callback get_profile(Polyjuice.Client.LowLevel.t(), String.t()) :: {:ok, map()} | any +end -- cgit v1.2.3