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
|
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)
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 |
---|---|
|
required Name of a configured client. |
|
|
|
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) /]
Example with custom values
Client definition:
baseUrl: http://localhost:8081
restCalls:
random:
method: post
body: {"name": "{name}", "email": "{email}"}
entityClass: com.fasterxml.jackson.databind.JsonNode
responseContentType: application/json
path: /alumni
headers:
Content-Type: "application/json; charset=UTF-8"
Page template definition and script:
title: My Home Page
templateScript: /my-light-module/templates/pages/myPage_restfn.ftl
renderType: freemarker
visible: true
<!doctype html>
<html lang="${cmsfn.language()}">
<head>
[@cms.page /]
<meta charset="utf-8">
</head>
[#assign customValues = {"name": "JohnDoe", "email": "johndoe@example.com"} /]
[#assign post = restfn.call("fooClient-restfn", "random", customValues) /]
<h1>${post}</h1>
<body>
<h1>Hello, world!</h1>
</body>
</html>
The return type of |
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
Call proxy
Invokes a method on a proxy with given parameters.
Returns a result according to the return type of the method called.
Arguments
Argument | Description |
---|---|
|
required Instance of a declared proxy. |
|
required Name of the method of the proxy. |
|
required A varargs parameter. In FreeMarker context, just pass an array of arguments. |
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 |
---|---|
|
required Name of a configured client. |
|
required Name of a proxy class. |
|
required Name of the method. |
|
required A varargs parameter. In FreeMarker context, just pass an array of arguments. |
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.
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.