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.



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

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.



17
18
19
# File 'lib/datadog/ci/transport/adapters/net.rb', line 17

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



17
18
19
# File 'lib/datadog/ci/transport/adapters/net.rb', line 17

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



17
18
19
# File 'lib/datadog/ci/transport/adapters/net.rb', line 17

def ssl
  @ssl
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



17
18
19
# File 'lib/datadog/ci/transport/adapters/net.rb', line 17

def timeout
  @timeout
end

Instance Method Details

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



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

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



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

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

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

  req.start(&block)
end

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



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

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