Snodo.Resource.Simple (snodo v0.2.0)

Copy Markdown View Source

Snodo.Resource with plain return values from read/2.

It accepts every Snodo.Resource option and produces the same definition and matcher. The difference is what read/2 may return:

  • {:ok, text} with a binary is one text content at the requested URI, carrying the declared :mime_type.
  • {:ok, value} with any other JSON value is one JSON content, with "application/json" unless another :mime_type is declared.
  • {:ok, %Snodo.Result{}} and {:error, reason} pass through unchanged. Use Snodo.Result.resource_read/2 for blobs, several contents, cache hints, or content metadata, and Snodo.Result.input_required/1 for MRTR.

JSON values must already use string keys; Snodo.JSONValue.encodable!/1 converts an atom-keyed domain value.

defmodule ToolboxGroups do
  use Snodo.Resource.Simple,
    uri: "toolbox://groups",
    name: "toolbox_groups",
    mime_type: "application/json"

  @impl true
  def read(_params, _context), do: {:ok, %{"groups" => ["web", "data"]}}
end

The conversion wraps the module's own read/2, so Snodo.Router receives an Snodo.Result exactly as it does from a plain Snodo.Resource.