From 1b6d6fc8e83c594653a3f19490c9d1e4cb80234f Mon Sep 17 00:00:00 2001 From: Steven Blowers Date: Fri, 16 Sep 2016 15:31:09 +0100 Subject: refactoring adding headers to common helpers --- lib/zendex/common_helpers.ex | 7 +++++++ lib/zendex/search.ex | 4 ++-- lib/zendex/ticket.ex | 10 +++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/zendex/common_helpers.ex b/lib/zendex/common_helpers.ex index 34aac30..dceb56d 100644 --- a/lib/zendex/common_helpers.ex +++ b/lib/zendex/common_helpers.ex @@ -5,4 +5,11 @@ defmodule Zendex.CommonHelpers do def decode_response(%{body: body}), do: Poison.decode!(body) + def get_headers(authentication) do + [{"Authorization", "Basic #{authentication}"}] + end + + def get_headers(authentication, %{content_type: :json}) do + get_headers(authentication) ++ [{"Content-Type", "application/json"}] + end end diff --git a/lib/zendex/search.ex b/lib/zendex/search.ex index 13843d4..badf0bb 100644 --- a/lib/zendex/search.ex +++ b/lib/zendex/search.ex @@ -10,14 +10,14 @@ defmodule Zendex.Search do @spec query(Zendex.Connection.t, map, String.t, String.t) :: map def query(connection, query, sort_by \\ "", sort_order \\ "desc") do + search_string = create_search_string(query) sort_string = create_sort_string(sort_by, sort_order) full_uri = connection.base_url <> @url <> search_string <> sort_string full_uri - |> @http_client.get!([{"Authorization", - "Basic #{connection.authentication}"}]) + |> @http_client.get!(CommonHelpers.get_headers(connection.authentication)) |> CommonHelpers.decode_response end diff --git a/lib/zendex/ticket.ex b/lib/zendex/ticket.ex index 6d2d75b..3be6789 100644 --- a/lib/zendex/ticket.ex +++ b/lib/zendex/ticket.ex @@ -13,7 +13,7 @@ defmodule Zendex.Ticket do def list(connection) do connection.base_url |> Kernel.<>(@url) - |> @http_client.get!(headers(connection.authentication)) + |> @http_client.get!(CommonHelpers.get_headers(connection.authentication)) |> CommonHelpers.decode_response end @@ -22,12 +22,8 @@ defmodule Zendex.Ticket do connection.base_url |> Kernel.<>(@url) |> @http_client.post!(Poison.encode!(ticket), - headers(connection.authentication) ++ - [{"Content-Type", "application/json"}]) + CommonHelpers.get_headers(connection.authentication, + %{content_type: :json})) |> CommonHelpers.decode_response end - - defp headers(authentication) do - [{"Authorization", "Basic #{authentication}"}] - end end -- cgit v1.2.1