This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
balena-device/lib/balena_device/http.ex

35 lines
836 B
Elixir
Raw Normal View History

defmodule BalenaDevice.HTTP do
use HTTPoison.Base
alias BalenaDevice.Error
2019-10-20 12:24:11 +13:00
@moduledoc """
Uses `HTTPoison.Base` to construct requests for the on-device supervisor.
"""
@doc false
def process_request_url(url) do
"#{supervisor_address()}#{url}?apikey=#{supervisor_api_key()}"
end
2019-10-20 12:24:11 +13:00
@doc false
def process_request_headers(headers) do
headers
|> Enum.into(%{})
|> Map.put_new("content-type", "application/json")
|> Enum.into([])
end
defp supervisor_address, do: get_env_var("BALENA_SUPERVISOR_ADDRESS")
defp supervisor_api_key, do: get_env_var("BALENA_SUPERVISOR_API_KEY")
defp get_env_var(variable) do
case System.get_env(variable) do
var when is_binary(var) ->
var
_ ->
raise Error, message: "No `#{variable}` environment variable set"
end
end
end