Tutorial

Scripting-on-the-Cas-Server

Introduction

This section shows the use of runCasl method of restalib to run your casl scripts on the server.

Flow

The flow of a typical program is shown below.

casl run

A typical program using this functionality is shown below The two key functions are:

  • casSetup - creates a cas session. The only argument to this method is restaf store
  • caslRun - execute the casl program. The arguments to this function are:
    • store - the restaf store
    • session - the session obtained thru casSetup
    • casl - A string with the casl code to be executed
    • args - The args parameter( a standard js object) is passed to the casl program as a casl dictionary named args. This is a friendly way to pass parameters to your casl programs.

The caslRun executes sccasl.runcasl action on the server. The returned value is the typical response from a sccasl.runcasl action. If the status in the returned object is non-zero caslRun will throw an error that you can handle in your catch block. The key elements of the response are:

  • result.log - is the log lines
  • result.disposition - the standard cas disposition object
  • result.results - the information you passed to the send-response function in the casl program
let store = restaf.initStore();                                                     /* (1)   */
store.logon(payload)
   .then ( () => example())
   .catch(err) => console.log(e)

async function example() {
  let {session} = await restaflib.casSetup (store);                                  /* (2) */
  /* Recommendation: Store the casl code in a repository */
  let casl = \`                                                                       /* (3) */
          print 'input values';
          print _args_;
           action datastep.runcode/ single='YES' code = 'data casuser.a; x=1; run;';
           action table.fetch r=r1/
              table= { caslib= 'casuser', name= 'a' } ;
              run;
              action datastep.runcode/ single='YES' code = 'data casuser.b; y=1; run;';
            action table.fetch r=r2/
              table= { caslib= 'casuser', name= 'b' } ;
              run;
           c = {a=10, b=20}; /* just to show that anything can be returned */
           send_response({a=r1, b=r2, c=c});
        \`;
  let args   = {a: "this is arguments", b: "more data"};

  let result = await restaflib.caslRun (store, session, casl, args);                /* (4) */
  console.log(JSON.stringify(result, null,4));                                      /* (5) */
  await store.apiCall(session.links('delete'));
 }

Setup

Web Applications

Include the following two script tags.

    <script src="https://unpkg.com/@sassoftware/restaf@next"></script>

    <script src="https://unpkg.com/@sassoftware/restaflib@next"></script>

Two globals restaf and restaflib will be available for use in your script tags.

Nodejs application

Install restaf and restaflib using the following command

    npm install @sassoftware/restaf@next @sassoftware/restaflib@next