restfn

restfn templating functions give access to REST clients configured with the REST Client module.

accessing 3rd party systems with REST

Using Magnolia REST clients with restfn in FTL template scripts could slow or even block the rendering of the scripts, because the call is synchronous and waits till the request returns. Before an actual implementation, always consider at least the following:

  • The reliability of the endpoint.

  • Implementing restfn in an asynchronous way via a client side request.

  • Accessing the endpoint directly with client side code.

  • How any of your page caching configurations will affect content delivered from restfn.

Call response

Invokes a call for a full response to a configured REST client, with optional assignment of custom template values for request parameters or body, and with optional specific error handler.

Returns a full response as a map of clients and call names, with error handlers and/or custom template values, if available.

Method signature

Response callForResponse(String restClientName, String restCallName, Map<String, Object> customValues, RestClientExceptionHandler errorHandler)

Arguments

Argument Description

restClientName

required

Name of a configured client.

restCallName

required

Name of a configured call.

customValues

optional

A map of custom values passed to the endpoint with the call.

errorHandler

optional

Specification of a handler for errors, for example {"code": 404}.

Usage

restfn.callForResponse(restClientName, restCallName, customValues, errorHandler)

Call client

Invokes a call to a configured REST client with optional assignment of custom template values for request parameters or body.

Returns an object. The actual type of the returned object conforms with the entityClass configured for the REST call.

Method signature

Object call(String restClientName, String restCallName, Map<String, Object> customValues)

Arguments

Argument Description

restClientName

required

Name of a configured client.

restCallName

required

Name of a configured call.

customValues

optional

A map of custom values passed to the endpoint with the call.

Usage

restfn.call(restClientName, restCallName, customValues)

[#assign firstPost = restfn.call("restfn-posts", "single")]

[#-- with a custom value --]
[#assign customValues = {"id":"2"} /]
[#assign secondPost = restfn.call("restfn-posts", "single", customValues) /]

The form of the restCallName

By default, the name is mapped to this pattern: method:/path/pathAction. An operationId, a unique string used to identify the operation in the OpenAPI specification, is an optional alias for the call name.

In the following configuration snippet, an operationId has been set to findByStatus:

paths:
  /pet/findByStatus:
    get:
      operationId: findByStatus

You could then invoke the call using not only

restfn.invoke("get:/pet/findByStatus", "available")

but also with

 restfn.invoke("findByStatus", "available")

Get proxy implementation

Obtains a client proxy implementation for a given client and configured proxy class.

Returns an object mapping each client to its proxy.

Method signature

Object getProxy(String restClientName, String proxyClassName)

Arguments

Argument Description

restClientName

required

Name of a configured client.

proxyClassName

required

Name of a proxy class.

Usage

restfn.getProxy(restClientName, proxyClassName)

Call proxy

Invokes a method on a proxy with given parameters.

Returns a result according to the return type of the method called.

Method signature

Object call(Object proxy, String methodName, Object…​ args)

Arguments

Argument Description

proxy

required

Instance of a declared proxy.

methodName

required

Name of the method of the proxy.

args

required

A varargs parameter. In FreeMarker context, just pass an array of arguments.

Usage

restfn.call(proxy, methodName, args)

Call client with a proxy implementation

Invokes a method on a client with a specified proxy implementation and with given parameters.

Returns a result according to the return type of the method called.

Method signature

Object call(String restClientName, String proxyClass, String methodName, Object…​ args)

Arguments

Argument Description

restClientName

required

Name of a configured client.

proxyClass

required

Name of a proxy class.

methodName

required

Name of the method.

args

required

A varargs parameter. In FreeMarker context, just pass an array of arguments.

Usage

restfn.call(restClientName, proxyClass, methodName, args)

Convert JsonNode to Map

Converts a JsonNode object to a hash map. Use either this or the asList() function depending on what type of JSON the API your query returns.

Returns a hash map.

Method signature

Map<String, Object> asMap(jsonNode)

Arguments

Argument Description

jsonNode

required

Variable of type com.fasterxml.jackson.databind.JsonNode.

Usage

restfn.asMap(jsonNode)

[#assign colombia = restfn.asMap(restfn.call("countries", "colombia"))]

[#-- Note: Provided the restfn.call("countries", "colombia") is defined
using the com.fasterxml.jackson.databind.JsonNode entity class.--]

Convert JsonNode to List

Converts a JsonNode object to an array list. Use either this or the asMap() function depending on what type of JSON the API your query returns.

Returns an array list.

Method signature

List<Object> asList(jsonNode)

Arguments

Argument Description

jsonNode

required

Variable of type com.fasterxml.jackson.databind.JsonNode.

Usage

restfn.asList(jsonNode)

[#assign countriesList = restfn.asList(restfn.call("countries", "allCountriesArray"))]

[#-- Note: Provided the restfn.call("countries", "allCountriesArray") is defined
using the com.fasterxml.jackson.databind.JsonNode entity class.--]
Feedback