Module: LookerSDK::Client::Dynamic

Included in:
LookerSDK::Client
Defined in:
lib/looker-sdk/client/dynamic.rb

Constant Summary collapse

@@sharable_operations =

If a given client is created with ‘:shared_swagger => true’ then it will try to use a globally sharable @@operations hash built from one fetch of the swagger.json for the given api_endpoint. This is an optimization for the cases where many sdk clients get created and destroyed (perhaps with different access tokens) while all talking to the same endpoints. This cuts down overhead for such cases considerably.

Hash.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



104
105
106
107
# File 'lib/looker-sdk/client/dynamic.rb', line 104

def method_missing(method_name, *args, &block)
  entry = find_entry(method_name) || (return super)
  invoke_remote(entry, method_name, *args, &block)
end

Instance Attribute Details

#dynamicObject

Returns the value of attribute dynamic.



30
31
32
# File 'lib/looker-sdk/client/dynamic.rb', line 30

def dynamic
  @dynamic
end

Instance Method Details

#clear_swaggerObject



45
46
47
# File 'lib/looker-sdk/client/dynamic.rb', line 45

def clear_swagger
  @swagger = @operations = nil
end

#invoke(method_name, *args, &block) ⇒ Object

Callers can explicitly ‘invoke’ remote methods or let ‘method_missing’ do the trick. If nothing else, this gives clients a way to deal with potential conflicts between remote method names and names of methods on client itself.



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

def invoke(method_name, *args, &block)
  entry = find_entry(method_name) || raise(NameError, "undefined remote method '#{method_name}'")
  invoke_remote(entry, method_name, *args, &block)
end

#load_swaggerObject



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

def load_swagger
  # We only need the swagger if we are going to be building our own 'operations' hash
  return if shared_swagger && @@sharable_operations[api_endpoint]
  # First, try to load swagger.json w/o authenticating
  @swagger ||= without_authentication { try_load_swagger }

  unless @swagger
    # capture the bits we may need later, avoiding potential buffer reuse in last_response between requests
    response_wo_auth_status = last_response&.status
    response_wo_auth_data = last_response&.data

    # try again, this time with authentication
    @swagger = try_load_swagger
  end

  # in unit tests, @swagger may be nil and last_response nil because no HTTP request was made
  if @swagger.nil? && (response_wo_auth_status || last_response)
    msg = "Load of swagger.json failed. "
    msg << "Without authentication HTTP response status: (#{response_wo_auth_status}) data: #{response_wo_auth_data}. " if response_wo_auth_status
    msg << "WITH authentication HTTP response status: (#{last_response.status}) data: #{last_response.data}" if last_response
    looker_warn(msg)
  end

  @swagger
end


91
92
93
94
# File 'lib/looker-sdk/client/dynamic.rb', line 91

def method_link(entry)
  uri = URI.parse(api_endpoint)
  "#{uri.scheme}://#{uri.host}:#{uri.port}/api-docs/index.html#!/#{entry[:info][:tags].first}/#{entry[:info][:operationId]}" rescue "http://docs.looker.com/"
end

#operationsObject



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

def operations
  return @@sharable_operations[api_endpoint] if shared_swagger && @@sharable_operations[api_endpoint]

  return nil unless @swagger
  @operations ||= Hash[
    @swagger[:paths].map do |path_name, path_info|
      path_info.map do |method, route_info|
        route = @swagger[:basePath].to_s + path_name.to_s
        [route_info[:operationId], {:route => route, :method => method, :info => route_info}]
      end
    end.reduce(:+)
  ].freeze

  shared_swagger ? (@@sharable_operations[api_endpoint] = @operations) : @operations
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/looker-sdk/client/dynamic.rb', line 109

def respond_to?(method_name, include_private=false)
  !!find_entry(method_name) || super
end

#try_load_swaggerObject



32
33
34
35
# File 'lib/looker-sdk/client/dynamic.rb', line 32

def try_load_swagger
  resp = get('swagger.json') rescue nil
  resp && last_response && last_response.status == 200 && last_response.data && last_response.data.to_attrs
end