Class: LookerSDK::Client

Inherits:
Object
  • Object
show all
Includes:
Authentication, Dynamic, Configurable
Defined in:
lib/looker-sdk/client.rb,
lib/looker-sdk/client/dynamic.rb

Overview

Client for the LookerSDK API

See Also:

  • TODO docs link

Defined Under Namespace

Modules: Dynamic Classes: Serializer, StreamingClient

Constant Summary collapse

CONVENIENCE_HEADERS =

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

Set.new([:accept, :content_type])

Instance Attribute Summary

Attributes included from Dynamic

#dynamic

Attributes included from Configurable

#access_token, #api_endpoint, #auto_paginate, #client_id, #client_secret, #connection_options, #default_media_type, #faraday, #lazy_swagger, #middleware, #netrc, #netrc_file, #per_page, #proxy, #raw_responses, #shared_swagger, #swagger, #user_agent, #web_endpoint

Attributes included from Authentication

#access_token_expires_at, #access_token_type

Instance Method Summary collapse

Methods included from Dynamic

#clear_swagger, #load_swagger, #method_link, #operations, #respond_to?, #try_load_swagger

Methods included from Configurable

#configure, keys, #netrc?, #reset!

Methods included from Authentication

#application_credentials?, #authenticate, #ensure_logged_in, #logout, #set_access_token_from_params, #token_authenticated?, #without_authentication

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/looker-sdk/client.rb', line 49

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

Instance Method Details

#access_token=(value) ⇒ Object

Set OAuth access token for authentication

Parameters:

  • value (String)

    Looker OAuth access token



272
273
274
275
# File 'lib/looker-sdk/client.rb', line 272

def access_token=(value)
  reset_agent
  @access_token = value
end

#agentSawyer::Agent

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

Returns:

  • (Sawyer::Agent)


212
213
214
# File 'lib/looker-sdk/client.rb', line 212

def agent
  @agent ||= make_agent
end

#aliveObject

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

Returns:

  • http status code



226
227
228
229
230
231
# File 'lib/looker-sdk/client.rb', line 226

def alive
  without_authentication do
    get '/alive'
  end
  last_response.status
end

#alive?Boolean

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

Returns:

  • (Boolean)


234
235
236
237
238
239
240
241
242
243
# File 'lib/looker-sdk/client.rb', line 234

def alive?
  begin
    without_authentication do
      get('/alive')
    end
    true
  rescue
    false
  end
end

#authenticated?Boolean

Are we connected and authenticated to the server?

Returns:

  • (Boolean)


246
247
248
249
250
251
252
253
# File 'lib/looker-sdk/client.rb', line 246

def authenticated?
  begin
    ensure_logged_in
    true
  rescue
    false
  end
end

#client_id=(value) ⇒ Object

Set OAuth app client_id

Parameters:

  • value (String)

    Looker OAuth app client_id



280
281
282
283
# File 'lib/looker-sdk/client.rb', line 280

def client_id=(value)
  reset_agent
  @client_id = value
end

#client_secret=(value) ⇒ Object

Set OAuth app client_secret

Parameters:

  • value (String)

    Looker OAuth app client_secret



288
289
290
291
# File 'lib/looker-sdk/client.rb', line 288

def client_secret=(value)
  reset_agent
  @client_secret = value
end

#delete(url, options = {}, encoded = false, &block) ⇒ Sawyer::Resource

Make a HTTP DELETE request

Parameters:

  • url (String)

    The path, relative to LookerSDK::Configurable#api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

  • encoded (Boolean) (defaults to: false)

    true: url already encoded, false: url needs encoding

Returns:

  • (Sawyer::Resource)


149
150
151
# File 'lib/looker-sdk/client.rb', line 149

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) ⇒ Sawyer::Resource

Make a HTTP GET request

Parameters:

  • url (String)

    The path, relative to LookerSDK::Configurable#api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

  • encoded (Boolean) (defaults to: false)

    true: url already encoded, false: url needs encoding

  • &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.

Returns:

  • (Sawyer::Resource)


100
101
102
# File 'lib/looker-sdk/client.rb', line 100

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) ⇒ Sawyer::Resource

Make a HTTP HEAD request

Parameters:

  • url (String)

    The path, relative to LookerSDK::Configurable#api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

  • encoded (Boolean) (defaults to: false)

    true: url already encoded, false: url needs encoding

Returns:

  • (Sawyer::Resource)


159
160
161
# File 'lib/looker-sdk/client.rb', line 159

def head(url, options = {}, encoded=false, &block)
  request :head, url, nil, parse_query_and_convenience_headers(options), encoded
end

#inspectString

Text representation of the client, masking tokens and passwords

Returns:

  • (String)


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/looker-sdk/client.rb', line 80

def inspect
  vars = instance_variables.reject {|v| [:@swagger, :@operations].include? v}
  inspected = "#{self.class.name}: attributes=\n#{vars.map {|v| "\t#{v}=#{instance_variable_get(v)}"}.join("\n")}>"
  # 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_errorStandardError

Response for last HTTP request

Returns:

  • (StandardError)


265
266
267
# File 'lib/looker-sdk/client.rb', line 265

def last_error
  @last_error if defined? @last_error
end

#last_responseSawyer::Response

Response for last HTTP request

Returns:



258
259
260
# File 'lib/looker-sdk/client.rb', line 258

def last_response
  @last_response if defined? @last_response
end

#looker_warnnil

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

Returns:

  • (nil)


297
298
299
300
301
# File 'lib/looker-sdk/client.rb', line 297

ruby2_keywords def looker_warn(*message)
  unless ENV['LOOKER_SILENT']
    warn message
  end
end

#make_agent(options = nil) ⇒ Sawyer::Agent

Hypermedia agent for the LookerSDK API (with specific options)

Returns:

  • (Sawyer::Agent)


200
201
202
203
204
205
206
207
# File 'lib/looker-sdk/client.rb', line 200

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.headers[:authorization] = "token #{@access_token}" if token_authenticated?
  end
end

#paginate(url, options = {}, &block) ⇒ Sawyer::Resource

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

Parameters:

  • url (String)

    The path, relative to LookerSDK::Configurable#api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

  • block (Block)

    Block to perform the data concatenation 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.

Returns:

  • (Sawyer::Resource)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/looker-sdk/client.rb', line 174

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) ⇒ Sawyer::Resource

Make a HTTP PATCH request

Parameters:

  • url (String)

    The path, relative to LookerSDK::Configurable#api_endpoint

  • data (String|Array|Hash) (defaults to: {})

    Body and optionally header params for request

  • options (Hash) (defaults to: {})

    Optional header params for request

  • encoded (Boolean) (defaults to: false)

    true: url already encoded, false: url needs encoding

  • &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.

Returns:

  • (Sawyer::Resource)


139
140
141
# File 'lib/looker-sdk/client.rb', line 139

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) ⇒ Sawyer::Resource

Make a HTTP POST request

Parameters:

  • url (String)

    The path, relative to LookerSDK::Configurable#api_endpoint

  • data (String|Array|Hash) (defaults to: {})

    Body and optionally header params for request

  • options (Hash) (defaults to: {})

    Optional header params for request

  • encoded (Boolean) (defaults to: false)

    true: url already encoded, false: url needs encoding

  • &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.

Returns:

  • (Sawyer::Resource)


113
114
115
# File 'lib/looker-sdk/client.rb', line 113

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) ⇒ Sawyer::Resource

Make a HTTP PUT request

Parameters:

  • url (String)

    The path, relative to LookerSDK::Configurable#api_endpoint

  • data (String|Array|Hash) (defaults to: {})

    Body and optionally header params for request

  • options (Hash) (defaults to: {})

    Optional header params for request

  • encoded (Boolean) (defaults to: false)

    true: url already encoded, false: url needs encoding

  • &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.

Returns:

  • (Sawyer::Resource)


126
127
128
# File 'lib/looker-sdk/client.rb', line 126

def put(url, data = {}, options = {}, encoded=false, &block)
  request :put, url, data, parse_query_and_convenience_headers(options), encoded, &block
end

#rootSawyer::Resource

Fetch the root resource for the API

Returns:

  • (Sawyer::Resource)


219
220
221
# File 'lib/looker-sdk/client.rb', line 219

def root
  get URI(api_endpoint).path.sub(/\/$/,'')
end

#same_options?(opts) ⇒ Boolean

Compares client options to a Hash of requested options

Parameters:

  • opts (Hash)

    Options to compare with current client options

Returns:

  • (Boolean)


73
74
75
# File 'lib/looker-sdk/client.rb', line 73

def same_options?(opts)
  opts.hash == @original_options.hash
end