-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathheaders.ex
More file actions
47 lines (40 loc) · 1.42 KB
/
headers.ex
File metadata and controls
47 lines (40 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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