Class: Buildkite::TestCollector::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/buildkite/test_collector/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ HTTPClient

Returns a new instance of HTTPClient.



8
9
10
11
# File 'lib/buildkite/test_collector/http_client.rb', line 8

def initialize(url)
  @url = url
  @authorization_header = "Token token=\"#{Buildkite::TestCollector.api_token}\""
end

Instance Attribute Details

#authorization_headerObject (readonly)

Returns the value of attribute authorization_header.



7
8
9
# File 'lib/buildkite/test_collector/http_client.rb', line 7

def authorization_header
  @authorization_header
end

Instance Method Details

#metadataObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/buildkite/test_collector/http_client.rb', line 50

def 
  contact_uri = URI.parse("#{url}/metadata")

  http = Net::HTTP.new(contact_uri.host, contact_uri.port)
  http.use_ssl = contact_uri.scheme == "https"

  contact = Net::HTTP::Get.new(contact_uri.path, {
    "Authorization" => authorization_header,
    "Content-Type" => "application/json"
  })

  http.request(contact)
end

#post_json(data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/buildkite/test_collector/http_client.rb', line 13

def post_json(data)
  contact_uri = URI.parse(url)

  http = Net::HTTP.new(contact_uri.host, contact_uri.port)
  http.use_ssl = contact_uri.scheme == "https"

  contact = Net::HTTP::Post.new(contact_uri.path, {
    "Authorization" => authorization_header,
    "Content-Type" => "application/json",
    "Content-Encoding" => "gzip",
  })

  data_set = data.map(&:as_hash)

  body = {
    run_env: Buildkite::TestCollector::CI.env,
    format: "json",
    data: data_set
  }.to_json

  compressed_body = StringIO.new

  writer = Zlib::GzipWriter.new(compressed_body)
  writer.write(body)
  writer.close

  contact.body = compressed_body.string

  response = http.request(contact)

  if response.is_a?(Net::HTTPSuccess)
    response
  else
    raise "HTTP Request Failed: #{response.code} #{response.message}"
  end
end