Current section
Files
Jump to
Current section
Files
lib/retort/client/request.ex
defmodule Retort.Client.Request do
@moduledoc """
Metadata about a request that is in-flight for `Retort.Client.Generic`
"""
alias Alembic.{FromJson, Meta}
# Struct
defstruct [:from, :method]
# Types
@typedoc """
The methods supported by the JSON API over JSON RPC servers
"""
# Should match elements of `CarrotRpc::RpcServer::JSONAPIResources::Actions::NAME_SET`
@type method :: :create |
:create_relationship |
:destroy |
:destroy_relationship |
:get_related_resource |
:index |
:show |
:show_relationship |
:update |
:update_relationship
@typedoc """
Information needed to send the response from the server by the Elixir process.
* `from` the `Genserver` call identifier to reply to the ELixir client process
* `method` - the method of the call. Used to generate `Alembic.Meta.t` (using `to_meta/1`) that
controls the parsing of the server response.
"""
@type t :: %__MODULE__{
from: {pid, reference},
method: method
}
# Functions
@doc """
Translates the `method` to the corresponding `Alembic.FromJson.action`
"""
@spec method_to_action(method) :: FromJson.action
def method_to_action(method) when method in ~W(create create_relationship)a, do: :create
def method_to_action(method) when method in ~W(destroy destroy_relationship)a, do: :delete
def method_to_action(method) when method in ~W(get_related_resource index show show_relationship)a, do: :fetch
def method_to_action(method) when method in ~W(update update_relationship)a, do: :update
@doc """
Converts to `Alembic.Meta.t` with `"action"` and `"sender"` set to work with
`Alembic.FromJson.from_json/2`'s error template.
`"sender"` is always set to `:server` since parsing responses from this request will by definition come from a server.
"""
@spec to_meta(t) :: Meta.t
def to_meta(%__MODULE__{method: method}), do: %{"action" => method_to_action(method), "sender" => :server}
end