Lux.Lens behaviour (Lux v0.5.0)

View Source

Lenses are used to load data from a source and return it to the calling agent.

Example

defmodule MyApp.Lenses.WeatherLens do
  use Lux.Lens,
      name: "OpenWeather API",
      description: "Fetches weather data from OpenWeather",
      url: "https://api.openweathermap.org/data/2.5/weather",
      method: :get,
      schema: %{
        type: :object,
        properties: %{
          q: %{
            type: :string,
            description: "City name"
          },
          units: %{
            type: :string,
            description: "Temperature units. For temperature in Fahrenheit use units=imperial and for temperature in Celsius use units=metric",
            enum: ["metric", "imperial"]
          },
          appid: %{type: :string, description: "API key"}
        },
        required: ["q", "appid"]
      }

  # Optional: Define a custom after_focus function
  def after_focus(%{"main" => %{"temp" => temp}} = body) do
    {:ok, %{temperature: temp, raw_data: body}}
  end

  def after_focus(%{"error" => error}) do
    {:error, error}
  end
end

Summary

Types

An optional type of a type t is a type that can be either the type t or nil.

t()

Types

nullable(t)

@type nullable(t) :: t | nil

An optional type of a type t is a type that can be either the type t or nil.

t()

@type t() :: %Lux.Lens{
  after_focus: (any() -> any()),
  auth: map(),
  description: String.t(),
  headers: list(),
  method: atom(),
  name: String.t(),
  params: map(),
  schema: map(),
  url: String.t()
}

Callbacks

after_focus(response)

(optional)
@callback after_focus(response :: any()) :: {:ok, any()} | {:error, any()}

Functions

authenticate(lens)

focus(lens, opts \\ [])

new(attrs)