Sha256: 44fa5631857db4f4c8c8e654d885aa0cd6f98ef0fdb443b87f803bd86519f7b9

Contents?: true

Size: 858 Bytes

Versions: 1

Compression:

Stored size: 858 Bytes

Contents

require "net/http"
require "uri"
require "json"

module Sensu
  module Run
    class APIClient
      def initialize(options={})
        @options = options
        uri = URI.parse(select_backend)
        @http = Net::HTTP.new(uri.host, uri.port)
      end

      def select_backend
        @backends ||= []
        if @backends.empty?
          @backends = @options[:backends].shuffle
        end
        @backends.shift
      end

      def post_event(event)
        namespace = @options.fetch(:namespace, "default")
        request = Net::HTTP::Post.new("/api/core/v2/namespaces/#{namespace}/events")
        request["Content-Type"] = "application/json"
        request["Authorization"] = "Key #{@options[:api_key]}"
        request.body = JSON.dump(event)
        response = @http.request(request)
        puts response.inspect
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sensu-run-0.1.0 lib/sensu/run/api_client.rb