Class: Datadog::CI::Transport::Adapters::Net

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/transport/adapters/net.rb

Overview

Adapter for Net::HTTP

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname:, port:, ssl:, timeout_seconds:) ⇒ Net

Returns a new instance of Net.



21
22
23
24
25
26
# File 'lib/datadog/ci/transport/adapters/net.rb', line 21

def initialize(hostname:, port:, ssl:, timeout_seconds:)
  @hostname = hostname
  @port = port
  @timeout = timeout_seconds
  @ssl = ssl
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



15
16
17
# File 'lib/datadog/ci/transport/adapters/net.rb', line 15

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/datadog/ci/transport/adapters/net.rb', line 15

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



15
16
17
# File 'lib/datadog/ci/transport/adapters/net.rb', line 15

def ssl
  @ssl
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



15
16
17
# File 'lib/datadog/ci/transport/adapters/net.rb', line 15

def timeout
  @timeout
end

Instance Method Details

#call(path:, payload:, headers:, verb:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/datadog/ci/transport/adapters/net.rb', line 37

def call(path:, payload:, headers:, verb:)
  headers ||= {}
  # skip tracing for internal DD requests
  headers[Core::Transport::Ext::HTTP::HEADER_DD_INTERNAL_UNTRACED_REQUEST] = "1"

  if respond_to?(verb)
    send(verb, path: path, payload: payload, headers: headers)
  else
    raise "Unknown HTTP method [#{verb}]"
  end
end

#open(&block) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/datadog/ci/transport/adapters/net.rb', line 28

def open(&block)
  req = net_http_client.new(hostname, port)

  req.use_ssl = ssl
  req.open_timeout = req.read_timeout = timeout

  req.start(&block)
end

#post(path:, payload:, headers:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/datadog/ci/transport/adapters/net.rb', line 49

def post(path:, payload:, headers:)
  post = ::Net::HTTP::Post.new(path, headers)
  post.body = payload

  # Connect and send the request
  http_response = open do |http|
    http.request(post)
  end

  # Build and return response
  Response.new(http_response)
end