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

Instance Attribute Details

#dynamicObject

Returns the value of attribute dynamic.



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

def dynamic
  @dynamic
end

Instance Method Details

#clear_swaggerObject



47
48
49
# File 'lib/looker-sdk/client/dynamic.rb', line 47

def clear_swagger
  @swagger = @operations = nil
end

#load_swaggerObject



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

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
    # 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?
    if @last_error
      raise @last_error
    else
      raise "Load of swagger.json failed."
    end
  end

  @swagger
end


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

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



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

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

  if !@swagger && @lazy_swagger
    load_swagger
  end

  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].to_sym, {: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)


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

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

#try_load_swaggerObject



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

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