Class: LookerSDK::Client
- Inherits:
-
Object
- Object
- LookerSDK::Client
- Includes:
- Authentication, Dynamic, Configurable
- Defined in:
- lib/looker-sdk/client.rb,
lib/looker-sdk/client/dynamic.rb
Overview
Client for the LookerSDK API
Defined Under Namespace
Modules: Dynamic Classes: Serializer, StreamingClient
Constant Summary collapse
- CONVENIENCE_HEADERS =
Set.new([:accept, :content_type])
Instance Attribute Summary
Attributes included from 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
-
#access_token=(value) ⇒ Object
Set OAuth access token for authentication.
-
#agent ⇒ Sawyer::Agent
Cached Hypermedia agent for the LookerSDK API (with default options).
-
#alive ⇒ Object
Is the server alive (this can be called w/o authentication).
-
#alive? ⇒ Boolean
Are we connected to the server? - Does not attempt to authenticate.
-
#authenticated? ⇒ Boolean
Are we connected and authenticated to the server?.
-
#client_id=(value) ⇒ Object
Set OAuth app client_id.
-
#client_secret=(value) ⇒ Object
Set OAuth app client_secret.
-
#delete(url, options = {}, encoded = false, &block) ⇒ Sawyer::Resource
Make a HTTP DELETE request.
-
#get(url, options = {}, encoded = false, &block) ⇒ Sawyer::Resource
Make a HTTP GET request.
-
#head(url, options = {}, encoded = false, &block) ⇒ Sawyer::Resource
Make a HTTP HEAD request.
-
#initialize(opts = {}) {|_self| ... } ⇒ Client
constructor
A new instance of Client.
-
#inspect ⇒ String
Text representation of the client, masking tokens and passwords.
-
#last_error ⇒ StandardError
Response for last HTTP request.
-
#last_response ⇒ Sawyer::Response
Response for last HTTP request.
-
#looker_warn(*message) ⇒ nil
Wrapper around Kernel#warn to print warnings unless LOOKER_SILENT is set to true.
-
#make_agent(options = nil) ⇒ Sawyer::Agent
Hypermedia agent for the LookerSDK API (with specific options).
-
#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.
-
#patch(url, data = {}, options = {}, encoded = false, &block) ⇒ Sawyer::Resource
Make a HTTP PATCH request.
-
#post(url, data = {}, options = {}, encoded = false, &block) ⇒ Sawyer::Resource
Make a HTTP POST request.
-
#put(url, data = {}, options = {}, encoded = false, &block) ⇒ Sawyer::Resource
Make a HTTP PUT request.
-
#root ⇒ Sawyer::Resource
Fetch the root resource for the API.
-
#same_options?(opts) ⇒ Boolean
Compares client options to a Hash of requested options.
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.
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 = .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
270 271 272 273 |
# File 'lib/looker-sdk/client.rb', line 270 def access_token=(value) reset_agent @access_token = value end |
#agent ⇒ Sawyer::Agent
Cached Hypermedia agent for the LookerSDK API (with default options)
210 211 212 |
# File 'lib/looker-sdk/client.rb', line 210 def agent @agent ||= make_agent end |
#alive ⇒ Object
Is the server alive (this can be called w/o authentication)
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.
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?
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
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
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
147 148 149 |
# File 'lib/looker-sdk/client.rb', line 147 def delete(url, = {}, encoded=false, &block) request :delete, url, nil, parse_query_and_convenience_headers(), encoded, &block end |
#get(url, options = {}, encoded = false, &block) ⇒ Sawyer::Resource
Make a HTTP GET request
98 99 100 |
# File 'lib/looker-sdk/client.rb', line 98 def get(url, = {}, encoded=false, &block) request :get, url, nil, parse_query_and_convenience_headers(), encoded, &block end |
#head(url, options = {}, encoded = false, &block) ⇒ Sawyer::Resource
Make a HTTP HEAD request
157 158 159 |
# File 'lib/looker-sdk/client.rb', line 157 def head(url, = {}, encoded=false, &block) request :head, url, nil, parse_query_and_convenience_headers(), encoded end |
#inspect ⇒ String
Text representation of the client, masking tokens and passwords
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_error ⇒ StandardError
Response for last HTTP request
263 264 265 |
# File 'lib/looker-sdk/client.rb', line 263 def last_error @last_error if defined? @last_error end |
#last_response ⇒ Sawyer::Response
Response for last HTTP request
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.
295 296 297 298 299 |
# File 'lib/looker-sdk/client.rb', line 295 def looker_warn(*) unless ENV['LOOKER_SILENT'] warn end end |
#make_agent(options = nil) ⇒ Sawyer::Agent
Hypermedia agent for the LookerSDK API (with specific options)
198 199 200 201 202 203 204 205 |
# File 'lib/looker-sdk/client.rb', line 198 def make_agent( = nil) ||= Sawyer::Agent.new(api_endpoint, ) 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.
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, = {}, &block) opts = parse_query_and_convenience_headers() 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
137 138 139 |
# File 'lib/looker-sdk/client.rb', line 137 def patch(url, data = {}, = {}, encoded=false, &block) request :patch, url, data, parse_query_and_convenience_headers(), encoded, &block end |
#post(url, data = {}, options = {}, encoded = false, &block) ⇒ Sawyer::Resource
Make a HTTP POST request
111 112 113 |
# File 'lib/looker-sdk/client.rb', line 111 def post(url, data = {}, = {}, encoded=false, &block) request :post, url, data, parse_query_and_convenience_headers(), encoded, &block end |
#put(url, data = {}, options = {}, encoded = false, &block) ⇒ Sawyer::Resource
Make a HTTP PUT request
124 125 126 |
# File 'lib/looker-sdk/client.rb', line 124 def put(url, data = {}, = {}, encoded=false, &block) request :put, url, data, parse_query_and_convenience_headers(), encoded, &block end |
#root ⇒ Sawyer::Resource
Fetch the root resource for the API
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
71 72 73 |
# File 'lib/looker-sdk/client.rb', line 71 def (opts) opts.hash == @original_options.hash end |