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, #invoke, #load_swagger, #method_link, #method_missing, #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:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class LookerSDK::Client::Dynamic

Instance Method Details

#access_token=(value) ⇒ Object

Set OAuth access token for authentication

Parameters:

  • value (String)

    Looker OAuth access token



270
271
272
273
# File 'lib/looker-sdk/client.rb', line 270

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)


210
211
212
# File 'lib/looker-sdk/client.rb', line 210

def agent
  @agent ||= make_agent
end

#aliveObject

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

Returns:

  • http status code



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

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)


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

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)


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

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



278
279
280
281
# File 'lib/looker-sdk/client.rb', line 278

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



286
287
288
289
# File 'lib/looker-sdk/client.rb', line 286

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)


147
148
149
# 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) ⇒ 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)


98
99
100
# 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) ⇒ 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)


157
158
159
# 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

#inspectString

Text representation of the client, masking tokens and passwords

Returns:

  • (String)


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

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)


263
264
265
# File 'lib/looker-sdk/client.rb', line 263

def last_error
  @last_error if defined? @last_error
end

#last_responseSawyer::Response

Response for last HTTP request

Returns:



256
257
258
# File 'lib/looker-sdk/client.rb', line 256

def last_response
  @last_response if defined? @last_response
end

#looker_warn(*message) ⇒ nil

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

Returns:

  • (nil)


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

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)


198
199
200
201
202
203
204
205
# 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.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)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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) ⇒ 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)


137
138
139
# 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) ⇒ 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)


111
112
113
# 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) ⇒ 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)


124
125
126
# 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

#rootSawyer::Resource

Fetch the root resource for the API

Returns:

  • (Sawyer::Resource)


217
218
219
# File 'lib/looker-sdk/client.rb', line 217

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)


71
72
73
# File 'lib/looker-sdk/client.rb', line 71

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