diff options
Diffstat (limited to 'lib/exdatadog')
-rw-r--r-- | lib/exdatadog/client.ex | 33 | ||||
-rw-r--r-- | lib/exdatadog/config.ex | 15 |
2 files changed, 48 insertions, 0 deletions
diff --git a/lib/exdatadog/client.ex b/lib/exdatadog/client.ex new file mode 100644 index 0000000..02c1017 --- /dev/null +++ b/lib/exdatadog/client.ex @@ -0,0 +1,33 @@ +defmodule Exdatadog.Client do + @moduledoc """ + Datadog Client record for endpoint and authentication data + """ + + import Exdatadog.Config, only: [get_env_var: 2] + + defstruct auth: nil, endpoint: "https://app.datadoghq.com/" + + @type auth :: %{api_key: binary, app_key: binary} | %{api_key: binary} + @type t :: %__MODULE__{auth: auth, endpoint: binary} + + @spec new() :: t + def new() do + auth = %{api_key: get_env_var(:exdatadog, :api_key), + app_key: get_env_var(:exdatadog, :app_key)} + %__MODULE__{auth: auth} + end + + @spec new(auth) :: t + def new(auth), do: %__MODULE__{auth: auth} + + @spec new(auth, binary) :: t + def new(auth, endpoint) do + endpoint = if String.ends_with?(endpoint, "/") do + endpoint + else + endpoint <> "/" + end + %__MODULE__{auth: auth, endpoint: endpoint} + end + +end diff --git a/lib/exdatadog/config.ex b/lib/exdatadog/config.ex new file mode 100644 index 0000000..f686309 --- /dev/null +++ b/lib/exdatadog/config.ex @@ -0,0 +1,15 @@ +defmodule Exdatadog.Config do + @moduledoc """ + Provides helper functions for interacting with Application variables + """ + + def get_env_var(app, key, default \\ nil) do + app + |> Application.get_env(key, default) + |> case do + {:system, env_key} -> System.get_env(env_key) + env_var -> env_var + end + end + +end |