PhoenixGenApi.Structs.FunConfig (PhoenixGenApi v0.0.13)

View Source

For declare a general config for a function.

Summary

Based on function config, the request will be forwarded to the target service. Params will be validated & converted to the correct types before forwarding. The response will be handled based on the response type.

For basic check permission, it will check if the user_id of the request is the same as the user_id in the argument (indicated by check_permission, ex: check_permission: {:arg, arg_name}).

For advanced permission check, please pass the request_info to the target function for checking.

Example

Below is an example of a function config:

%FunConfig{
  request_type: "get_user",
  service: :user,
  nodes: ["user1", "user2"],
  choose_node_mode: :random,
  timeout: 1000,
  mfa: {User, :get_user, []},
  arg_types: %{
    "user_id" => :string,
    "device_id" => :string,
  },
  arg_orders: ["user_id", "device_id"],
  response_type: :async,
  check_permission: false,
  request_info: true,
}

Explain:

  • request_type: the unique identifier for the type of request & response. This is unqiue name in system for cliet can call right function.

  • service: the service that will handle the request.

  • nodes: the nodes that will handle the request. You can choose local node by set to :local. Currently, all nodes must have the same config.

  • choose_node_mode: the way to chose node, support: :random, :hash, :round_robin.

  • timeout: the timeout for the request.

  • mfa: the module, function, and arguments that will be called to handle the request.

  • arg_types: the types of the arguments that will be passed to the function. For validation & converting.

  • arg_orders: the order of the arguments that will be passed to the function.

  • response_type: indicates if the request has a response. Type of response: :sync, :async, :stream, :none.

  • check_permission: check permission, false or {:arg, arg_name}.

  • request_info: indicates if need request info, info will be added to the request in the last argument. %{request_id: request_id, user_id: user_id, device_id: device_id}. user_id is the user_id of the user who made the request.

Summary

Functions

Check permission for request from client.

Validate & Convert request arguments to the correct types.

Select target based on config.

Check if the service is local (run on the same node).

Validate request arguments.

Types

t()

@type t() :: %PhoenixGenApi.Structs.FunConfig{
  arg_orders: [String.t()],
  arg_types: map() | nil,
  check_permission: false | {:arg, String.t()},
  choose_node_mode: atom(),
  mfa: {module(), function(), args :: list()},
  nodes: [String.t()] | {module(), function(), args :: list()},
  request_info: boolean(),
  request_type: String.t(),
  response_type: :sync | :async | :stream | :none,
  service: atom() | String.t(),
  timeout: integer() | :infinity
}

Functions

check_permission(request, fun_config)

Check permission for request from client.

check_permission!(request, fun_config)

convert_args!(config, request)

Validate & Convert request arguments to the correct types.

get_node(config, request)

Select target based on config.

is_local_service?(config)

Check if the service is local (run on the same node).

validate_args!(config, request)

Validate request arguments.