Skip to content
Matt Muller edited this page Apr 10, 2024 · 10 revisions

The Config class is an immutable struct that contains the options that will be used by the Client. It can resolve default value chains and validates provided values against expected types. Config classes have a 1:1 mapping with a Smithy service shape.

@railsJson
@title("High Score Sample Rails Service")
service HighScoreService {
    version: "2021-02-15",
    resources: [HighScore]
}

Usage

A Config object is created implicitly when creating a Client. Config has default values which can be sourced from multiple locations. Config will also validate that the provided values are of expected types. For example, :endpoint is validated to be a String.

client = HighScoreService::Client.new(endpoint: 'http://127.0.0.1:3000')
# => #<HighScoreService::Client ... >

client.config
# => #<struct HighScoreService::Config ... >

Config is immutable after validation, but it may contain "refreshed" values such as a new Identity.

client.config.endpoint = 'http://127.0.0.1:8080
# => Cannot modify frozen HighScoreService::Config

Plugins

All Plugins have access to the Config instance and can modify values.

# Plugin (callable) that changes the configured endpoint
plugin = proc { |config| config.endpoint = 'http://127.0.0.1:8080' }
# => #<Proc ... >

client = HighScoreService.new(
  endpoint: '127.0.0.1:3000',
  plugins: Hearth::PluginList.new([plugin])
)
# => #<HighScoreService::Client ... >

client.list_high_scores
# <uses endpoint from plugin>
# => #<Hearth::Output @data=... >