aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dns.ex
blob: d5dbcec56d923b3745b1e00efc0f4e7413e66120 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
defmodule Lithium.DNS do
  @moduledoc """
  DNS query module.

  Some query magic is performed to do things like joining TXT records together.
  """

  @spec fetch_txt(String.t()) :: {:ok, [String.t()]} | {:error, any()}
  def fetch_txt(name) do
    case :inet_res.getbyname(String.to_charlist(name), :txt) do
      {:ok, {:hostent, _domain, _aliases, :txt, _length, records}} ->
        records
        |> Enum.map(&List.to_string/1)
        |> then(&{:ok, &1})

      {:error, reason} ->
        {:error, reason}
    end
  end
end