Class: ConfigurationService::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/configuration_service/client.rb

Overview

The configuration service API

See README for a summary of the service, including links to user stories.

It is recommended that consumers use a Factory to create and configure the service.

Instance Method Summary collapse

Constructor Details

#initialize(credentials: nil, provider: nil, identifier: nil) ⇒ Client

Creates a new instance of configuration service client

Raises:

Parameters:

  • (defaults to: nil)

    opaque string representing authority to access the configuration data and associated metadata. Interpretation of the credentials is provider-dependent; it is opaque to the API and client.

  • (defaults to: nil)

    a configured service provider instance, to which requests will be delegated.

  • (defaults to: nil)

    unique configuration identifier (optional). If omitted, instance methods will require the identifier argument.



25
26
27
28
29
30
31
# File 'lib/configuration_service/client.rb', line 25

def initialize(credentials: nil, provider: nil, identifier: nil)
  #raise(ArgumentError, "missing argument credentials") unless credentials
  raise(ArgumentError, "missing argument provider") unless provider
  @credentials = credentials 
  @provider = provider
  @identifier = identifier
end

Instance Method Details

#authorize_consumption(identifier: nil) ⇒ String

Authorize consumption of a configuration ie. get credentials to consume configurations ie. get a token to consume configurations

Raises:

  • if no configuration was found for the configured identifier

  • if no identifier is given as an argument or supplied in the initialized context.

  • if the request failed

Parameters:

  • (defaults to: nil)

    unique identifier of configuration. Required if the instance was initialized with a context that provided no identifier.

Returns:

  • credentials allowing consumption of the provided configuration



93
94
95
96
97
# File 'lib/configuration_service/client.rb', line 93

def authorize_consumption(identifier: nil)
  identifier = identifier_argument_or_instance_variable(identifier)
  @provider.authorize_consumption(identifier, @credentials) or
    raise ConfigurationNotFoundError, "configuration not found for identifier: #{identifier}"
end

#publish_configuration(identifier: nil, data: nil, metadata: {}) ⇒ ConfigurationService::Configuration

Publishes configuration data and metadata

The metadata is decorated with the following keys:

  • “timestamp” - the current UTC time in ISO8601 format

  • “revision” - a UUID for this publication

Delegates the request to the configured provider. The provider may further decorate the metadata.

It is recommended that both the data and metadata dictionaries use strings as keys, and that values be limited to those that can be serialized to JSON.

Raises:

  • if no identifier is given as an argument or supplied in the initialized context.

  • if publication failed

Parameters:

  • (defaults to: nil)

    unique identifier of configuration. Required if the instance was initialized with a context that provided no identifier.

  • (defaults to: nil)

    dictionary of probably sensitive configuration data intended for an application, which providers are expected to secure

  • (defaults to: {})

    dictionary of data about the configuration data, which providers are not expected to secure

Returns:

  • object containing the configuration data, decorated metadata and identifier



73
74
75
76
77
78
79
80
81
# File 'lib/configuration_service/client.rb', line 73

def publish_configuration(identifier: nil, data: nil, metadata: {})
  identifier = identifier_argument_or_instance_variable(identifier)
  Utils.dictionary?(data) or raise ConfigurationService::Error, "data must be a dictionary"
  Utils.dictionary?() or raise ConfigurationService::Error, "metadata must be a dictionary"

   = Utils.decorate()
  configuration = Configuration.new(identifier, data, )
  @provider.publish_configuration(configuration, @credentials)
end

#request_configuration(identifier: nil) ⇒ ConfigurationService::Configuration

Requests the configuration data and metadata Delegates the request to the configured provider.

Raises:

  • if no configuration was found for the identifier

  • if no identifier is given as an argument or supplied in the initialized context.

  • if the request failed

Parameters:

  • (defaults to: nil)

    unique identifier of configuration. Required if the instance was initialized with a context that provided no identifier.

Returns:

  • object containing the configuration data, metadata and identifier



44
45
46
47
48
# File 'lib/configuration_service/client.rb', line 44

def request_configuration(identifier: nil)
  identifier = identifier_argument_or_instance_variable(identifier)
  @provider.request_configuration(identifier, @credentials) or 
    raise ConfigurationNotFoundError, "configuration not found for identifier: #{identifier}"
end