Populating a ViewBox from a REST API

Description

The ViewBox control can be populated from a REST API data source by setting the Datasource type to "REST API". Use this option to make a REST API call to retrieve the JSON data to be rendered in the ViewBox.

images/viewboxrestapi_server.jpg

When you use the REST API option, you can specify if the REST call is made from the server or from the client.

When you make the call from the server, the Alpha Anywhere server will make the REST API call and then return the JSON data to the browser. If you later refresh the ViewBox, an Ajax callback is made to the server, the server executes the REST API call and then returns the JSON data to the client.

When you make the call from the client, the REST call is made directly from the client using the Javascript XHR object. The Alpha Anywhere server is not involved at all.

When you make the call from the server, you can specify a URL for the REST API end point that includes placeholders that will get resolved before the call is made. You can use controls on the UX and also arguments as placeholders. For example, say you have a textbox on your UX called customerId. You might specify the URL for the REST API as:

http://www.myapi.com/customers/{customerId}

When the API call is made, the placeholders in the URL are resolved. So, for example, if at the time the ViewBox is refreshed, the customerId control has a value of CUST_01, the URL for API call will be:

http://www.myapi.com/customers/CUST_01

Argument values are represented in the URL using :argument_name. For example, assume you have an Argument called CUSTOMER_ID. You could specify this URL for the API endpoint:

http://www.myapi.com/customers/:CUSTOMER_ID

In some cases, the API endpoint you call will be authenticated. For example, many API endpoints require an oAuth authentication or a "service account" login. You can specify the name of a Named Provider (defined by going to the Tools menu when the Web Control Panel has focus, and selecting Tools, Named Providers, or by opening the Project Properties dialog).

In the case where the call is made from the server, you can optionally specify an Xbasic function to post-process the JSON data that is returned by the REST API call before the data is returned to the client. In the case where the call is made from the client, you can optionally specify Javascript code to post-process the data before the ViewBox is populated.

In the case where the API call is made from the client, the URL is defined by Javascript code that must return the URL. This allows you to easily construct URLs that are dynamically computed, based on data in the component.

images/restapi_client.jpg