Packages

Heroku API client and Heroku Mix tasks for Elixir projects.

Current section

Files

Jump to
hexoku lib hexoku.ex
Raw

lib/hexoku.ex

defmodule Hexoku do
@moduledoc """
Basic API support and MIX tasks for Heroku support.
## Warning
This is early work and few API and Mix commands have been finished. I am adding more every day(ish) though.
Some API functions *might* completely change between versions.
## Examples
client = Hexoku.toolbelt
client |> Hexoku.API.Apps.list
For complete info read the [Heroku API Reference](https://devcenter.heroku.com/articles/platform-api-reference)
## Mix Support
Hexoku adds some common Heroku commands to your Mix.
The application name is read from the heroku_app key in your `mix.exs` files project.
If that is not defined it falls back to the :app key.
def project do
[
app: :uberweb,
heroku_app: "uberweb-staging"
version: "0.0.1",
elixir: "~> 1.0.0",
deps: deps
]
end
It relies on you having your credentials set up in your `~/.netrc` file.
If you are already using the [Heroku Toolbet](toolbelt.heroku.com) you have likely already set this up using the '`heroku login`' command.
Otherwise you can create `~/.netrc` with the following body:
machine api.heroku.com
login user@example.com
password a1b2c3e4f5
machine code.heroku.com
login user@example.com
password a1b2c3e4f5
### Examples
bash> mix hexoku.config
DOWNSTREAM_APP = myapp-production
REDIS_HOST = somehost.redistogo.com
"""
defmodule Client do
defstruct auth: nil
@type auth :: Hexoku.Client.Auth.OAuth2.t | Hexoku.Client.Auth.Basic.t
@type t :: %__MODULE__{auth: auth}
defmodule Auth do
defprotocol Authenticate do
def auth_header(auth)
end
defmodule Basic do
defstruct username: nil, password: nil
@type t :: %__MODULE__{username: binary, password: binary}
end
defimpl Authenticate, for: Basic do
def auth_header(auth), do: {"Authorization", "Basic " <> Base.encode64(auth.username <> ":" <> auth.password)}
end
defmodule OAuth2 do
defstruct token: nil
@type t :: %__MODULE__{token: binary}
end
defimpl Authenticate, for: OAuth2 do
def auth_header(auth), do: {"Authorization", Base.encode64(":" <> auth.token)}
end
end
end
defmodule Response do
defstruct status: nil, body: nil
@type t :: %__MODULE__{status: integer, body: any}
end
@doc """
Create a Hexoku client using username and password.
## Examples:
client = Hexoku.basic("HEROKU_USER", "HEROKU_PASS")
"""
@spec basic(binary, binary) :: Hexoku.Client.t
def basic(username, password) do
Hexoku.Request.start
%Hexoku.Client{
auth: %Hexoku.Client.Auth.Basic{username: username, password: password}
}
end
@doc """
Create a Hexoku client using OAuth2 token.
## Examples:
client = Hexoku.oauth2("HEROKU_KEY")
"""
@spec oauth2(binary) :: Hexoku.Client.t
def oauth2(token) do
Hexoku.Request.start
%Hexoku.Client{
auth: %Hexoku.Client.Auth.OAuth2{token: token}
}
end
@doc """
Create a Hexoku client using token from the [Heroku Toobelt](https://toolbelt.heroku.com/) if installed.
Optionally define a custom heroku command if you are using new [Go client](https://github.com/heroku/hk).
## Examples:
client = Hexoku.toolbelt
client = Hexoku.toolbelt"hk")
"""
@spec toolbelt(binary, binary) :: Hexoku.Client.t
def toolbelt(command \\ "heroku", argument \\ "auth:token") do
Hexoku.Request.start
{token, 0} = System.cmd command, [argument]
%Hexoku.Client{
auth: %Hexoku.Client.Auth.OAuth2{token: String.strip(token)}
}
end
end