Skip to content

Commit

Permalink
Merge pull request #1 from jgchristopher/add_support_for_headers
Browse files Browse the repository at this point in the history
Support passing in headers to Tentacat
  • Loading branch information
jgchristopher committed Feb 6, 2021
2 parents 6e4de63 + 59f10eb commit b7b0820
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/tentacat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,20 @@ defmodule Tentacat do
"""
@spec get(binary, Client.t()) :: response
@spec get(binary, Client.t(), keyword) :: response
@spec get(binary, Client.t(), keyword, keyword) ::
response | Enumerable.t() | pagination_response
def get(path, client, params \\ [], options \\ []) do
@spec get(binary, Client.t(), keyword, keyword) :: response | Enumerable.t() | pagination_response
@spec get(binary, Client.t(), keyword, keyword, keyword) :: response | Enumerable.t() | pagination_response
def get(path, client, params \\ [], options \\ [], headers \\ []) do
url =
client
|> url(path)
|> add_params_to_url(params)

case pagination(options) do
nil -> request_stream(:get, url, client.auth)
:none -> request_stream(:get, url, client.auth, "", :one_page)
:auto -> request_stream(:get, url, client.auth)
:stream -> request_stream(:get, url, client.auth, "", :stream)
:manual -> request_with_pagination(:get, url, client.auth)
nil -> request_stream(:get, url, client.auth, "", nil, headers)
:none -> request_stream(:get, url, client.auth, "", :one_page, headers)
:auto -> request_stream(:get, url, client.auth, "", nil, headers)
:stream -> request_stream(:get, url, client.auth, "", :stream, headers)
:manual -> request_with_pagination(:get, url, client.auth, headers)
end
end

Expand All @@ -101,8 +101,8 @@ defmodule Tentacat do
Application.get_env(:tentacat, :request_options, [])
end

defp extra_headers do
Application.get_env(:tentacat, :extra_headers, [])
defp extra_headers(headers) do
Application.get_env(:tentacat, :extra_headers, headers)
end

defp deserialization_options do
Expand All @@ -116,14 +116,14 @@ defmodule Tentacat do

def raw_request(method, url, body \\ "", headers \\ [], options \\ []) do
method
|> request!(url, body, extra_headers() ++ headers, extra_options() ++ options)
|> request!(url, body, extra_headers(headers), extra_options() ++ options)
|> process_response
end

@spec request_stream(atom, binary, Client.auth(), any, :one_page | nil | :stream) ::
Enumerable.t() | response
def request_stream(method, url, auth, body \\ "", override \\ nil) do
request_with_pagination(method, url, auth, Jason.encode!(body))
def request_stream(method, url, auth, body \\ "", override \\ nil, headers \\ []) do
request_with_pagination(method, url, auth, Jason.encode!(body), headers)
|> stream_if_needed(override)
end

Expand Down Expand Up @@ -157,14 +157,14 @@ defmodule Tentacat do
{[item], {[], next, auth}}
end

@spec request_with_pagination(atom, binary, Client.auth(), any) :: pagination_response
def request_with_pagination(method, url, auth, body \\ "") do
@spec request_with_pagination(atom, binary, Client.auth(), any, keyword) :: pagination_response
def request_with_pagination(method, url, auth, body \\ "", headers \\ []) do
resp =
request!(
method,
url,
Jason.encode!(body),
authorization_header(auth, extra_headers() ++ @user_agent),
authorization_header(auth, extra_headers(headers) ++ @user_agent),
extra_options()
)

Expand Down
58 changes: 58 additions & 0 deletions lib/tentacat/users/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ defmodule Tentacat.Users.Events do
get("users/#{user}/events", client)
end

@doc """
List events performed by a user since last check represented by e_tag
## Example
Tentacat.Users.Events.list client, "bastos"
Tentacat.Users.Events.list client, "bastos", "2a97f8e2fba8b548ae56bce4074f25cc00674f213963d2f8cf579b8a3"
More info at: https://developer.github.com/v3/activity/events/#list-events-performed-by-a-user
"""

def list(client, user, e_tag) do
get("users/#{user}/events", client, [], [], [{"If-None-Match" , "\"#{e_tag}\""}])
end

@doc """
List public events performed by a user
Expand All @@ -32,6 +47,21 @@ defmodule Tentacat.Users.Events do
get("users/#{user}/events/public", client)
end

@doc """
List public events performed by a user since last check represented by e_tag
## Example
Tentacat.Users.Events.list_public client, "bastos"
Tentacat.Users.Events.list_public client, "bastos", "2a97f8e2fba8b548ae56bce4074f25cc00674f213963d2f8cf579b8a3"
More info at: https://developer.github.com/v3/activity/events/#list-public-events-performed-by-a-user
"""
@spec list_public(Client.t(), binary, binary) :: Tentacat.response()
def list_public(client, user, e_tag) do
get("users/#{user}/events/public", client, [], [], [{"If-None-Match" , "\"#{e_tag}\""}])
end

@doc """
List events for an organization
Expand All @@ -46,6 +76,20 @@ defmodule Tentacat.Users.Events do
get("users/#{user}/events/orgs/#{org}", client)
end

@doc """
List events for an organization since last check represented by e_tag
## Example
Tentacat.Users.Events.list_user_org client, "bastos", "elixir-lang", "2a97f8e2fba8b548ae56bce4074f25cc00674f213963d2f8cf579b8a3"
More info at: https://developer.github.com/v3/activity/events/#list-events-for-an-organization
"""
@spec list_user_org(Client.t(), binary, binary, binary) :: Tentacat.response()
def list_user_org(client, user, org, e_tag) do
get("users/#{user}/events/orgs/#{org}", client, [], [], [{"If-None-Match" , "\"#{e_tag}\""}])
end

@doc """
List public events that a user has received
Expand All @@ -59,4 +103,18 @@ defmodule Tentacat.Users.Events do
def list_received_public(client \\ %Client{}, user) do
get("users/#{user}/received_events/public", client)
end

@doc """
List public events that a user has received since last check represented by e_tag
## Example
Tentacat.Users.Events.list_received_public client, "bastos", "2a97f8e2fba8b548ae56bce4074f25cc00674f213963d2f8cf579b8a3"
More info at: https://developer.github.com/v3/activity/events/#list-public-events-that-a-user-has-received
"""
@spec list_received_public(Client.t(), binary, binary) :: Tentacat.response()
def list_received_public(client, user, e_tag) do
get("users/#{user}/received_events/public", client, [], [], [{"If-None-Match" , "\"#{e_tag}\""}])
end
end

0 comments on commit b7b0820

Please sign in to comment.