class LookerSDK::Client

Client for the LookerSDK API

@see look TODO docs link

Constants

CONVENIENCE_HEADERS

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

Public Class Methods

new(opts = {}) { |self| ... } click to toggle source
# File lib/looker-sdk/client.rb, line 47
def initialize(opts = {})
  # Use options passed in, but fall back to module defaults
  LookerSDK::Configurable.keys.each do |key|
    instance_variable_set(:"@#{key}", opts[key] || LookerSDK.instance_variable_get(:"@#{key}"))
  end

  # allow caller to do configuration in a block before we load swagger and become dynamic
  yield self if block_given?

  # Save the original state of the options because live variables received later like access_token and
  # client_id appear as if they are options and confuse the automatic client generation in LookerSDK#client
  @original_options = options.dup

  load_credentials_from_netrc unless application_credentials?
  if !@lazy_swagger
    load_swagger
  end
  self.dynamic = true
end

Public Instance Methods

access_token=(value) click to toggle source

Set OAuth access token for authentication

@param value [String] Looker OAuth access token

# File lib/looker-sdk/client.rb, line 270
def access_token=(value)
  reset_agent
  @access_token = value
end
agent() click to toggle source

Cached Hypermedia agent for the LookerSDK API (with default options)

@return [Sawyer::Agent]

# File lib/looker-sdk/client.rb, line 210
def agent
  @agent ||= make_agent
end
alive() click to toggle source

Is the server alive (this can be called w/o authentication)

@return http status code

# File lib/looker-sdk/client.rb, line 224
def alive
  without_authentication do
    get '/alive'
  end
  last_response.status
end
alive?() click to toggle source

Are we connected to the server? - Does not attempt to authenticate.

# File lib/looker-sdk/client.rb, line 232
def alive?
  begin
    without_authentication do
      get('/alive')
    end
    true
  rescue
    false
  end
end
authenticated?() click to toggle source

Are we connected and authenticated to the server?

# File lib/looker-sdk/client.rb, line 244
def authenticated?
  begin
    ensure_logged_in
    true
  rescue
    false
  end
end
client_id=(value) click to toggle source

Set OAuth app client_id

@param value [String] Looker OAuth app client_id

# File lib/looker-sdk/client.rb, line 278
def client_id=(value)
  reset_agent
  @client_id = value
end
client_secret=(value) click to toggle source

Set OAuth app client_secret

@param value [String] Looker OAuth app client_secret

# File lib/looker-sdk/client.rb, line 286
def client_secret=(value)
  reset_agent
  @client_secret = value
end
delete(url, options = {}, encoded=false, &block) click to toggle source

Make a HTTP DELETE request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @param encoded [Boolean] true: url already encoded, false: url needs encoding @return [Sawyer::Resource]

# File lib/looker-sdk/client.rb, line 147
def delete(url, options = {}, encoded=false, &block)
  request :delete, url, nil, parse_query_and_convenience_headers(options), encoded, &block
end
get(url, options = {}, encoded=false, &block) click to toggle source

Make a HTTP GET request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @param encoded [Boolean] true: url already encoded, false: url needs encoding @param &block [Block] Block to be called with |response, chunk| for each chunk of the body from

the server. The block must return true to continue, or false to abort streaming.

@return [Sawyer::Resource]

# File lib/looker-sdk/client.rb, line 98
def get(url, options = {}, encoded=false, &block)
  request :get, url, nil, parse_query_and_convenience_headers(options), encoded, &block
end
head(url, options = {}, encoded=false, &block) click to toggle source

Make a HTTP HEAD request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @param encoded [Boolean] true: url already encoded, false: url needs encoding @return [Sawyer::Resource]

# File lib/looker-sdk/client.rb, line 157
def head(url, options = {}, encoded=false, &block)
  request :head, url, nil, parse_query_and_convenience_headers(options), encoded
end
inspect() click to toggle source

Text representation of the client, masking tokens and passwords

@return [String]

Calls superclass method
# File lib/looker-sdk/client.rb, line 78
def inspect
  inspected = super

  # Only show last 4 of token, secret
  [@access_token, @client_secret].compact.each do |str|
    len = [str.size - 4, 0].max
    inspected = inspected.gsub! str, "#{'*'*len}#{str[len..-1]}"
  end

  inspected
end
last_error() click to toggle source

Response for last HTTP request

@return [StandardError]

# File lib/looker-sdk/client.rb, line 263
def last_error
  @last_error if defined? @last_error
end
last_response() click to toggle source

Response for last HTTP request

@return [Sawyer::Response]

# File lib/looker-sdk/client.rb, line 256
def last_response
  @last_response if defined? @last_response
end
looker_warn(*message) click to toggle source

Wrapper around Kernel#warn to print warnings unless LOOKER_SILENT is set to true.

@return [nil]

# File lib/looker-sdk/client.rb, line 295
def looker_warn(*message)
  unless ENV['LOOKER_SILENT']
    warn message
  end
end
make_agent(options = nil) click to toggle source

Hypermedia agent for the LookerSDK API (with specific options)

@return [Sawyer::Agent]

# File lib/looker-sdk/client.rb, line 198
def make_agent(options = nil)
  options ||= sawyer_options
  Sawyer::Agent.new(api_endpoint, options) do |http|
    http.headers[:accept] = default_media_type
    http.headers[:user_agent] = user_agent
    http.authorization('token', @access_token) if token_authenticated?
  end
end
paginate(url, options = {}) { |data, last_response| ... } click to toggle source

Make one or more HTTP GET requests, optionally fetching the next page of results from URL in Link response header based on value in {#auto_paginate}.

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @param block [Block] Block to perform the data concatination of the

multiple requests. The block is called with two parameters, the first
contains the contents of the requests so far and the second parameter
contains the latest response.

@return [Sawyer::Resource]

# File lib/looker-sdk/client.rb, line 172
def paginate(url, options = {}, &block)
  opts = parse_query_and_convenience_headers(options)
  if @auto_paginate || @per_page
    opts[:query][:per_page] ||=  @per_page || (@auto_paginate ? 100 : nil)
  end

  data = request(:get, url, nil, opts)

  if @auto_paginate
    while @last_response.rels[:next] && rate_limit.remaining > 0
      @last_response = @last_response.rels[:next].get
      if block_given?
        yield(data, @last_response)
      else
        data.concat(@last_response.data) if @last_response.data.is_a?(Array)
      end
    end

  end

  data
end
patch(url, data = {}, options = {}, encoded=false, &block) click to toggle source

Make a HTTP PATCH request

@param url [String] The path, relative to {#api_endpoint} @param data [String|Array|Hash] Body and optionally header params for request @param options [Hash] Optional header params for request @param encoded [Boolean] true: url already encoded, false: url needs encoding @param &block [Block] Block to be called with |response, chunk| for each chunk of the body from

the server. The block must return true to continue, or false to abort streaming.

@return [Sawyer::Resource]

# File lib/looker-sdk/client.rb, line 137
def patch(url, data = {}, options = {}, encoded=false, &block)
  request :patch, url, data, parse_query_and_convenience_headers(options), encoded, &block
end
post(url, data = {}, options = {}, encoded=false, &block) click to toggle source

Make a HTTP POST request

@param url [String] The path, relative to {#api_endpoint} @param data [String|Array|Hash] Body and optionally header params for request @param options [Hash] Optional header params for request @param encoded [Boolean] true: url already encoded, false: url needs encoding @param &block [Block] Block to be called with |response, chunk| for each chunk of the body from

the server. The block must return true to continue, or false to abort streaming.

@return [Sawyer::Resource]

# File lib/looker-sdk/client.rb, line 111
def post(url, data = {}, options = {}, encoded=false, &block)
  request :post, url, data, parse_query_and_convenience_headers(options), encoded, &block
end
put(url, data = {}, options = {}, encoded=false, &block) click to toggle source

Make a HTTP PUT request

@param url [String] The path, relative to {#api_endpoint} @param data [String|Array|Hash] Body and optionally header params for request @param options [Hash] Optional header params for request @param encoded [Boolean] true: url already encoded, false: url needs encoding @param &block [Block] Block to be called with |response, chunk| for each chunk of the body from

the server. The block must return true to continue, or false to abort streaming.

@return [Sawyer::Resource]

# File lib/looker-sdk/client.rb, line 124
def put(url, data = {}, options = {}, encoded=false, &block)
  request :put, url, data, parse_query_and_convenience_headers(options), encoded, &block
end
root() click to toggle source

Fetch the root resource for the API

@return [Sawyer::Resource]

# File lib/looker-sdk/client.rb, line 217
def root
  get URI(api_endpoint).path.sub(/\/$/,'')
end
same_options?(opts) click to toggle source

Compares client options to a Hash of requested options

@param opts [Hash] Options to compare with current client options @return [Boolean]

# File lib/looker-sdk/client.rb, line 71
def same_options?(opts)
  opts.hash == @original_options.hash
end

Private Instance Methods

delete_succeeded?() click to toggle source
# File lib/looker-sdk/client.rb, line 417
def delete_succeeded?
  !!last_response && last_response.status == 204
end
faraday_options(options = {}) click to toggle source
# File lib/looker-sdk/client.rb, line 457
def faraday_options(options = {})
  conn_opts = @connection_options.clone
  builder = options[:builder] || @middleware
  conn_opts[:builder] = builder if builder
  conn_opts[:proxy] = @proxy if @proxy
  conn_opts
end
merge_content_type_if_body(body, options = {}) click to toggle source
# File lib/looker-sdk/client.rb, line 473
def merge_content_type_if_body(body, options = {})
  if body
    if body.kind_of?(Faraday::UploadIO)
      length = File.new(body.local_path).size.to_s
      headers = {:content_type => body.content_type, :content_length => length}.merge(options[:headers] || {})
    else
      headers = {:content_type => default_media_type}.merge(options[:headers] || {})
    end
    {:headers => headers}.merge(options)
  else
    options
  end
end
parse_query_and_convenience_headers(options) click to toggle source
# File lib/looker-sdk/client.rb, line 487
def parse_query_and_convenience_headers(options)
  return {} if options.nil?
  raise "options is not a hash" unless options.is_a?(Hash)
  return {} if options.empty?

  options = options.dup
  headers = options.delete(:headers) || {}
  CONVENIENCE_HEADERS.each do |h|
    if header = options.delete(h)
      headers[h] = header
    end
  end
  query = options.delete(:query) || {}
  raise "query '#{query}' is not a hash" unless query.is_a?(Hash)
  query = options.merge(query)

  opts = {}
  opts[:query] = query unless query.empty?
  opts[:headers] = headers unless headers.empty?

  opts
end
request(method, path, data, options, encoded, &block) click to toggle source
# File lib/looker-sdk/client.rb, line 307
def request(method, path, data, options, encoded, &block)
  ensure_logged_in
  begin
    path = path.to_s
    if !encoded
      path = URI::Parser.new.escape(path)
    end
    @last_response = @last_error = nil
    return stream_request(method, path, data, options, &block) if block_given?
    @last_response = response = agent.call(method, path, data, options)
    @raw_responses ? response : response.data
  rescue StandardError => e
    @last_error = e
    raise
  end
end
reset_agent() click to toggle source
# File lib/looker-sdk/client.rb, line 303
def reset_agent
  @agent = nil
end
sawyer_options(options = {}) click to toggle source
# File lib/looker-sdk/client.rb, line 465
def sawyer_options(options = {})
  {
    :links_parser => Sawyer::LinkParsers::Simple.new,
    :serializer  => serializer,
    :faraday => options[:faraday] || @faraday || Faraday.new(faraday_options)
  }
end
serializer() click to toggle source
# File lib/looker-sdk/client.rb, line 450
def serializer
  @serializer ||= (
    require 'json'
    Serializer.new(JSON)
  )
end
stream_request(method, path, data, options, &block) click to toggle source
# File lib/looker-sdk/client.rb, line 324
def stream_request(method, path, data, options, &block)
  conn_opts = faraday_options(:builder => StreamingClient.new(self, &block))
  agent = make_agent(sawyer_options(:faraday => Faraday.new(conn_opts)))
  @last_response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
end