ha1973
AEAsh Elixir
•Created by ha1973 on 9/8/2023 in #support
Use without data layers
Hi,
I'm trying to fit the example from https://hexdocs.pm/ash/use-without-data-layers.html to call a web api,
defmodule Asher.Edge.FetchMyip do
use Ash.Resource.Preparation
def prepare(query, _, _) do
case Req.get("https://httpbin.org/ip") do
{:ok,
%Req.Response{
status: 200,
body: data
}} ->
Ash.DataLayer.Simple.set_data(query, data)
{:error, error} ->
Ash.Query.add_error(query, "doh #{inspect(error)}")
end
end
end
when trying to read i get
iex(21)> Ash.Query.for_read(Asher.Edge.Myip, :read) |> Asher.Edge.read!()
** (Ash.Error.Unknown) Unknown Error
Context: resolving data on fetch Asher.Edge.Myip.read
* Context: resolving data on fetch Asher.Edge.Myip.read
** (KeyError) key :__metadata__ not found in: %{"origin" => "80.208.68.43"}
(ash 2.14.12) lib/ash/resource.ex:267: Ash.Resource.set_metadata/2
.....
Anyone who can point me to a working example on how to consume a web api ?10 replies