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, #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
# 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?
  load_swagger
  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



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

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)


202
203
204
# File 'lib/looker-sdk/client.rb', line 202

def agent
  @agent ||= make_agent
end

#aliveObject

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

Returns:

  • http status code



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

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)


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

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)


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

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



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

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



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

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

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

Make a HTTP DELETE request

Parameters:

Returns:

  • (Sawyer::Resource)


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

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

#get(url, options = {}, &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

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


95
96
97
# File 'lib/looker-sdk/client.rb', line 95

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

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

Make a HTTP HEAD request

Parameters:

Returns:

  • (Sawyer::Resource)


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

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

#inspectString

Text representation of the client, masking tokens and passwords

Returns:

  • (String)


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

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_errorStandardError

Response for last HTTP request

Returns:

  • (StandardError)


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

def last_error
  @last_error if defined? @last_error
end

#last_responseSawyer::Response

Response for last HTTP request

Returns:



248
249
250
# File 'lib/looker-sdk/client.rb', line 248

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)


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

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)


190
191
192
193
194
195
196
197
# File 'lib/looker-sdk/client.rb', line 190

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 = {}, &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 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.

Returns:

  • (Sawyer::Resource)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/looker-sdk/client.rb', line 164

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 = {}, &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

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


131
132
133
# File 'lib/looker-sdk/client.rb', line 131

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

#post(url, data = {}, options = {}, &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

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


107
108
109
# File 'lib/looker-sdk/client.rb', line 107

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

#put(url, data = {}, options = {}, &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

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


119
120
121
# File 'lib/looker-sdk/client.rb', line 119

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

#rootSawyer::Resource

Fetch the root resource for the API

Returns:

  • (Sawyer::Resource)


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

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)


69
70
71
# File 'lib/looker-sdk/client.rb', line 69

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