Class: Superset::Client

Inherits:
Happi::Client
  • Object
show all
Includes:
Superset::Credential::ApiUser
Defined in:
lib/superset/client.rb

Constant Summary collapse

CSRF_PROTECTED_METHODS =

Superset enforces CSRF on state-changing requests once WTF_CSRF_ENABLED is on; GETs are never CSRF-checked. Bearer-token auth is not sufficient, so these verbs must carry an X-CSRFToken header (see #call / #csrf_token).

%i[post put patch delete].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Superset::Credential::ApiUser

#credentials

Constructor Details

#initializeClient

Returns a new instance of Client.



14
15
16
17
# File 'lib/superset/client.rb', line 14

def initialize
  @authenticator = Superset::Authenticator.new(credentials)
  super(log_level: :debug, host: superset_host)
end

Instance Attribute Details

#authenticatorObject (readonly)

Returns the value of attribute authenticator.



12
13
14
# File 'lib/superset/client.rb', line 12

def authenticator
  @authenticator
end

Instance Method Details

#access_tokenObject



19
20
21
# File 'lib/superset/client.rb', line 19

def access_token
  @access_token ||= authenticator.access_token
end

#call(method, url, params = {}) ⇒ Object

All verbs funnel through Happi::Client#call. Before any state-changing request, attach a CSRF token; fetching one also sets the Flask session cookie that the token is validated against, which the cookie jar on this connection replays on the write.



31
32
33
34
# File 'lib/superset/client.rb', line 31

def call(method, url, params = {})
  set_csrf_token if CSRF_PROTECTED_METHODS.include?(method)
  super
end

#put(resource, params = {}) ⇒ Object

TODO: Happi has not got a put method yet



37
38
39
40
# File 'lib/superset/client.rb', line 37

def put(resource, params = {})
  call(:put, url(resource), param_check(params))
    .body.with_indifferent_access
end

#raise_error(response) ⇒ Object

TODO: Happi is not surfacing the errors correctly overriding raise_error for now

Raises:

  • (errors[response.status])


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/superset/client.rb', line 43

def raise_error(response)
  message =
  if response.body['errors']
    response.body['errors']
  else
    response.body
  end

  puts "API Error: #{message}"  # display the error message for console debugging
  # binding.pry                 # helpfull to debug the response

  raise errors[response.status].new(message, response)  # message is not being surfaced from Happi correctly, :(
end

#superset_hostObject



23
24
25
# File 'lib/superset/client.rb', line 23

def superset_host
  @superset_host ||= authenticator.superset_host
end