Skip to content

Commit

Permalink
pull the token module at runtime, not compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
peburrows committed Apr 11, 2018
1 parent 5dbb7f8 commit 8fdeec2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ erl_crash.dump
*.ez
/config/*credentials*
!/config/test-credentials.json
.elixir_ls/
17 changes: 10 additions & 7 deletions lib/kane/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@ defmodule Kane.Client do
alias Response.Success
alias Response.Error

@token_mod Application.get_env(:kane, :token, Goth.Token)

@spec get(binary) :: Success.t | Error.t
@spec get(binary) :: Success.t() | Error.t()
def get(path), do: call(:get, path)

@spec put(binary, any) :: Success.t | Error.t
@spec put(binary, any) :: Success.t() | Error.t()
def put(path, data \\ ""), do: call(:put, path, data)

@spec post(binary, any) :: Success.t | Error.t
@spec post(binary, any) :: Success.t() | Error.t()
def post(path, data), do: call(:post, path, data)

@spec delete(binary) :: Success.t | Error.t
@spec delete(binary) :: Success.t() | Error.t()
def delete(path), do: call(:delete, path)

defp call(method, path) do
headers = [auth_header()]

apply(HTTPoison, method, [url(path), headers])
|> handle_response
end

defp call(method, path, data) do
headers = [auth_header(), {"content-type", "application/json"}]

apply(HTTPoison, method, [url(path), encode!(data), headers])
|> handle_response
end

defp url(path), do: Path.join([endpoint(), path])

defp endpoint, do: Application.get_env(:kane, :endpoint, "https://pubsub.googleapis.com/v1")
defp token_mod, do: Application.get_env(:kane, :token, Goth.Token)

defp auth_header do
{:ok, token} = @token_mod.for_scope(Kane.oauth_scope())
{:ok, token} = token_mod().for_scope(Kane.oauth_scope())
{"Authorization", "#{token.type} #{token.token}"}
end

Expand All @@ -43,6 +45,7 @@ defmodule Kane.Client do
case response.status_code do
code when code in 200..299 ->
{:ok, response.body, code}

err ->
{:error, response.body, err}
end
Expand Down

0 comments on commit 8fdeec2

Please sign in to comment.