Class: Instana::Backend::RequestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/instana/backend/request_client.rb

Overview

Convince wrapper around Net::HTTP.

Since:

  • 1.197.0

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, use_ssl: false) ⇒ RequestClient

Returns a new instance of RequestClient.

Since:

  • 1.197.0



37
38
39
40
41
42
# File 'lib/instana/backend/request_client.rb', line 37

def initialize(host, port, use_ssl: false)
  timeout = Integer(ENV.fetch('INSTANA_TIMEOUT', 500))
  @host = host
  @port = port
  @client = Net::HTTP.start(host, port, use_ssl: use_ssl, read_timeout: timeout)
end

Instance Attribute Details

#hostObject (readonly)

Since:

  • 1.197.0



23
24
25
# File 'lib/instana/backend/request_client.rb', line 23

def host
  @host
end

#portObject (readonly)

Since:

  • 1.197.0



23
24
25
# File 'lib/instana/backend/request_client.rb', line 23

def port
  @port
end

Instance Method Details

#send_request(method, path, data = nil, headers = {}) ⇒ Object

Send a request to the backend. If data is a Hash, encode the object as JSON and set the proper headers.

Parameters:

  • method (String)

    request method

  • path (String)

    request path

  • data (Hash, String) (defaults to: nil)

    request body

  • headers (Hash) (defaults to: {})

    extra request headers to send

Since:

  • 1.197.0



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/instana/backend/request_client.rb', line 51

def send_request(method, path, data = nil, headers = {})
  body = if data.is_a?(Hash) || data.is_a?(Array)
           headers['Content-Type'] = 'application/json'
           headers['Accept'] = 'application/json'

           encode_body(data)
         else
           headers['Content-Type'] = 'application/octet-stream'

           data
         end

  response = @client.send_request(method, path, body, headers)
  Response.new(response)
end