Seven.Sync.ApiCommandRouter (Seven Otters v0.8.0) View Source

Make a syncronized command.

Example:

  defmodule MyApp.Command.Ping do
    alias __MODULE__
    require Logger

    use Seven.Sync.ApiCommandRouter,
      post: %{
        command: "Ping",
        pre_command: &Ping.pre_command/1,
        post_command: &Ping.post_command/2,
        wait_for_events: ["Pinged"]
      }

    def pre_command(%ApiRequest{} = _req) do
      Logger.debug("Ping.pre_command()")
      :ok
    end

    def post_command(%ApiRequest{} = _req, %Seven.Otters.Event{type: "Pinged"} = _event) do
      Logger.debug("Ping.post_command(): pinged event received.")
      %{ping: "ok"}
    end
  end

post map can contains:

  • command: the command to send
  • pre_command: a function that will be run after to send the command, expressed in the form of func_name(%ApiRequest{})
  • post_command: a function that will be run before to send the command, expressed in the form of func_name(%ApiRequest{}, %Seven.Otters.Event{})
  • wait_for_events: list of events to wait for; this macro waits for all indicated events that are raised from the same request generated by the command above.

Usage:

  iex> MyApp.Command.Ping.run(params)