Invoking script in composition API

Hello,

what is the right approach to invoke a script that's stored in the database within the Composition API? Does the API Server has any similar method for invoking scripts as the App Server has?

Best regards

Parents
  • Hi Daniel,

    You have to wrap the script call inside an API endpoint. Here is the code for a sample endpoint.

        // This class shows how to expose a script call through the API.
        public class Scripts : IApiProvider
        {
            public void Build(IApiBuilder builder)
            {
                // This method takes two string parameters and returns a string.
                // For demonstration purposes, the method simply calls the script
                // VI_BuildInitials.
    
                builder.AddMethod(Method.Define("getinitials/{firstname}/{lastname}")
                    .WithParameter("firstname", typeof(string), isInQuery: false)
                    .WithParameter("lastname", typeof(string), isInQuery: false)
                    .HandleGet(qr =>
                    {
                        // Setup the script runner
                        var scriptClass = qr.Session.Scripts().GetScriptClass(ScriptContext.Scripts);
                        var runner = new ScriptRunner(scriptClass, qr.Session);
    
                        // Add any script input parameters to this array.
                        // In this example, the script parameters are defined as
                        // URL parameters, and their values must be supplied
                        // by the client. This does not have to be the case.
                        var parameters = new object[]
                        {
                            qr.Parameters.Get<string>("firstname"),
                            qr.Parameters.Get<string>("lastname")
                        };
    
                        // This assumes that the script returns a string.
                        return runner.Eval("VI_BuildInitials", parameters) as string;
                    }));
            }
        }

  • Hi Hanno,

    is it also possible to transfer the parameters of the script via a request body as JSON input?

    Best regards

  • Hi Michael,

    Sure. You have to define a data type. In this case it will be a very simple class:

    public class InputDataObject
    {
        public string FirstName { get; set;}
        
        public string LastName { get; set; }
        
    }

    Then you can use the type as an input type as follows:

                builder.AddMethod(Method.Define("callscript")
                    .Handle<InputDataObject>("POST",
                        (posted, request) =>
                        {
                            // "posted" contains the deserialized JSON data
                            // --> call the script
                        }));
                        

    The server takes care of the JSON deserialization.

    Regards,

    Hanno

Reply Children
No Data