Invoking External Services/API

Hi!

Is there a way for me to leverage on the CUBA platform to rapidly build-up UI components and wire-up the respective action buttons to invoke external API calls (eg: where there are existing REST APIs being exposed by other legacy applications in the enterprise)?

Also, can this be done without the need to configure any data store/persistence for my new application built on CUBA platform (since the persistence tier in this enterprise setup is effectively residing on the other legacy applications)

Hi, Stan

I haven’t yet used REST API with Cuba Platform but I’m intending to do it soon. For this reason I discovered this documentation that could help you :
Hi, Stan

https://doc.cuba-platform.com/restapi-7.2/?_ga=2.180741359.91455702.1584634912-1236808527.1584634912

Regards,
Xavier

Thanks Xavier!

I’ve had a quick browse through on the documentation - it looks to concentrate on being able to provide REST APIs to allow other applications to interact with our CUBA platform-based application. I’m actually looking for the reverse scenario - where my UI components built on CUBA platform interacts with other applications via APIs.

Hi Stan,

You can create a set of non-persistent entities corresponding to the data model of your external services - see “Not persistent” option in the “Entity type” field of the “New CUBA Entity” dialog. Then use loadDelegate for data loaders and commitDelegate for DataContext to load and save data using your services instead of CUBA middleware.

A simpler solution would be using KeyValueEntity, below is an example of loading data from a REST service and displaying it in a table:

<window xmlns="http://schemas.haulmont.com/cuba/screen/window.xsd"
        caption="msg://caption"
        messagesPack="com.company.demo.web.screens">
    <data readOnly="true">
        <keyValueCollection id="namesDc">
        </keyValueCollection>
    </data>
    <layout>
        <dataGrid id="namesDataGrid" dataContainer="namesDc" height="100%" width="100%">
        </dataGrid>
    </layout>
</window>
@UiController("demo_DemoScreen")
@UiDescriptor("demo-screen.xml")
public class DemoScreen extends Screen {

    @Inject
    private KeyValueCollectionContainer namesDc;
    @Inject
    private DataGrid namesDataGrid;

    @Subscribe
    public void onInit(InitEvent event) {
        String jsonString = getJsonString();
        JSONArray jsonArray = new JSONArray(jsonString);

        if (jsonArray.length() > 0) {
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            // initialize container properties and grid columns
            for (String key : jsonObject.keySet()) {
                namesDc.addProperty(key);
                namesDataGrid.addColumn(key, namesDc.getEntityMetaClass().getPropertyPath(key));
            }
        }

        // go through your data set
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            // create KeyValueEntity instances
            KeyValueEntity entity = new KeyValueEntity();
            for (String key : jsonObject.keySet()) {
                // set values
                entity.setValue(key, jsonObject.getString(key));
            }
            // add entities to the container
            namesDc.getMutableItems().add(entity);
        }
    }

    private String getJsonString() {
        String json;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("https://uinames.com/api/?region=England&amount=25");
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            json = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return json;
    }
}

Hello,
Thank you so much for this solution. It would be really helpful if you can provide any sample project for this.

Thank You!