From 29a86c1d5f169a37725bf379ebbf9e4b6acab703 Mon Sep 17 00:00:00 2001 From: Cam Date: Sun, 20 Nov 2016 13:44:30 -0800 Subject: [PATCH] Update hooks to properly use HTTPoison Allow `Hook` to properly use `headers` and `options` in requests using `HTTPoison`. --- README.md | 8 +++++--- lib/grapple/hook.ex | 20 ++++++++++---------- mix.exs | 2 +- test/hook_test.exs | 2 +- test/http_client.exs | 12 ++++++------ 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1acd2f6..a8fae00 100644 --- a/README.md +++ b/README.md @@ -47,12 +47,14 @@ The default struct, `%Grapple.Hook{}`, has the following fields: - `life` - `ref` - `method` -- `headers` - `body` +- `headers` +- `options` - `query` -- `timeout` -Note that `url` is **required**. +Note that `url` is **required**. Additionally, the fields `body`, `headers`, and +`options` all correspond to those used in `HTTPoison` requests. See [HTTPoison](https://github.com/edgurgel/httpoiso://github.com/edgurgel/httpoison) +for more info. **Topics** diff --git a/lib/grapple/hook.ex b/lib/grapple/hook.ex index cfc1477..64900d8 100644 --- a/lib/grapple/hook.ex +++ b/lib/grapple/hook.ex @@ -11,10 +11,10 @@ defmodule Grapple.Hook do :life, :ref, method: "GET", - headers: [], body: %{}, + headers: [], + options: [], query: %{}, - timeout: 5000, ] # API @@ -75,20 +75,20 @@ defmodule Grapple.Hook do # Helpers - defp notify(webhook = %Grapple.Hook{method: "GET"}, _body) do - @http.get(webhook.url, webhook.headers) + defp notify(hook = %Grapple.Hook{method: "GET"}, _body) do + @http.get(hook.url, hook.headers, hook.options) end - defp notify(webhook = %Grapple.Hook{method: "POST"}, body) do - @http.post(webhook.url, webhook.headers, body) + defp notify(hook = %Grapple.Hook{method: "POST"}, body) do + @http.post(hook.url, body, hook.headers, hook.options) end - defp notify(webhook = %Grapple.Hook{method: "PUT"}, body) do - @http.put(webhook.url, webhook.headers, body) + defp notify(hook = %Grapple.Hook{method: "PUT"}, body) do + @http.put(hook.url, body, hook.headers, hook.options) end - defp notify(webhook = %Grapple.Hook{method: "DELETE"}, _body) do - @http.delete(webhook.url, webhook.headers) + defp notify(hook = %Grapple.Hook{method: "DELETE"}, _body) do + @http.delete(hook.url, hook.headers, hook.options) end defp send_to_owner(%{owner: owner}, response) when is_pid(owner) do diff --git a/mix.exs b/mix.exs index 8d63f66..ded27dd 100644 --- a/mix.exs +++ b/mix.exs @@ -3,7 +3,7 @@ defmodule Grapple.Mixfile do def project do [app: :grapple, - version: "1.0.0", + version: "1.0.1", elixir: "~> 1.3", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, diff --git a/test/hook_test.exs b/test/hook_test.exs index fc51e4f..94b23fe 100644 --- a/test/hook_test.exs +++ b/test/hook_test.exs @@ -104,7 +104,7 @@ defmodule HookTest do defhook pokemon(name), do: name end - res = HookableArgs.pokemon("dragonite") + HookableArgs.pokemon("dragonite") assert_receive {:hook_response, ^pid, response} assert response == {:ok, %{body: %{}, status_code: 200}} diff --git a/test/http_client.exs b/test/http_client.exs index de66655..0a15f0d 100644 --- a/test/http_client.exs +++ b/test/http_client.exs @@ -1,24 +1,24 @@ defmodule Grapple.Test.HttpClient do - def get("NOT_FOUND", _headers) do + def get("NOT_FOUND", _headers, _options) do {:ok, %{status_code: 404}} end - def get("ERROR", _headers) do + def get("ERROR", _headers, _options) do {:error, %{reason: ""}} end - def get(_url, _headers) do + def get(_url, _headers, _options) do {:ok, %{status_code: 200, body: %{}}} end - def post(_url, _headers, body) do + def post(_url, body, _headers, _options) do {:ok, %{status_code: 200, body: body}} end - def put(_url, _headers, body) do + def put(_url, body, _headers, _options) do {:ok, %{status_code: 200, body: body}} end - def delete(_url, _headers) do + def delete(_url, _headers, _options) do {:ok, %{status_code: 200, body: %{}}} end