Sha256: e72e7914ff562f72e24523b01c3e30ad066124d8bbe521076d9b8eaa2d6f0ba9

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require "net/http"
require "excon"

module BingAdsRubySdk
  class HttpClient
    @http_connections = {}
    HTTP_OPEN_TIMEOUT = 10
    HTTP_READ_TIMEOUT = 20
    HTTP_RETRY_COUNT_ON_TIMEOUT = 2
    HTTP_INTERVAL_RETRY_COUNT_ON_TIMEOUT = 1
    HTTP_ERRORS = [ Net::HTTPServerError, Net::HTTPClientError ]
    CONNECTION_SETTINGS = {
      persistent: true,
      tcp_nodelay: true,
      retry_limit: HTTP_RETRY_COUNT_ON_TIMEOUT,
      idempotent: true,
      retry_interval: HTTP_INTERVAL_RETRY_COUNT_ON_TIMEOUT,
      connect_timeout: HTTP_OPEN_TIMEOUT,
      read_timeout: HTTP_READ_TIMEOUT,
      ssl_version: :TLSv1_2,
      ciphers: "TLSv1.2:!aNULL:!eNULL",
    }

    class << self
      def post(request)
        uri = URI(request.url)
        conn = self.connection(request.url)
        raw_response = conn.post(
          path: uri.path,
          body: request.content,
          headers: request.headers,
        )

        if contains_error?(raw_response)
          BingAdsRubySdk.log(:warn) { BingAdsRubySdk::LogMessage.new(raw_response.body).to_s }
          raise BingAdsRubySdk::Errors::ServerError, raw_response.body
        else
          BingAdsRubySdk.log(:debug) { BingAdsRubySdk::LogMessage.new(raw_response.body).to_s }
        end

        raw_response.body
      end

      def close_http_connections
        self.http_connections.values.each do |connection|
          connection.reset
        end
      end

      protected

      attr_reader :http_connections

      def contains_error?(response)
        HTTP_ERRORS.any? { |http_error_class| response.class <= http_error_class }
      end

      def connection_settings
        CONNECTION_SETTINGS.tap do |args|
          instrumentor = BingAdsRubySdk.config.instrumentor
          args[:instrumentor] = instrumentor if instrumentor
        end
      end

      def connection(host)
        self.http_connections[host] ||= Excon.new(
          host,
          connection_settings
        )
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bing_ads_ruby_sdk-1.3.4 lib/bing_ads_ruby_sdk/http_client.rb
bing_ads_ruby_sdk-1.3.3 lib/bing_ads_ruby_sdk/http_client.rb
bing_ads_ruby_sdk-1.3.2 lib/bing_ads_ruby_sdk/http_client.rb
bing_ads_ruby_sdk-1.3.1 lib/bing_ads_ruby_sdk/http_client.rb
bing_ads_ruby_sdk-1.3.0 lib/bing_ads_ruby_sdk/http_client.rb