Class: OpinionatedHTTP::Client
- Inherits:
-
Object
- Object
- OpinionatedHTTP::Client
- Defined in:
- lib/opinionated_http/client.rb
Constant Summary collapse
- HTTP_RETRY_CODES =
502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout
%w[502 503 504]
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#error_class ⇒ Object
readonly
Returns the value of attribute error_class.
-
#http_retry_codes ⇒ Object
readonly
Returns the value of attribute http_retry_codes.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#metric_prefix ⇒ Object
readonly
Returns the value of attribute metric_prefix.
-
#retry_count ⇒ Object
readonly
Returns the value of attribute retry_count.
-
#retry_interval ⇒ Object
readonly
Returns the value of attribute retry_interval.
-
#retry_multiplier ⇒ Object
readonly
Returns the value of attribute retry_multiplier.
-
#secret_config_prefix ⇒ Object
readonly
Returns the value of attribute secret_config_prefix.
Instance Method Summary collapse
-
#get(action:, path: "/#{action}", parameters: nil) ⇒ Object
Perform an HTTP Get against the supplied path.
-
#initialize(secret_config_prefix:, logger: nil, metric_prefix:, error_class:, **options) ⇒ Client
constructor
A new instance of Client.
- #post(action:, path: "/#{action}", parameters: nil) ⇒ Object
Constructor Details
#initialize(secret_config_prefix:, logger: nil, metric_prefix:, error_class:, **options) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/opinionated_http/client.rb', line 16 def initialize(secret_config_prefix:, logger: nil, metric_prefix:, error_class:, **) @metric_prefix = metric_prefix @logger = logger || SemanticLogger[self] @error_class = error_class @retry_count = SecretConfig.fetch("#{secret_config_prefix}/retry_count", type: :integer, default: 11) @retry_interval = SecretConfig.fetch("#{secret_config_prefix}/retry_interval", type: :float, default: 0.01) @retry_multiplier = SecretConfig.fetch("#{secret_config_prefix}/retry_multiplier", type: :float, default: 1.8) http_retry_codes = SecretConfig.fetch("#{secret_config_prefix}/http_retry_codes", type: :string, default: HTTP_RETRY_CODES.join(",")) @http_retry_codes = http_retry_codes.split(",").collect { |str| str.strip } internal_logger = OpinionatedHTTP::Logger.new(@logger) = { logger: internal_logger, debug_output: internal_logger, name: "", pool_size: SecretConfig.fetch("#{secret_config_prefix}/pool_size", type: :integer, default: 100), open_timeout: SecretConfig.fetch("#{secret_config_prefix}/open_timeout", type: :float, default: 10), read_timeout: SecretConfig.fetch("#{secret_config_prefix}/read_timeout", type: :float, default: 10), idle_timeout: SecretConfig.fetch("#{secret_config_prefix}/idle_timeout", type: :float, default: 300), keep_alive: SecretConfig.fetch("#{secret_config_prefix}/keep_alive", type: :float, default: 300), pool_timeout: SecretConfig.fetch("#{secret_config_prefix}/pool_timeout", type: :float, default: 5), warn_timeout: SecretConfig.fetch("#{secret_config_prefix}/warn_timeout", type: :float, default: 0.25), proxy: SecretConfig.fetch("#{secret_config_prefix}/proxy", type: :symbol, default: :ENV), force_retry: SecretConfig.fetch("#{secret_config_prefix}/force_retry", type: :boolean, default: true), } url = SecretConfig["#{secret_config_prefix}/url"] [:url] = url if url @driver = PersistentHTTP.new(.merge()) end |
Instance Attribute Details
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
13 14 15 |
# File 'lib/opinionated_http/client.rb', line 13 def driver @driver end |
#error_class ⇒ Object (readonly)
Returns the value of attribute error_class.
13 14 15 |
# File 'lib/opinionated_http/client.rb', line 13 def error_class @error_class end |
#http_retry_codes ⇒ Object (readonly)
Returns the value of attribute http_retry_codes.
13 14 15 |
# File 'lib/opinionated_http/client.rb', line 13 def http_retry_codes @http_retry_codes end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
13 14 15 |
# File 'lib/opinionated_http/client.rb', line 13 def logger @logger end |
#metric_prefix ⇒ Object (readonly)
Returns the value of attribute metric_prefix.
13 14 15 |
# File 'lib/opinionated_http/client.rb', line 13 def metric_prefix @metric_prefix end |
#retry_count ⇒ Object (readonly)
Returns the value of attribute retry_count.
13 14 15 |
# File 'lib/opinionated_http/client.rb', line 13 def retry_count @retry_count end |
#retry_interval ⇒ Object (readonly)
Returns the value of attribute retry_interval.
13 14 15 |
# File 'lib/opinionated_http/client.rb', line 13 def retry_interval @retry_interval end |
#retry_multiplier ⇒ Object (readonly)
Returns the value of attribute retry_multiplier.
13 14 15 |
# File 'lib/opinionated_http/client.rb', line 13 def retry_multiplier @retry_multiplier end |
#secret_config_prefix ⇒ Object (readonly)
Returns the value of attribute secret_config_prefix.
13 14 15 |
# File 'lib/opinionated_http/client.rb', line 13 def secret_config_prefix @secret_config_prefix end |
Instance Method Details
#get(action:, path: "/#{action}", parameters: nil) ⇒ Object
Perform an HTTP Get against the supplied path
48 49 50 51 52 53 54 55 56 |
# File 'lib/opinionated_http/client.rb', line 48 def get(action:, path: "/#{action}", parameters: nil) path = "/#{path}" unless path.start_with?("/") path = "#{path}?#{URI.encode_www_form(parameters)}" if parameters request = Net::HTTP::Get.new(path) response = request_with_retry(action: action, path: path, request: request) response.body end |
#post(action:, path: "/#{action}", parameters: nil) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/opinionated_http/client.rb', line 58 def post(action:, path: "/#{action}", parameters: nil) path = "/#{path}" unless path.start_with?("/") request = Net::HTTP::Post.new(path) request.set_form_data(parameters) if parameters response = request_with_retry(action: action, path: path, request: request) response.body end |