Class: Common::Client::Configuration::Base

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/common/client/configuration/base.rb

Overview

Base class for HTTP service configurations. This class should not be used directly. Use REST or SOAP instead.

Direct Known Subclasses

REST, SOAP

Class Attributes collapse

Class Attributes collapse

Instance Attribute Details

#base_request_headersObject

headers to include in all requests



39
# File 'lib/common/client/configuration/base.rb', line 39

class_attribute :base_request_headers

#open_timeoutObject

rubocop:disable ThreadSafety/ClassAndModuleAttributes These attributes are inherited by subclasses so that they can change their own value without impacting the parent class timeout for opening the connection



19
# File 'lib/common/client/configuration/base.rb', line 19

class_attribute :open_timeout

#read_timeoutObject

timeout for reading a response



24
# File 'lib/common/client/configuration/base.rb', line 24

class_attribute :read_timeout

#request_typesObject

allowed requests types e.g. ‘%i[get put post delete].freeze`



29
# File 'lib/common/client/configuration/base.rb', line 29

class_attribute :request_types

#user_agentObject

value for the User-Agent header



34
# File 'lib/common/client/configuration/base.rb', line 34

class_attribute :user_agent

Instance Method Details

#base_pathObject

The base path to use for all requests. Implemented by sub classes.

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/common/client/configuration/base.rb', line 55

def base_path
  raise NotImplementedError, "Subclass #{self.class.name} of Configuration must implement base_path"
end

#breakers_error_thresholdInteger

The percentage of errors over which an outage will be reported as part of breakers gem

Returns:

  • (Integer)

    corresponding to percentage



137
138
139
# File 'lib/common/client/configuration/base.rb', line 137

def breakers_error_threshold
  50
end

#breakers_exception_handlerObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/common/client/configuration/base.rb', line 110

def breakers_exception_handler
  proc do |exception|
    case exception
    when Common::Exceptions::BackendServiceException
      (500..599).cover?(exception.response_values[:status])
    when Common::Client::Errors::HTTPError
      (500..599).cover?(exception.status)
    when Faraday::ServerError
      (500..599).cover?(exception.response&.[](:status))
    else
      false
    end
  end
end

#breakers_matcherObject



102
103
104
105
106
107
108
# File 'lib/common/client/configuration/base.rb', line 102

def breakers_matcher
  base_uri = URI.parse(base_path)
  proc do |request_env|
    request_env.url.host == base_uri.host && request_env.url.port == base_uri.port &&
      request_env.url.path =~ /^#{base_uri.path}/
  end
end

#breakers_serviceObject

Default request options, sets the read and open timeouts.

Returns:

  • Hash default request options.



96
97
98
99
100
# File 'lib/common/client/configuration/base.rb', line 96

def breakers_service
  return @service if defined?(@service)

  @service = create_new_breakers_service(breakers_matcher, breakers_exception_handler)
end

#create_new_breakers_service(matcher, exception_handler) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/common/client/configuration/base.rb', line 125

def create_new_breakers_service(matcher, exception_handler)
  Breakers::Service.new(
    name: service_name,
    request_matcher: matcher,
    error_threshold: breakers_error_threshold,
    exception_handler:
  )
end

#request_optionsObject

Default request options, sets the read and open timeouts.

Returns:

  • Hash default request options.



84
85
86
87
88
89
# File 'lib/common/client/configuration/base.rb', line 84

def request_options
  {
    open_timeout:,
    timeout: read_timeout
  }
end

#service_exceptionObject

Creates a custom service exception with the same namespace as the implementing class.

Returns:

  • Common::Exceptions::BackendServiceException execption with the class’ namespace



71
72
73
74
75
76
77
# File 'lib/common/client/configuration/base.rb', line 71

def service_exception
  if current_module.const_defined?('ServiceException')
    current_module.const_get('ServiceException')
  else
    current_module.const_set('ServiceException', Class.new(Common::Exceptions::BackendServiceException))
  end
end

#service_nameObject

The service name that shows up in breakers metrics and logs. Implemented by sub classes.

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/common/client/configuration/base.rb', line 62

def service_name
  raise NotImplementedError, "Subclass #{self.class.name} of Configuration must implement service_name"
end