Class: Datadog::Core::Remote::Transport::HTTP::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/remote/transport/http/builder.rb

Overview

Builds new instances of Transport::HTTP::Client

Defined Under Namespace

Classes: NoAdapterForApiError, NoApisError, NoDefaultApiError, UnknownAdapterError, UnknownApiError

Constant Summary collapse

REGISTRY =
Datadog::Core::Transport::HTTP::Adapters::Registry.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Builder

Returns a new instance of Builder.

Yields:

  • (_self)

Yield Parameters:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 57

def initialize
  # Global settings
  @default_adapter = nil
  @default_headers = {}

  # Client settings
  @apis = Datadog::Core::Transport::HTTP::API::Map.new
  @default_api = nil

  # API settings
  @api_options = {}

  yield(self) if block_given?
end

Instance Attribute Details

#api_optionsObject (readonly)

Returns the value of attribute api_options.



50
51
52
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 50

def api_options
  @api_options
end

#apisObject (readonly)

Returns the value of attribute apis.



50
51
52
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 50

def apis
  @apis
end

#default_adapterObject (readonly)

Returns the value of attribute default_adapter.



50
51
52
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 50

def default_adapter
  @default_adapter
end

#default_apiObject

Returns the value of attribute default_api.



50
51
52
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 50

def default_api
  @default_api
end

#default_headersObject (readonly)

Returns the value of attribute default_headers.



50
51
52
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 50

def default_headers
  @default_headers
end

Instance Method Details

#adapter(config, *args, **kwargs) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 72

def adapter(config, *args, **kwargs)
  @default_adapter = case config
                     when Core::Configuration::AgentSettingsResolver::AgentSettings
                       registry_klass = REGISTRY.get(config.adapter)
                       raise UnknownAdapterError, config.adapter if registry_klass.nil?

                       registry_klass.build(config)
                     when Symbol
                       registry_klass = REGISTRY.get(config)
                       raise UnknownAdapterError, config if registry_klass.nil?

                       registry_klass.new(*args, **kwargs)
                     else
                       config
                     end
end

#api(key, spec, options = {}) ⇒ Object

Adds a new API to the client Valid options:

- :adapter
- :default
- :fallback
- :headers


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 99

def api(key, spec, options = {})
  options = options.dup

  # Copy spec into API map
  @apis[key] = spec

  # Apply as default API, if specified to do so.
  @default_api = key if options.delete(:default) || @default_api.nil?

  # Save all other settings for initialization
  (@api_options[key] ||= {}).merge!(options)
end

#api_instance_classObject



152
153
154
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 152

def api_instance_class
  API::Instance
end

#headers(values = {}) ⇒ Object



89
90
91
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 89

def headers(values = {})
  @default_headers.merge!(values)
end

#to_api_instancesObject

Raises:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 124

def to_api_instances
  raise NoApisError if @apis.empty?

  @apis.inject(Datadog::Core::Transport::HTTP::API::Map.new) do |instances, (key, spec)|
    instances.tap do
      api_options = @api_options[key].dup

      # Resolve the adapter to use for this API
      adapter = api_options.delete(:adapter) || @default_adapter
      raise NoAdapterForApiError, key if adapter.nil?

      # Resolve fallback and merge headers
      fallback = api_options.delete(:fallback)
      api_options[:headers] = @default_headers.merge((api_options[:headers] || {}))

      # Add API::Instance with all settings
      instances[key] = api_instance_class.new(
        spec,
        adapter,
        api_options
      )

      # Configure fallback, if provided.
      instances.with_fallbacks(key => fallback) unless fallback.nil?
    end
  end
end

#to_transport(klass) ⇒ Object

Raises:



118
119
120
121
122
# File 'lib/datadog/core/remote/transport/http/builder.rb', line 118

def to_transport(klass)
  raise NoDefaultApiError if @default_api.nil?

  klass.new(to_api_instances, @default_api)
end