Skip to content

Plugins

Matt Muller edited this page Apr 10, 2024 · 7 revisions

Plugins are distributable classes that modify Config to add custom behavior.

Usage

Plugins are any class (or even a Proc) that responds to call(config). Plugins can be applied to the Client class, configured onto the instance of the Client, or passed as operation options.

Note: The NetworkInterceptor example is defined in Interceptors.

class NetworkPlugin
  def call(config)
    config.interceptors << NetworkInterceptor.new
  end
end
# => :call

# Add our plugin to a plugin list
plugins = Hearth::PluginList.new([NetworkPlugin.new])
# => #<Hearth::PluginList ... >

# Configure plugin onto the Client
client = HighScoreService::Client.new(
  endpoint: 'http://127.0.0.1:3000',
  plugins: plugins
)
# => #<HighScoreService::Client ... >

client.list_high_scores
# log request headers: { ... }
# log response headers: { ... }
# log request time is: ...
# => #<Hearth::Output @data=... >

Plugin ordering

The plugin ordering is as follows:

  1. Plugins registered to the Client class.
  2. Plugins registered to the instance of that Client.
  3. Plugins passed as options to operation calls.

Note: Unlike other configuration, Plugins passed as options to operation calls are additive and do not override other plugins configured on the Client.

# These are applied (called) first, if any.
HighScoreService::Client.plugins
# => #<Hearth::PluginList ... >
# Register a Client class plugin
HighScoreService::Client.plugins << SomeOtherPlugin.new
# => [ ... ]

# Then, these are applied (called), if any.
client.config.plugins
# => #<Hearth::PluginList ... >

# Finally, these are applied (called), if any
more_plugins = Hearth::PluginList.new([YetAnotherPlugin.new])
# => #<Hearth::PluginList ... >
client.list_high_scores({}, plugins: more_plugins)