Toegang krijgen tot de Apache Superset API's vanuit Invantive PSQL

In dit onderwerp wordt uitgelegd hoe de Apache Superset API’s vanuit Invantive PSQL benaderd kunnen worden. Met behulp van Invantive PSQL kunnen de rijen van een dataset worden opgehaald.

De gebruikersinterface van Superset maakt gebruik van de Superset API’s (zie https://superset.apache.org/docs/api/). Door gebruik te maken van de Chrome Devtools of iets vergelijkbaars is het vrij eenvoudig om te bepalen welke API-aanroepen nodig zijn.

Query uitvoeren

Gebruik de volgende aanroep om een query uit te voeren. Eerst wordt een toegangstoken opgehaald, vervolgens het CSRF-token en de sessiecookie. Hiermee kan een willekeurige query worden uitgevoerd:

declare
  --
  -- Superset server: protocol, host and port.
  --
  g_server_url   varchar2 := 'https://superset.acme.com:8088'
  --
  -- Superset user name.
  --
  g_username     varchar2 := 'john.doe@acme.com';
  --
  -- Superset user password.
  --
  g_password     varchar2 := 'secret';
  --
  -- Query to run.
  --
  g_query        varchar2 := 'SELECT * FROM `myschema`.tablename LIMIT 5';
  --
  l_access_token varchar2;
  l_csrf_token   varchar2;
  l_query        varchar2;
  l_xml          varchar2;
  l_download_uid varchar2;
  l_cookie_value varchar2;
begin
  select jte.access_token
  into   l_access_token
  from   HttpDownload@DataDictionary
         ( url => g_server_url || '/api/v1/security/login'
         , method => 'POST'
         , acceptMimeType => 'application/json'
         , contentType => 'application/json'
         , textPayload 
           => '{' 
           || jsonelement('username', g_username, false) 
           || ',' || jsonelement('provider', 'db') 
           || ',' || jsonelement('refresh', true) 
           || ',' || jsonelement('password', g_password, false) 
           || '}'
         ) htp
  join   jsontable
         ( ''
           passing htp.contents_char
           columns access_token varchar2 path 'access_token'
           ,       refresh_token varchar2 path 'refresh_token'
        ) jte
  ;
  --
  -- Get CSRF token.
  --
  -- The cookie with name 'session' is also needed.
  --
  select jte.result
  ,      htp.uid
  into   l_csrf_token
  ,      l_download_uid
  from   HttpDownload@DataDictionary
         ( url => g_server_url || '/api/v1/security/csrf_token/'
         , method => 'GET'
         , acceptMimeType => 'application/json'
         , contentType => 'application/json'
         , headers => 'Authorization=Bearer ' || l_access_token
         ) htp
  join   jsontable
         ( ''
           passing htp.contents_char
           columns result varchar2 path 'result'
        ) jte
  ;
  --
  -- Get the session cookie.
  --
  select value
  into   l_cookie_value
  from   HttpDownloadCookiesHistory@datadictionary
  where  download_uid = l_download_uid
  and    name = 'session'
  ;
  --
  -- Construct query.
  --
  l_query :=
    '{'
    || jsonelement('client_id', null, false)
    || ','
    || jsonelement('ctas_method', null, false)
    || ','
    || jsonelement('database_id', 1, false)
    || ','
    || jsonelement('expand_data', true, false)
    || ','
    || jsonelement('json', true, false)
    || ','
    || jsonelement('queryLimit', 0, false)
    || ','
    || jsonelement('runAsync', false, false)
    || ','
    || jsonelement('schema', null, false)
    || ','
    || jsonelement('select_as_cta', false, false)
    || ','
    || jsonelement('sql', g_query, false)
    || ','
    || jsonelement('sql_editor_id', null, false)
    || ','
    || jsonelement('tab', null, false)
    || ','
    || jsonelement('templateParams', null, false)
    || ','
    || jsonelement('tmp_table_name', null, false)
    || '}'
  ;
  dbms_output.put_line('*** Credentials ***');
  dbms_output.put_line('Access token: ' || l_access_token);
  dbms_output.put_line('CSRF token: ' || l_csrf_token);
  dbms_output.put_line('Cookie session: ' || l_cookie_value);
  dbms_output.put_line('Query: ' || l_query);
  --
  select *
  into   l_xml
  from   HttpDownload@DataDictionary
         ( url => g_server_url ||'/api/v1/sqllab/execute/'
         , method => 'POST'
         , acceptMimeType => 'application/json'
         , contentType => 'application/json'
         , headers
           => 'Authorization=Bearer ' || l_access_token
           || '&'
           || 'X-CSRF-Token=' || l_csrf_token
         , cookies => 'session=' || l_cookie_value
         , textpayload => l_query
         ) htp
  join   jsontable
         ( 'data[*]'
           passing htp.contents_char
           columns id           varchar2 path 'id'
           ,       name         varchar2 path 'name'
           ,       registerDate datetime path 'registerDate'
        ) jte
  for xml auto
  ;
  dbms_output.put_line('Results for example: ' || l_xml);
end;

Let op: vanaf release 24.0 beschikt PSQL niet over een yamltable om YAML te parsen of for yaml om YAML te exporteren.