Class: Buildkite::TestCollector::HTTPClient

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

Instance Method Summary collapse

Constructor Details

#initialize(url:, api_token:) ⇒ HTTPClient

Returns a new instance of HTTPClient.



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

def initialize(url:, api_token:)
  @url = url
  @api_token = api_token
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 
  endpoint_uri = URI.parse("#{url}/metadata")

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

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

  http.request(request)
end

#post_upload(data:, run_env:, tags:) ⇒ Object



12
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 12

def post_upload(data:, run_env:, tags:)
  endpoint_uri = URI.parse(url)

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

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

  data_set = data.map(&:as_hash)

  body = {
    run_env: run_env,
    tags: tags,
    format: "json",
    data: data_set
  }.to_json

  compressed_body = StringIO.new

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

  request.body = compressed_body.string

  response = http.request(request)

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