class Buildkit::Client

Constants

CONVENIENCE_HEADERS

Header keys that can be passed in options hash to {#get},{#head}

DEFAULT_ENDPOINT
RACK_BUILDER_CLASS

In Faraday 0.9, Faraday::Builder was renamed to Faraday::RackBuilder

Attributes

auto_paginate[RW]
last_response[R]

Public Class Methods

build_middleware() { |builder| ... } click to toggle source
# File lib/buildkit/client.rb, line 34
def build_middleware
  RACK_BUILDER_CLASS.new do |builder|
    builder.use Buildkit::Response::RaiseError
    builder.adapter Faraday.default_adapter
    yield builder if block_given?
  end
end
new(endpoint: ENV.fetch('BUILDKITE_API_ENDPOINT', DEFAULT_ENDPOINT), token: ENV.fetch('BUILDKITE_API_TOKEN'), middleware: self.class.build_middleware, auto_paginate: false) click to toggle source
# File lib/buildkit/client.rb, line 43
def initialize(endpoint: ENV.fetch('BUILDKITE_API_ENDPOINT', DEFAULT_ENDPOINT),
               token: ENV.fetch('BUILDKITE_API_TOKEN'),
               middleware: self.class.build_middleware, auto_paginate: false)
  @middleware = middleware
  @endpoint = endpoint
  @token = token
  @auto_paginate = auto_paginate
end

Public Instance Methods

delete(url, options = {}) click to toggle source

Make a HTTP DELETE request

@param url [String] The path, relative to {@endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/buildkit/client.rb, line 97
def delete(url, options = {})
  request :delete, url, options
end
get(url, options = {}) click to toggle source

Make a HTTP GET request

@param url [String] The path, relative to {@endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/buildkit/client.rb, line 57
def get(url, options = {})
  if @auto_paginate
    paginate :get, url, parse_query_and_convenience_headers(options)
  else
    request :get, url, parse_query_and_convenience_headers(options)
  end
end
head(url, options = {}) click to toggle source

Make a HTTP HEAD request

@param url [String] The path, relative to {@endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/buildkit/client.rb, line 106
def head(url, options = {})
  request :head, url, parse_query_and_convenience_headers(options)
end
patch(url, options = {}) click to toggle source

Make a HTTP PATCH request

@param url [String] The path, relative to {@endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/buildkit/client.rb, line 88
def patch(url, options = {})
  request :patch, url, options
end
post(url, options = {}) click to toggle source

Make a HTTP POST request

@param url [String] The path, relative to {@endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/buildkit/client.rb, line 70
def post(url, options = {})
  request :post, url, options
end
put(url, options = {}) click to toggle source

Make a HTTP PUT request

@param url [String] The path, relative to {@endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/buildkit/client.rb, line 79
def put(url, options = {})
  request :put, url, options
end
root() click to toggle source

Fetch the root resource for the API

@return [Sawyer::Resource]

# File lib/buildkit/client.rb, line 115
def root
  get('/')
end

Private Instance Methods

build_path(uri) click to toggle source
# File lib/buildkit/client.rb, line 165
def build_path(uri)
  "#{uri.path}?#{uri.query}"
end
extract_query_and_headers_from(data) click to toggle source
# File lib/buildkit/client.rb, line 133
def extract_query_and_headers_from(data)
  {
    query: data.delete(:query) || {},
    headers: data.delete(:headers) || {},
  }
end
next_page(next_page) click to toggle source
# File lib/buildkit/client.rb, line 161
def next_page(next_page)
  build_path URI(next_page)
end
paginate(method, path, data, options = {}) click to toggle source
# File lib/buildkit/client.rb, line 140
def paginate(method, path, data, options = {})
  response = []
  loop do
    request method, path, data, options

    # Paginated API calls always return Arrays.
    return last_response.data unless @last_response.data.is_a?(Array)

    response.concat @last_response.data

    break if @last_response.headers[:link].nil?

    link_header = parse_link_header(@last_response.headers[:link])
    break if link_header[:next].nil?

    unescaped_next = CGI.unescape(link_header[:next])
    path = next_page(unescaped_next)
  end
  response
end
parse_query_and_convenience_headers(options) click to toggle source
# File lib/buildkit/client.rb, line 189
def parse_query_and_convenience_headers(options)
  headers = options.fetch(:headers, {})
  CONVENIENCE_HEADERS.each do |h|
    if header = options.delete(h)
      headers[h] = header
    end
  end
  query = options.delete(:query)
  opts = {query: options}
  opts[:query].merge!(query) if query&.is_a?(Hash)
  opts[:headers] = headers unless headers.empty?

  opts
end
request(method, path, data, options = {}) click to toggle source
# File lib/buildkit/client.rb, line 121
def request(method, path, data, options = {})
  if data.is_a?(Hash)
    options = extract_query_and_headers_from data
    if accept = data.delete(:accept)
      options[:headers][:accept] = accept
    end
  end

  @last_response = response = sawyer_agent.call(method, URI::DEFAULT_PARSER.escape(path.to_s), data, options)
  response.data
end
sawyer_agent() click to toggle source
# File lib/buildkit/client.rb, line 169
def sawyer_agent
  @sawyer_agent ||= Sawyer::Agent.new(@endpoint, sawyer_options) do |http|
    http.headers[:accept] = 'application/json'
    http.headers[:content_type] = 'application/json'
    http.headers[:user_agent] = "Buildkit v#{Buildkit::VERSION}"
    if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('1.7.1')
      http.request :authorization, 'Bearer', @token
    else
      http.authorization 'Bearer', @token
    end
  end
end
sawyer_options() click to toggle source
# File lib/buildkit/client.rb, line 182
def sawyer_options
  {
    links_parser: Sawyer::LinkParsers::Simple.new,
    faraday: Faraday.new(builder: @middleware),
  }
end