Sha256: f6c4ae96d8f6c82e0b65207bd56235aabc3b516a376abd1fe2e87e5ee4d3eb64

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require "net/http"

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

    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

      http.request(contact)
    end

    def metadata
      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

    private

    attr :url
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
buildkite-test_collector-2.6.1 lib/buildkite/test_collector/http_client.rb
buildkite-test_collector-2.6.0 lib/buildkite/test_collector/http_client.rb
buildkite-test_collector-2.5.0 lib/buildkite/test_collector/http_client.rb
buildkite-test_collector-2.4.0 lib/buildkite/test_collector/http_client.rb