nova_plugin behaviour (NOVA vv0.8.0)

Plugins can be run at two different times; either in the beginning or at the end of a request. They can modify both the actual request or the nova-state. A plugin is implemented with the nova_plugin behaviour and needs to implement three different functions: pre_request/2, post_request/2 and plugin_info/0.

To register the plugin above you have to call nova_plugin:register_plugin(RequestType, http, example_plugin). in order to run it. RequestType can either be pre_request or post_request.

Link to this section Summary

Functions

Returns all registered plugins.
Get all plugins that is associated with a specific RequestType. Will return {ok, [{Priority :: integer(), Payload :: #{id => binary(), module => atom(), options => map()}]}.
Register a plugin. This operation is asyncronous so the atom ok will always be returned. If an error occurs it will be stated in the logs.
Unregisters a plugin with a given Id. The id can be retrieved by calling either get_all_plugins/0 or get_plugins/2 to find the specific plugin.

Link to this section Types

Link to this type

request_type/0

Specs

request_type() ::
    pre_http_request | post_http_request | pre_ws_upgrade | pre_ws_request | post_ws_request.

Link to this section Callbacks

Specs

plugin_info() ->
               {Title :: binary(),
                Version :: binary(),
                Author :: binary(),
                Description :: binary(),
                Options :: [{Key :: atom(), OptionDescription :: binary()}]}.
Link to this callback

post_http_request/2

(optional)

Specs

post_http_request(State :: nova:state(), Options :: map()) ->
                     {ok, State0 :: nova:state()} |
                     {break, State0 :: nova:state()} |
                     {stop, State0 :: nova:state()} |
                     {error, Reason :: term()}.
Link to this callback

post_ws_request/2

(optional)

Specs

post_ws_request({ControlCode :: reply | ok | stop,
                 Frame :: cow_ws:frame() | [cow_ws:frame()],
                 State :: nova_ws_handler:nova_ws_state()},
                Options :: map()) ->
                   {ok, State0 :: nova_ws_handler:nova_ws_state(), Options :: map()} |
                   {reply,
                    Frames :: cow_ws:frame() | [cow_ws:frame()],
                    State0 :: nova_ws_handler:nova_ws_state(),
                    Options :: map()} |
                   {break, State :: nova_ws_handler:nova_ws_state(), Options :: map()} |
                   {break,
                    OutFrame :: cow_ws:frame() | [cow_ws:frame()],
                    State :: nova_ws_handler:nova_ws_state(),
                    Options :: map()} |
                   {stop, State0 :: nova_ws_handler:nova_ws_state()} |
                   {error, Reason :: term()}.
Link to this callback

pre_http_request/2

(optional)

Specs

pre_http_request(State :: nova:state(), Options :: map()) ->
                    {ok, State0 :: nova:state()} |
                    {break, State0 :: nova:state()} |
                    {stop, State0 :: nova:state()} |
                    {error, Reason :: term()}.
Link to this callback

pre_ws_request/2

(optional)

Specs

pre_ws_request(State :: nova_ws_handler:nova_ws_state(), Options :: map()) ->
                  {ok, State :: nova_ws_handler:nova_ws_state()} |
                  {break, State :: nova_ws_handler:nova_ws_state()} |
                  {break,
                   OutFrame :: cow_ws:frame() | [cow_ws:frame()],
                   State :: nova_ws_handler:nova_ws_state()} |
                  {stop, State0 :: nova_ws_handler:nova_ws_state()} |
                  {error, Reason :: term()}.

Link to this section Functions

Link to this function

get_all_plugins()

Specs

get_all_plugins() -> {ok, map()}.
Returns all registered plugins.
Link to this function

get_plugins(RequestType)

Specs

get_plugins(RequestType :: request_type()) ->
               {ok,
                [{Priority :: integer(),
                  Payload :: #{id => integer(), module => atom(), options => map()}}]}.
Get all plugins that is associated with a specific RequestType. Will return {ok, [{Priority :: integer(), Payload :: #{id => binary(), module => atom(), options => map()}]}.
Link to this function

register_plugin(RequestType, Module)

Specs

register_plugin(RequestType :: request_type(), Module :: atom()) -> ok | {error, Reason :: atom()}.
Register a plugin. This operation is asyncronous so the atom ok will always be returned. If an error occurs it will be stated in the logs.
Link to this function

register_plugin(RequestType, Module, Options)

Specs

register_plugin(RequestType :: request_type(), Module :: atom(), Options :: map()) ->
                   ok | {error, Reason :: atom()}.
Link to this function

register_plugin(RequestType, Module, Options, Priority)

Specs

register_plugin(RequestType :: request_type(),
                Module :: atom(),
                Options :: map(),
                Priority :: integer()) ->
                   ok | {error, Reason :: atom()}.

Specs

start_link() ->
              {ok, Pid :: pid()} |
              {error, Error :: {already_started, pid()}} |
              {error, Error :: term()} |
              ignore.
Link to this function

unregister_plugin(Id)

Specs

unregister_plugin(Id :: binary()) -> ok.
Unregisters a plugin with a given Id. The id can be retrieved by calling either get_all_plugins/0 or get_plugins/2 to find the specific plugin.