Class: BitsService::LoggingHttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/bits_service_client/logging_http_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(http_client) ⇒ LoggingHttpClient

Returns a new instance of LoggingHttpClient.



4
5
6
7
# File 'lib/bits_service_client/logging_http_client.rb', line 4

def initialize(http_client)
  @http_client = http_client
  @logger = Steno.logger('cc.bits_service_client')
end

Instance Method Details

#delete(path, vcap_request_id) ⇒ Object



25
26
27
# File 'lib/bits_service_client/logging_http_client.rb', line 25

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

#do_request(request, vcap_request_id) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bits_service_client/logging_http_client.rb', line 29

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

#get(path, vcap_request_id, credentials = nil) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/bits_service_client/logging_http_client.rb', line 9

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

#head(path, vcap_request_id) ⇒ Object



17
18
19
# File 'lib/bits_service_client/logging_http_client.rb', line 17

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

#put(path, body, vcap_request_id) ⇒ Object



21
22
23
# File 'lib/bits_service_client/logging_http_client.rb', line 21

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