defmodule Mercury.Attachment do @moduledoc "A file attachment with a short-lived presigned download URL." @type t :: %__MODULE__{ id: String.t(), file_name: String.t(), url: String.t(), download_url: String.t(), created_at: DateTime.t() | nil, attachment_type: String.t() | nil } defstruct [:id, :file_name, :url, :download_url, :created_at, :attachment_type] @doc false def from_json(map) when is_map(map) do %__MODULE__{ id: map["id"], file_name: map["fileName"], url: map["url"], download_url: map["downloadUrl"], created_at: Mercury.Types.datetime(map["createdAt"]), attachment_type: map["attachmentType"] } end end defmodule Mercury.Attachments do @moduledoc "The Mercury Attachments API — file attachment metadata and download URLs." alias Mercury.{Attachment, Client, HTTP, Support} @doc """ Returns attachment metadata, including a short-lived presigned download URL. `GET /attachments/{id}` """ @spec get(client :: Client.t(), attachment_id :: String.t()) :: {:ok, Attachment.t()} | {:error, Exception.t()} def get(%Client{} = client, attachment_id) do with {:ok, body} <- HTTP.get(client, "attachments/#{attachment_id}") do {:ok, Attachment.from_json(body)} end end @spec get!(client :: Client.t(), attachment_id :: String.t()) :: Attachment.t() def get!(client, attachment_id), do: Support.bang!(get(client, attachment_id)) end