defmodule Untappd do use HTTPoison.Base alias Untappd.Client @user_agent [{"User-agent", "uptappd"}] @type response :: {integer, any} | :jsx.json_term @spec process_response(HTTPoison.Response.t) :: response def process_response(%HTTPoison.Response{status_code: 200, body: ""}), do: nil def process_response(%HTTPoison.Response{status_code: 200, body: body}), do: JSX.decode!(body) def process_response(%HTTPoison.Response{status_code: status_code, body: ""}), do: { status_code, nil } def process_response(%HTTPoison.Response{status_code: status_code, body: body }), do: { status_code, JSX.decode!(body) } def delete(path, client, body \\ "") do _request(:delete, url(client, path), client.auth, body) end def post(path, client, body \\ "") do _request(:post, url(client, path), client.auth, body) end def patch(path, client, body \\ "") do _request(:patch, url(client, path), client.auth, body) end def put(path, client, body \\ "") do _request(:put, url(client, path), client.auth, body) end def get(path, client, params \\ []) do initial_url = url(client, path) url = add_params_to_url(initial_url, params) _request(:get, url, client.auth) end def _request(method, url, _auth, body \\ "") do json_request(method, url, body, @user_agent) end def json_request(method, url, body \\ "", headers \\ [], options \\ []) do request!(method, url, JSX.encode!(body), headers, options) |> process_response end def raw_request(method, url, body \\ "", headers \\ [], options \\ []) do request!(method, url, body, headers, options) |> process_response end @spec url(client :: Client.t, path :: binary) :: binary defp url(_client = %Client{endpoint: endpoint, auth: auth}, path) do endpoint <> path <> "?client_id=" <> auth.client_id <> "&client_secret=" <> auth.client_secret end defp add_params_to_url(url, params) do <> end @spec build_qs([{atom, binary}]) :: binary defp build_qs([]), do: "" defp build_qs(kvs), do: to_string('&' ++ URI.encode_query(kvs)) def authorization(%{client_id: client_id, client_secret: client_secret}) do %{client_id: client_id, client_secret: client_secret} end def authorization(%{access_token: token}, headers) do headers ++ [{"Authorization", "token #{token}"}] end def authorization(_, headers), do: headers end