code-corps/code-corps-api

View on GitHub
lib/code_corps/github/api/headers.ex

Summary

Maintainability
Test Coverage
defmodule CodeCorps.GitHub.API.Headers do
  alias CodeCorps.GitHub.API.JWT

  @typep header :: {String.t, String.t}
  @type t :: list(header)

  @spec user_request(%{String.t => String.t} | %{}, list) :: t
  def user_request(%{} = headers, options) do
    headers
    |> add_default_headers()
    |> add_access_token_header(options)
    |> Map.to_list()
  end

  @spec integration_request(%{String.t => String.t} | %{}) :: t
  def integration_request(%{} = headers) do
    headers
    |> add_default_headers()
    |> add_jwt_header()
    |> Map.to_list()
  end

  @spec access_token_request :: t
  def access_token_request do
    %{"Accept" => "application/json", "Content-Type" => "application/json"}
    |> add_default_headers()
    |> Map.to_list()
  end

  @spec add_default_headers(%{String.t => String.t}) :: %{String.t => String.t}
  defp add_default_headers(%{} = headers) do
    Map.merge(%{"Accept" => "application/vnd.github.machine-man-preview+json"}, headers)
  end

  @spec add_access_token_header(%{String.t => String.t}, list) :: %{String.t => String.t}
  defp add_access_token_header(%{} = headers, opts) do
    case opts[:access_token] do
      nil -> headers
      token -> headers |> Map.put("Authorization", "token #{token}")
    end
  end

  @spec add_jwt_header(%{String.t => String.t}) :: %{String.t => String.t}
  defp add_jwt_header(%{} = headers) do
    Map.put(headers, "Authorization", "Bearer #{JWT.generate}")
  end
end