backin

backin

I’m fighting with this code

  @moduledoc false
  require Logger

  def send_event_to_eventbridge() do
    event = %{
      entries: [
        %{
          source: "car-event",
          detailType: "car-created",
          detail: (%{
            brand: "Renault",
            name: "A4",
            country: "US"
          }),
          eventBusName: "my-event-bus"
        }
      ]
    }

    event_json = Poison.encode(event)

    Logger.info("Sending: #{inspect(event_json)}")
    
    case ExAws.EventBridge.put_events(event_json) |> ExAws.request() do
      {:ok, response} ->
        Logger.info("Successfully: #{inspect(response)}")
      {:error, error} ->
        Logger.error("Error: #{inspect(error)}")
    end
  end
end

and I don’t know how to fix this error

(Protocol.UndefinedError) protocol Enumerable not implemented for {:ok, “{"entries":[{"eventBusName":"my-event-bus","detail":{"country":"US","brand":"Renault","name":"A4"},"detailType":"car-created","source":"car-event"}]}”} of type Tuple. This protocol is implemented for the following type(s): DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, Jason.OrderedObject, List, Map, MapSet, Phoenix.LiveView.LiveStream, Postgrex.Stream, Range, Stream
(elixir 1.14.0) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir 1.14.0) lib/enum.ex:166: Enumerable.reduce/3
(elixir 1.14.0) lib/enum.ex:4307: Enum.map/2
(ex_aws_eventbridge 0.1.1) lib/ex_aws/event_bridge.ex:88: ExAws.EventBridge.put_events/2
(my_app 0.1.0) lib/my_app/eventbridge.ex:27: MyApp.Eventbridge.send_event_to_eventbridge/0

Showing Posts 1 to 2

msmithstubbs

msmithstubbs

This error message:

(Protocol.UndefinedError) protocol Enumerable not implemented for {:ok, “{"entries":[{"eventBusName":"my-event-bus","detail":{"country":"US","brand":"Renault","name":"A4"},"detailType":"car-created","source":"car-event"}]}”} of type Tuple. 

is telling you “I’m calling an Enumerable function on something, but it doesn’t support it because it is a tuple”

Where is the tuple coming from? Here:

  event_json = Poison.encode(event)`

Poison.encode returns a tuple. If you just want the encoded JSON, you can call Poison.encode!

iex> Poison.encode([1, 2, 3])
{:ok, "[1,2,3]"}

iex> Poison.encode!([1, 2, 3])
"[1,2,3]"
backin

backin OP

I was able to get it now, I’ll leave it here for other users.

defmodule MyApp.EventBridge do
  @moduledoc false
  require Logger
  alias ExAws.EventBridge

  def send_event_to_eventbridge(source, detail_type, detail, event_bus_name) do
    event = %{
      "Source": source,
      "DetailType": detail_type,
      "Detail":  Poison.encode!(detail),
      "EventBusName": event_bus_name
    }

    Logger.info("Sending: #{inspect(event)}" )

    case EventBridge.put_events([event]) |> ExAws.request() do
      {:ok, response} ->
        Logger.info("Event ID: #{inspect(response)}")

      {:error, error} ->
        Logger.error("Error #{inspect(error)}")
    end
  end
end

I insert the data through the console

MyApp.EventBridge.send_event_to_eventbridge(
   "car-event",
   "car-created",
   %{brand: "Renault", name: "A4", country: "US"},
   "my-event-bus"
  )

[info] Sending: %{Source: "car-event", DetailType: "car-created", Detail: "{\"country\":\"US\",\"brand\":\"Renault\",\"name\":\"A4\"}", EventBusName: "my-event-bus"}

[info] Event ID: %{"Entries" => [%{"EventId" => "e4accabf-b89d-0dd2-0fff-92e6de215a95"}], "FailedEntryCount" => 0}

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews