Sha256: 1485d03d448077a7c7488b47d3ed98a7d312d3c3be3100377ef3903ab98e480d

Contents?: true

Size: 1.25 KB

Versions: 11

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true
module BitsService
  class LoggingHttpClient
    def initialize(http_client)
      @http_client = http_client
      @logger = Steno.logger('cc.bits_service_client')
    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)

      @http_client.request(request).tap do |response|
        @logger.info('Response', { code: response.code, vcap_request_id: vcap_request_id })
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
bits_service_client-0.3.1 lib/bits_service_client/logging_http_client.rb
bits_service_client-0.3.1.pre.1 lib/bits_service_client/logging_http_client.rb
bits_service_client-0.3.1.pre.pre.1 lib/bits_service_client/logging_http_client.rb
bits_service_client-0.3.0 lib/bits_service_client/logging_http_client.rb
bits_service_client-0.2.2.pre.10 lib/bits_service_client/logging_http_client.rb
bits_service_client-0.2.2.pre.9 lib/bits_service_client/logging_http_client.rb
bits_service_client-0.2.2.pre.8 lib/bits_service_client/logging_http_client.rb
bits_service_client-0.2.2.pre.7 lib/bits_service_client/logging_http_client.rb
bits_service_client-0.2.2.pre.6 lib/bits_service_client/logging_http_client.rb
bits_service_client-0.2.2.pre.5 lib/bits_service_client/logging_http_client.rb
bits_service_client-0.2.2.pre.4 lib/bits_service_client/logging_http_client.rb