Sha256: 37fcde5b9efbbad5ff81310e82ef184c12c6e18ff4cbd43afa8ab0ddc52fb4da

Contents?: true

Size: 1.67 KB

Versions: 8

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

require 'statsd'

module BitsService
  class LoggingHttpClient
    def initialize(http_client)
      @http_client = http_client
      @logger = Steno.logger('cc.bits_service_client')
      @statsd = Statsd.new
    end

    def get(path, vcap_request_id, credentials=nil)
      req = Net::HTTP::Get.new(path)
      if credentials
        req.basic_auth(credentials[:username], credentials[:password])
      end
      do_request(req, vcap_request_id)
    end

    def head(path, vcap_request_id)
      do_request(Net::HTTP::Head.new(path), vcap_request_id)
    end

    def put(path, body, vcap_request_id)
      do_request(Net::HTTP::Put::Multipart.new(path, body), vcap_request_id)
    end

    def delete(path, vcap_request_id)
      do_request(Net::HTTP::Delete.new(path), vcap_request_id)
    end

    def do_request(request, vcap_request_id)
      @logger.info('Request', {
        method: request.method,
        path: request.path,
        address: @http_client.address,
        port: @http_client.port,
        vcap_request_id: vcap_request_id,
      })

      request.add_field('X-VCAP-REQUEST-ID', vcap_request_id)

      begin
        response = @http_client.request(request)
        @logger.info('Response', { code: response.code, vcap_request_id: vcap_request_id })
      rescue Net::ReadTimeout => ex
        @statsd.increment("cc.bits_#{request.method.downcase}.timeout")

        @logger.info('Request timeout', {
          method: request.method,
          path: request.path,
          address: @http_client.address,
          port: @http_client.port,
          vcap_request_id: vcap_request_id,
        })
        raise ex
      end
      response
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
bits_service_client-4.0.0 lib/bits_service_client/logging_http_client.rb
bits_service_client-4.0.0.pre.4 lib/bits_service_client/logging_http_client.rb
bits_service_client-4.0.0.pre.3 lib/bits_service_client/logging_http_client.rb
bits_service_client-4.0.0.pre.2 lib/bits_service_client/logging_http_client.rb
bits_service_client-4.0.0.pre.1 lib/bits_service_client/logging_http_client.rb
bits_service_client-3.4.0 lib/bits_service_client/logging_http_client.rb
bits_service_client-3.4.0.pre.2 lib/bits_service_client/logging_http_client.rb
bits_service_client-3.4.0.pre.1 lib/bits_service_client/logging_http_client.rb