Skip to content

Commit

Permalink
Merge pull request #12 from stepnivlk/0.2.2
Browse files Browse the repository at this point in the history
Fix Api gun response handling & cleanup
  • Loading branch information
Tomas Koutsky authored Apr 20, 2018
2 parents 60d1fef + f15f9bd commit 0999464
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
34 changes: 8 additions & 26 deletions lib/pushest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.:
```
Expand Down
22 changes: 16 additions & 6 deletions lib/pushest/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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__,
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -107,17 +116,18 @@ defmodule Pushest.Api do
{:noreply, state}
end

## ==========================================================================
## Private
## ==========================================================================

defp get_headers do
[
{"X-Pusher-Library", "Pushest #{@version}"}
]
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
Expand Down
12 changes: 12 additions & 0 deletions lib/pushest/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ defmodule Pushest.Socket do

@client Pushest.Client.for_env()

## ==========================================================================
## Client
## ==========================================================================

def start_link(opts) do
GenServer.start_link(
__MODULE__,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -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], []},
}

0 comments on commit 0999464

Please sign in to comment.