Class: Datadog::Transport::HTTP::Builder
- Inherits:
-
Object
- Object
- Datadog::Transport::HTTP::Builder
- Defined in:
- lib/ddtrace/transport/http/builder.rb
Overview
Builds new instances of Transport::HTTP::Client
Defined Under Namespace
Classes: NoAdapterForApiError, NoApisError, NoDefaultApiError, UnknownAdapterError, UnknownApiError
Constant Summary collapse
Instance Attribute Summary collapse
-
#api_options ⇒ Object
readonly
Returns the value of attribute api_options.
-
#apis ⇒ Object
readonly
Returns the value of attribute apis.
-
#default_adapter ⇒ Object
readonly
Returns the value of attribute default_adapter.
-
#default_api ⇒ Object
Returns the value of attribute default_api.
-
#default_headers ⇒ Object
readonly
Returns the value of attribute default_headers.
Instance Method Summary collapse
- #adapter(type, *args) ⇒ Object
-
#api(key, spec, options = {}) ⇒ Object
Adds a new API to the client Valid options: - :adapter - :default - :fallback - :headers.
- #headers(values = {}) ⇒ Object
-
#initialize {|_self| ... } ⇒ Builder
constructor
A new instance of Builder.
- #to_api_instances ⇒ Object
- #to_client ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Builder
Returns a new instance of Builder.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ddtrace/transport/http/builder.rb', line 20 def initialize # Global settings @default_adapter = nil @default_headers = {} # Client settings @apis = API::Map.new @default_api = nil # API settings @api_options = {} yield(self) if block_given? end |
Instance Attribute Details
#api_options ⇒ Object (readonly)
Returns the value of attribute api_options.
13 14 15 |
# File 'lib/ddtrace/transport/http/builder.rb', line 13 def @api_options end |
#apis ⇒ Object (readonly)
Returns the value of attribute apis.
13 14 15 |
# File 'lib/ddtrace/transport/http/builder.rb', line 13 def apis @apis end |
#default_adapter ⇒ Object (readonly)
Returns the value of attribute default_adapter.
13 14 15 |
# File 'lib/ddtrace/transport/http/builder.rb', line 13 def default_adapter @default_adapter end |
#default_api ⇒ Object
Returns the value of attribute default_api.
13 14 15 |
# File 'lib/ddtrace/transport/http/builder.rb', line 13 def default_api @default_api end |
#default_headers ⇒ Object (readonly)
Returns the value of attribute default_headers.
13 14 15 |
# File 'lib/ddtrace/transport/http/builder.rb', line 13 def default_headers @default_headers end |
Instance Method Details
#adapter(type, *args) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/ddtrace/transport/http/builder.rb', line 35 def adapter(type, *args) @default_adapter = if type.is_a?(Symbol) registry_klass = REGISTRY.get(type) raise UnknownAdapterError, type if registry_klass.nil? registry_klass.new(*args) else type end end |
#api(key, spec, options = {}) ⇒ Object
Adds a new API to the client Valid options:
- :adapter
- :default
- :fallback
- :headers
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ddtrace/transport/http/builder.rb', line 55 def api(key, spec, = {}) = .dup # Copy spec into API map @apis[key] = spec # Apply as default API, if specified to do so. @default_api = key if .delete(:default) || @default_api.nil? # Save all other settings for initialization (@api_options[key] ||= {}).merge!() end |
#headers(values = {}) ⇒ Object
45 46 47 |
# File 'lib/ddtrace/transport/http/builder.rb', line 45 def headers(values = {}) @default_headers.merge!(values) end |
#to_api_instances ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ddtrace/transport/http/builder.rb', line 82 def to_api_instances raise NoApisError if @apis.empty? @apis.inject(API::Map.new) do |instances, (key, spec)| instances.tap do = @api_options[key].dup # Resolve the adapter to use for this API adapter = .delete(:adapter) || @default_adapter raise NoAdapterForApiError, key if adapter.nil? # Resolve fallback and merge headers fallback = .delete(:fallback) [:headers] = @default_headers.merge(([:headers] || {})) # Add API::Instance with all settings instances[key] = API::Instance.new( spec, adapter, ) # Configure fallback, if provided. instances.with_fallbacks(key => fallback) unless fallback.nil? end end end |
#to_client ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/ddtrace/transport/http/builder.rb', line 73 def to_client raise NoDefaultApiError if @default_api.nil? @client ||= Client.new( to_api_instances, @default_api ) end |