retort v2.7.0 Retort.Resources behaviour View Source
Default callbacks for Calcinator.Resources behaviour when backed by Retort.Client.Generic
Link to this section Summary
Types
Options passed to client_start_link that should be combined with resource-specific options controlled by the module
implementing this behaviour
Functions
use Retort.Resources implements Calcinator.Resources callbacks to use Retort.Client.Generic
Constructs Ecto.Changeset.t using module changeset/2 (which is the implementatin of the
Calcinator.Resources.changeset/2 callback) with module ecto_schema_module/0 __struct__/0 as the initial data
Destroys changeset data using Retort.Client.Generic.destroy/4
Gets struct with id using Retort.Client.Generic.show/4
Inserts params using Retort.Client.Generic.create/4
Lists resources matching the query_options using Retort.Client.Generic.index/3
module specific sandbox token key
Applies updates in changeset by converting them back to params for Retort.Client.Generic.update/5
Updates data with params using Retort.Client.Generic.update/5
Callbacks
Converts the associations passed from Calcinator to the full set of includes passed to Retort.Client.Generic
Call Retort.Client.Generic.start_link and return its result
Used to generate changesets and translate error messages
Add additional associations to the list passed down from Calcinator
Timeout for function call on Retort.Client.Generic
Link to this section Types
client_start_link_options() :: Keyword.t()
Options passed to client_start_link that should be combined with resource-specific options controlled by the module
implementing this behaviour.
:meta- ContainsCalcinator.Meta.Beamsandbox token. Will not be set ifCalcinator.Resources.sandboxed?/0isfalseorCalcinator.Resources.allow_sandbox_access/1has not been called in the current process.
Link to this section Functions
use Retort.Resources implements Calcinator.Resources callbacks to use Retort.Client.Generic.
defmodule MyRPC.Authors do
alias MyRPC.{Author, Client}
alias MyLocal.Repo
use Retort.Resources
# Functions
## Calcinator.Resources callbacks
def sandboxed?, do: Repo.config()[:pool] == Ecto.Adapters.SQL.Sandbox
## Retort.Resources callbacks
def association_to_include(:posts), do: "posts"
def client_start_link(options), do: Client.Author.start_link(options)
def ecto_schema_module, do: Author
end
Callbacks
The caller must implement the following callbacks.
Retort.Resources callbacks
Retort.Resources.association_to_include/1Retort.Resources.client_start_link/1Retort.Resources.ecto_schema_module/0
Calcinator.Resources callback
use Retort.Resources doesn’t implement all the required Calcinator.Resources.
Calcinator.Resources.changeset/2Calcinator.Resources.sandboxed?/0- If allRetort.Client.Genericcalls never hit the projects owned database, this can returnfalse, but if there is a chance of theRetort.Client.Genericcalls may trigger additional RPC calls that come back to the project, you may need to check if the owned database is sandboxed withRepo.config()[:pool] == Ecto.Adapters.SQL.Sandbox
Overridable Functions
The following functions defined by use Retort.Resources are marked as overridable. You can use super/0, but you
may also want to call the original implementation directly.
| Function | Callee |
|---|---|
allow_sandbox_access/1 | Retort.Resources.allow_sandbox_access/2 |
delete/2 | Retort.Resources.delete/3 |
full_associations/1 | Inlined as def full_associations(list) when is_list(list), do: list |
get/2 | Retort.Resources.get/2 |
insert/2 | Retort.Resources.insert/3 |
list/1 | Retort.Resources.list/2 |
timeout/1 | Retort.Resources.timeout/2 |
update/2 | Retort.Resources.update/3 |
update/3 | Retort.Resources.update/4 |
Timeouts
Timeouts for a module that use Retort.Resources can be configured using Retort.Resources.Timeout
allow_sandbox_access(module(), Calcinator.Resources.sandbox_access_token()) :: :ok
changeset(module(), Calcinator.Resources.params()) :: {:ok, Ecto.Changeset.t()} | {:error, :ownership}
Constructs Ecto.Changeset.t using module changeset/2 (which is the implementatin of the
Calcinator.Resources.changeset/2 callback) with module ecto_schema_module/0 __struct__/0 as the initial data.
Parameters
module- The module that calleduse Retort.Resources. MUST implementCalcinator.Resources.changeset/2andRetort.Resources.ecto_schema_module/0.params- params passed throughCalcinator.create/2.
client_start_link_options(module(), Calcinator.Resources.query_options()) :: Keyword.t()
delete(module(), Ecto.Changeset.t(), Calcinator.Resources.query_options()) :: :ok | {:error, :bad_gateway} | {:error, :not_found} | {:error, :sandbox_access_disallowed} | {:error, :timeout} | {:error, Ecto.Changeset.t()}
Destroys changeset data using Retort.Client.Generic.destroy/4.
Parameters
module- The module that calleduse Retort.Resources. MUST implementRetort.Resources.client_start_link/0andRetort.Resources.timeout/1.data- struct whoseidshould be destroyed.query_options- to supply:metafor client.
Returns
:ok- the struct in thechangesetwas deleted{:error, :bad_gateway}- A 500 Internal Server error from the remote server. Check its log.{:error, :not_found}- The id of the struct in the changeset was not found on the remote server. It is either (1) already deleted or (2) never existed.{:error, :sandbox_access_disallowed}- Thequery_options:meta,"beam"does not have the correct information to access the testing sandbox. Ensure you don’t have stale requests in your RabbitMQ queues that have"beam"from previous test runs.{:error, :timeout}- theRetort.Client.Generic.destroy/3call timed out. The struct in thechangesetmay have still been deleted if the request completes on the remote server after the timeout.To increase the timeout, increase
module.timeout(:destroy), which (ifmoduledoes not overridetimeout/1fromuse Retort.Resourcescan be changed in configconfig :retort, module, timeout: [ destroy: destroy_timeout # milliseconds ]… or it can be changed at runtime
Retort.Resources.Timeout.put(module, :destroy, destroy_timeout){:error, Ecto.Changeset.t}- validation errors that preventedchangesetfrom being deleted. For deletes, this is usually foreign key constraints, such as fromEcto.Changeset.no_assoc_constraint/2.
get(module(), Calcinator.Resources.id(), Calcinator.Resources.query_options()) :: {:ok, Ecto.Schema.t()} | {:error, :bad_gateway} | {:error, :not_found} | {:error, :sandbox_access_disallowed} | {:error, :timeout}
Gets struct with id using Retort.Client.Generic.show/4
Parameters
module- The module that calleduse Retort.Resources. MUST implementRetort.Resources.client_start_link/0andRetort.Resources.timeout/1.idtheidof the struct to getquery_options- associations and filters
Returns
:ok- the struct in thechangesetwas deleted{:error, :bad_gateway}- A 500 Internal Server error from the remote server. Check its log.{:error, :not_found}- Theidwas not found on the remote server.{:error, :sandbox_access_disallowed}- Thequery_options:meta,"beam"does not have the correct information to access the testing sandbox. Ensure you don’t have stale requests in your RabbitMQ queues that have"beam"from previous test runs.{:error, :timeout}- theRetort.Client.Generic.get/4call timed out. Theidmay still be retrieved from the remote server’s backing store if it completes on the remote server after the timeout, but it will not be returned to the caller because the RabbitMQ reply queue will not exist.To increase the timeout, increase
module.timeout(:get), which (ifmoduledoes not overridetimeout/1fromuse Retort.Resourcescan be changed in configconfig :retort, module, timeout: [ get: get_timeout # milliseconds ]… or it can be changed at runtime
Retort.Resources.Timeout.put(module, :get, get_timeout)
insert(module(), Ecto.Changeset.t() | Calcinator.Resources.params(), Calcinator.Resources.query_options()) :: {:ok, Ecto.Schema.t()} | {:error, :bad_gateway} | {:error, :sandbox_access_disallowed} | {:error, :timeout} | {:error, Ecto.Changeset.t()}
Inserts params using Retort.Client.Generic.create/4
Parameters
module- The module that calleduse Retort.Resources. MUST implementCalcinator.Resources.changeset/1and theinsert(changeset, query_options)form ofCalcinator.Resources.insert/2.params- params passed toCalcinator.create/2.query_options- associations to preload in the created struct
Returns
{:ok, struct}- theparamswas inserted yieldingstruct{:error, :bad_gateway}- A 500 Internal Server error from the remote server. Check its log.{:error, :sandbox_access_disallowed}- Thequery_options:meta,"beam"does not have the correct information to access the testing sandbox. Ensure you don’t have stale requests in your RabbitMQ queues that have"beam"from previous test runs.{:error, :timeout}- theRetort.Client.Generic.create/4call timed out. Theparamsmay have still been inserted if the request completes on the remote server after the timeout.To increase the timeout, increase
module.timeout(:create), which (ifmoduledoes not overridetimeout/1fromuse Retort.Resourcescan be changed in configconfig :retort, module, timeout: [ create: create_timeout # milliseconds ]… or it can be changed at runtime
Retort.Resources.Timeout.put(module, :create, create_timeout){:error, Ecto.Changeset.t}- validation errors that preventedparamsfrom being inserted.
list(module(), Calcinator.Resources.query_options()) :: {:ok, [Ecto.Schema.t()], Alembic.Pagination.t() | nil} | Retort.Client.Generic.error()
Lists resources matching the query_options using Retort.Client.Generic.index/3.
Parameters
module- The module that calleduse Retort.Resources. MUST implementRetort.Resources.client_start_link/0andRetort.Resources.timeout/1.query_options- association to preload, filters, pagination, and sorting.
Returns
{:ok, [Ecto.Schema.t], Alembic.Pagination.t | nil}- thestructsmatching thequery_optionsand (optionally) the{:error, :bad_gateway}- A 500 Internal Server error from the remote server. Check its log.{:error, :not_found}- Theidwas not found on the remote server.{:error, :sandbox_access_disallowed}- Thequery_options:meta,"beam"does not have the correct information to access the testing sandbox. Ensure you don’t have stale requests in your RabbitMQ queues that have"beam"from previous test runs.{:error, :timeout}- theRetort.Client.Generic.index/3call timed out. Theidmay still be retrieved from the remote server’s backing store if it completes on the remote server after the timeout, but it will not be returned to the caller because the RabbitMQ reply queue will not exist.To increase the timeout, increase
module.timeout(:index), which (ifmoduledoes not overridetimeout/1fromuse Retort.Resourcescan be changed in configconfig :retort, module, timeout: [ index: index_timeout # milliseconds ]… or it can be changed at runtime
Retort.Resources.Timeout.put(module, :index, index_timeout)
module specific sandbox token key
update(module(), Ecto.Changeset.t(), Calcinator.Resources.query_options()) :: {:ok, Ecto.Schema.t()} | {:error, :bad_gateway} | {:error, :sandbox_access_disallowed} | {:error, :timeout} | {:error, Ecto.Changeset.t()}
Applies updates in changeset by converting them back to params for Retort.Client.Generic.update/5.
Parameters
module- The module that calleduse Retort.Resources. MUST implementRetort.Resources.client_start_link/0andRetort.Resources.timeout/1.changeset- An update changesetquery_options- associations to preload in the returned updated struct.
Returns
{:ok, struct}- thechangesetwas updated yieldingstruct{:error, :bad_gateway}- A 500 Internal Server error from the remote server. Check its log.{:error, :sandbox_access_disallowed}- Thequery_options:meta,"beam"does not have the correct information to access the testing sandbox. Ensure you don’t have stale requests in your RabbitMQ queues that have"beam"from previous test runs.{:error, :timeout}- theRetort.Client.Generic.update/5call timed out. The struct in thechangesetmay have still been updated if the request completes on the remote server after the timeout.To increase the timeout, increase
module.timeout(:update), which (ifmoduledoes not overridetimeout/1fromuse Retort.Resourcescan be changed in configconfig :retort, module, timeout: [ update: update_timeout # milliseconds ]… or it can be changed at runtime
Retort.Resources.Timeout.put(module, :update, update_timeout){:error, Ecto.Changeset.t}- validation errors that preventedchangesetfrom being updated.
update(module(), Ecto.Schema.t(), Calcinator.Resources.params(), Calcinator.Resources.query_options()) :: {:ok, Ecto.Schema.t()} | {:error, :bad_gateway} | {:error, :sandbox_access_disallowed} | {:error, :timeout} | {:error, Ecto.Changeset.t()}
Updates data with params using Retort.Client.Generic.update/5.
Parameters
module- The module that calleduse Retort.Resources. MUST implementCalcinator.Resources.changeset/2andCalcinator.Resources.update/2.data- struct retrieved withmoduleget/2.params- params passed throughCalcinator.update/2query_options- associations to preload in the returned updated struct.
Returns
{:ok, struct}- thedatawas updated withparamsyieldingstruct{:error, :bad_gateway}- A 500 Internal Server error from the remote server. Check its log.{:error, :sandbox_access_disallowed}- Thequery_options:meta,"beam"does not have the correct information to access the testing sandbox. Ensure you don’t have stale requests in your RabbitMQ queues that have"beam"from previous test runs.{:error, :timeout}- theRetort.Client.Generic.update/5call timed out. Thedatamay have still been updated withparamsif the request completes on the remote server after the timeout.To increase the timeout, increase
module.timeout(:update), which (ifmoduledoes not overridetimeout/1fromuse Retort.Resourcescan be changed in configconfig :retort, module, timeout: [ update: update_timeout # milliseconds ]… or it can be changed at runtime
Retort.Resources.Timeout.put(module, :update, update_timeout){:error, Ecto.Changeset.t}- validation errors that preventedparamsfrom updatingdataupdated.
Link to this section Callbacks
Converts the associations passed from Calcinator to the full set of includes passed to Retort.Client.Generic.
- Translate between the local association name and the remote include name
- Check that the association is allowed
client_start_link(client_start_link_options()) :: GenServer.on_start()
Call Retort.Client.Generic.start_link and return its result.
Used to generate changesets and translate error messages.
Add additional associations to the list passed down from Calcinator.
- Add default includes
- Add includes that are needed for authorization that the caller doesn’t know are necessary.
Timeout for function call on Retort.Client.Generic.