Sha256: bb1b25783fc02e174efa48b35393b5c8ab97f5242065072c9718d012e2086e04

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 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
      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",
      })
      contact.body = {
        run_env: Buildkite::TestCollector::CI.env,
        format: "websocket"
      }.to_json

      http.request(contact)
    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",
      })

      data_set = data.map(&:as_hash)

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

      http.request(contact)
    end

    private

    attr :url
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
buildkite-test_collector-2.1.0.pre lib/buildkite/test_collector/http_client.rb
buildkite-test_collector-2.0.0.pre lib/buildkite/test_collector/http_client.rb