From f15f9bd6b62732e5f9397f605b2cb01e24745a84 Mon Sep 17 00:00:00 2001 From: Tomas Koutsky Date: Fri, 20 Apr 2018 09:53:42 +0200 Subject: [PATCH] Fix Api gun response handling & cleanup There was a mistake in `handle_info` clause responsible for awaiting to response body. Clean unused types. Add gen_server structure comments. --- lib/pushest.ex | 34 ++++++++-------------------------- lib/pushest/api.ex | 22 ++++++++++++++++------ lib/pushest/socket.ex | 12 ++++++++++++ mix.exs | 4 +++- mix.lock | 4 ++-- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/lib/pushest.ex b/lib/pushest.ex index a329ef0..701d57f 100644 --- a/lib/pushest.ex +++ b/lib/pushest.ex @@ -97,30 +97,6 @@ defmodule Pushest do defmacro __using__(opts) do quote bind_quoted: [opts: opts] do - @typedoc ~S""" - Options for Pushest to properly communicate with Pusher server. - - - `:app_id` - Pusher Application ID. - - `:key` - Pusher Application key. - - `:secret` - Necessary to subscribe to private/presence channels and trigger events. - - `:cluster` - Cluster where your Pusher app is configured. - - `:encrypted` - When set to true communication with Pusher is fully encrypted. - """ - @type pusher_opts :: %{ - app_id: String.t(), - secret: String.t(), - key: String.t(), - cluster: String.t(), - encrypted: boolean - } - - @typedoc ~S""" - Optional options for trigger function. - - - `:force_api` - Always triggers via Pusher REST API endpoint when set to `true` - """ - @type trigger_opts :: [force_api: boolean] - @behaviour Pushest @config Pushest.Supervisor.config(__MODULE__, opts) @@ -179,8 +155,6 @@ defmodule Pushest do Same as trigger/3 but adds a possiblity to enforce triggering via API endpoint. For enforced API trigger provide `force_api: true` as an `opts`. E.g.: `Mod.trigger("channel", "event", %{message: "m"}, force_api: true)` - - For trigger_opts values see `t:trigger_opts/0`. """ def trigger(channel, event, data, opts) do Router.cast({:trigger, channel, event, data}, opts) @@ -215,6 +189,14 @@ defmodule Pushest do Router.cast({:unsubscribe, channel}) end + def authenticate(_channel, _socket_id) do + {:error, "TBD"} + end + + def authenticate(_channel, _socket_id, _user_data) do + {:error, "TBD"} + end + @doc ~S""" Function meant to be overwritten in user module, e.g.: ``` diff --git a/lib/pushest/api.ex b/lib/pushest/api.ex index ad7a296..2963ace 100644 --- a/lib/pushest/api.ex +++ b/lib/pushest/api.ex @@ -15,6 +15,10 @@ defmodule Pushest.Api do @client Pushest.Client.for_env() @version Mix.Project.config()[:version] + ## ========================================================================== + ## Client + ## ========================================================================== + def start_link({pusher_opts, _callback_module, _init_channels}) do GenServer.start_link( __MODULE__, @@ -23,6 +27,10 @@ defmodule Pushest.Api do ) end + ## ========================================================================== + ## Server + ## ========================================================================== + def init(state = %State{url: %Url{domain: domain, port: port}}) do {:ok, conn_pid} = @client.open(domain, port) Process.monitor(conn_pid) @@ -78,11 +86,12 @@ defmodule Pushest.Api do end def handle_info( - {:gun_response, conn_pid, stream_ref, :fin, status, _headers}, + {:gun_response, conn_pid, stream_ref, :nofin, status, _headers}, state = %State{conn_pid: conn_pid} ) do case status do - 200 -> {:ok, _body} = :gun.await_body(conn_pid, stream_ref) + 200 -> + {:ok, _body} = :gun.await_body(conn_pid, stream_ref) _ -> Logger.error("Api | Pusher response status #{inspect(status)}") end @@ -107,6 +116,10 @@ defmodule Pushest.Api do {:noreply, state} end + ## ========================================================================== + ## Private + ## ========================================================================== + defp get_headers do [ {"X-Pusher-Library", "Pushest #{@version}"} @@ -114,10 +127,7 @@ defmodule Pushest.Api do end defp post_headers do - [ - {"content-type", "application/json"}, - {"X-Pusher-Library", "Pushest #{@version}"} - ] + [{"content-type", "application/json"} | get_headers()] end defp client_sync(conn_pid, stream_ref) do diff --git a/lib/pushest/socket.ex b/lib/pushest/socket.ex index 0acfbd8..bbf853b 100644 --- a/lib/pushest/socket.ex +++ b/lib/pushest/socket.ex @@ -14,6 +14,10 @@ defmodule Pushest.Socket do @client Pushest.Client.for_env() + ## ========================================================================== + ## Client + ## ========================================================================== + def start_link(opts) do GenServer.start_link( __MODULE__, @@ -22,6 +26,10 @@ defmodule Pushest.Socket do ) end + ## ========================================================================== + ## Server + ## ========================================================================== + def init(state = %State{url: %Url{domain: domain, path: path, port: port}}) do {:ok, conn_pid} = @client.open(domain, port) Process.monitor(conn_pid) @@ -166,6 +174,10 @@ defmodule Pushest.Socket do {:noreply, state} end + ## ========================================================================== + ## Private + ## ========================================================================== + @spec do_subscribe(String.t(), map, %State{}) :: term defp do_subscribe(channel, user_data, state = %State{conn_pid: conn_pid}) do auth = Utils.auth(state, channel, user_data) diff --git a/mix.exs b/mix.exs index 6af03c5..4370bc3 100644 --- a/mix.exs +++ b/mix.exs @@ -1,10 +1,12 @@ defmodule Pushest.MixProject do + @moduledoc false + use Mix.Project def project do [ app: :pushest, - version: "0.2.1", + version: "0.2.2", elixir: "~> 1.6", start_permanent: Mix.env() == :prod, deps: deps(), diff --git a/mix.lock b/mix.lock index 15580e5..85d4b09 100644 --- a/mix.lock +++ b/mix.lock @@ -1,11 +1,11 @@ %{ "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], []}, "cowlib": {:hex, :cowlib, "2.2.1", "1afa5b233cee8f642c153ec6f1dc48db0dd9a43e2114bc8b43e9d59636f6ae1f", [:rebar3], []}, - "credo": {:hex, :credo, "0.9.0", "5d1b494e4f2dc672b8318e027bd833dda69be71eaac6eedd994678be74ef7cb4", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}, {:poison, ">= 0.0.0", [hex: :poison, optional: false]}]}, + "credo": {:hex, :credo, "0.9.1", "f021affa11b32a94dc2e807a6472ce0914289c9132f99644a97fc84432b202a1", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}, {:poison, ">= 0.0.0", [hex: :poison, optional: false]}]}, "dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], []}, "earmark": {:hex, :earmark, "1.2.5", "4d21980d5d2862a2e13ec3c49ad9ad783ffc7ca5769cf6ff891a4553fbaae761", [:mix], []}, "ex_doc": {:hex, :ex_doc, "0.18.3", "f4b0e4a2ec6f333dccf761838a4b253d75e11f714b85ae271c9ae361367897b7", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]}, "gun": {:hex, :gun, "1.0.0-pre.5", "69a11e259d5bf7fd9b1a74e1aa98e7979c2ae3ebbda27fe1b1c9cb4481f23ba3", [:rebar3], [{:cowlib, "~> 2.1", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.4", [hex: :ranch, optional: false]}]}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []}, - "ranch": {:hex, :ranch, "1.4.0", "10272f95da79340fa7e8774ba7930b901713d272905d0012b06ca6d994f8826b", [:rebar3], []}, + "ranch": {:hex, :ranch, "1.5.0", "f04166f456790fee2ac1aa05a02745cc75783c2bfb26d39faf6aefc9a3d3a58a", [:rebar3], []}, }