Sha256: 8339f30917a43a7c6b2e86bb2bad707572f01e615bb09fe309b4362eb7b6324c

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'uri'
require 'open-uri'
require 'net/https'

module Concourse
  class Client
    API_BASE_PATH = '/api/v1/teams/main/'

    attr_reader :base_uri

    def initialize(concourse_uri, username: nil, password: nil, options: {})
      @concourse_uri = URI(concourse_uri)
      @username = username
      @password = password
      @options = options
      @base_uri = @concourse_uri.merge(API_BASE_PATH)
    end

    def get(path)
      path = path[1..-1] if path.start_with?('/')

      begin
        options_with_auth = if authenticated?
                              options.merge('Cookie' => "ATC-Authorization=\"#{bearer_token}\"")
                            else
                              options
                            end

        open(base_uri.merge(path), options_with_auth).read
      rescue OpenURI::HTTPError => e
        if e.message == '401 Unauthorized' && @bearer_token
          @bearer_token = nil
          retry
        else
          raise
        end
      end
    end

    private

    attr_reader :options, :username, :password

    def bearer_token
      @bearer_token ||= authenticate
    end

    def authenticated?
      username && password
    end

    def authenticate
      io = open(
        @concourse_uri.merge('/auth/basic/token?team_name=main'),
        options.merge(http_basic_authentication: [@username, @password])
      )

      code, message = io.status
      raise message unless code == '200'

      json = JSON.parse(io.read)
      "#{json['type']} #{json['value']}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bitbar-concourse-1.1 lib/concourse/client.rb