Packages
lexdee
2.4.0
2.6.4
2.6.3
2.6.2
2.6.1
2.6.0
2.5.1
2.5.0
2.4.5
2.4.4
2.4.3
2.4.2
2.4.1
2.4.0
2.3.11
2.3.10
2.3.9
2.3.8
2.3.7
2.3.6
2.3.5
2.3.4
2.3.3
2.3.2
2.3.0
2.2.7
2.2.6
2.2.5
2.2.3
2.2.2
2.2.1
2.2.0
2.1.1
2.0.2
2.0.1
2.0.0
1.0.6
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
Client library for LXD
Current section
Files
Jump to
Current section
Files
lib/lexdee/client.ex
defmodule Lexdee.Client do
alias X509.{
Certificate,
PrivateKey
}
def new(base_url, cert \\ nil, key \\ nil, options \\ []) do
middleware = [
Lexdee.Response,
{Tesla.Middleware.BaseUrl, base_url},
Tesla.Middleware.JSON
]
adapter = get_adapter(cert, key, options)
Tesla.client(middleware, adapter)
end
defp build_key(key) do
key = PrivateKey.from_pem!(key)
type = elem(key, 0)
{type, PrivateKey.to_der(key)}
end
defp build_cert(cert) do
cert
|> Certificate.from_pem!()
|> Certificate.to_der()
end
defp get_adapter(cert, key, options) do
timeout = Keyword.get(options, :timeout, 30_000)
if Application.get_env(:lexdee, :environment) == :test do
{Tesla.Adapter.Mint, timeout: timeout}
else
{Tesla.Adapter.Mint,
timeout: timeout,
transport_opts: [
verify: :verify_none,
cert: build_cert(cert || File.read!(cert_path())),
key: build_key(key || File.read!(key_path()))
]}
end
end
defp cert_path,
do:
Application.get_env(:lexdee, :cert_path) ||
Path.expand("~/.config/lxc/client.crt")
defp key_path,
do:
Application.get_env(:lexdee, :key_path) ||
Path.expand("~/.config/lxc/client.key")
end