Custom Webservice Example

1 - Object configuration

Public webservice

- External Object : TrnCustomWebservice
   - Function : TrnCustomWebservice-R
      - Habilitation : PUBLIC (standard group in System module)

Authenticated webservice

- External Object : TrnCustomWebservice
   - Function : TrnCustomWebservice-R
      - Habilitation : TRN_ADMIN

2 - External Object Code ( => docs )

External Object > Edit Code

package com.simplicite.extobjects.Training;
import java.util.*;
import com.simplicite.util.*;
import com.simplicite.util.tools.*;
import org.json.JSONObject;
import com.simplicite.webapp.services.RESTServiceExternalObject;

public class TrnCustomWebservice extends RESTServiceExternalObject {
    private static final long serialVersionUID = 1L;

    @Override
    public Object get(Parameters params){
    	JSONObject data = new JSONObject();
    	data.put("webservice_status", "OK");
    	data.put("url_param", params.getParameter("url_param", "empty"));
        return data;
    }
    
    @Override
    public Object post(Parameters params){
    	JSONObject data = new JSONObject();
    	data.put("webservice_status", "OK");
    	data.put("posted_param", params.getParameter("post_param", "empty"));
        return data;
    }
}

3 - Testing

Use curl in a terminal.

Public webservice ( /ext )

> curl "<instance_url>/ext/TrnCustomWebservice?url_param=hello_world"
{"webservice_status":"OK","url_param":"hello_world"}
> curl --data "post_param=hello_world" "<instance_url>/ext/TrnCustomWebservice"
{"posted_param":"hello_world","webservice_status":"OK"}⏎ 

Authenticated webservice ( /api/ext )

See https://www.simplicite.io/resources/documentation/02-integration/services-auth.md

> curl -u <login>:<password> "<instance_url>/api/ext/TrnCustomWebservice?url_param=hello_world"
{"webservice_status":"OK","url_param":"hello_world"}
> curl -u <login>:<password> --data "post_param=hello_world" "<instance_url>/api/ext/TrnCustomWebservice"
{"posted_param":"hello_world","webservice_status":"OK"}

4 - Sample use with Simplicité Ajax API

If you already use the Ajax API, use the _call method to use the token and credentials from the Simplicité Object

// https://docs.simplicite.io/4.0/jsdoc-alpha/Simplicite.Ajax.html
var app = new Simplicite.Ajax("myapp", "api", "myuser", "mypassword");

// Params
var useAsync = true; // use async callback pattern
var url = "/api/ext/TrnExternalTreeView"; // authenticated webservice
var params = {post_param: "hello_world"}; // post params

// Call Webservice (POST requests only)
app._call(useAsync, url, params, function callback(rslt){
    console.log(JSON.stringify(rslt, null, 4));
});

Cf. https://docs.simplicite.io/documentation/02-integration/custom-services.md#java