The restfn templating functions allow you to obtain and use REST clients. This works only for clients that have been configured and declared with the RESTEasy client.
Line 1 accesses an instance of the client interface. In this example, it is of the IcndbService type.
The first parameter is the name of the client configuration node (icndbClient).
The second parameter is the fully qualified class name of the declared service interface (info.magnolia.documentation.modules.restclientexamples.client.IcndbService).
Line 2 calls the method #joke, which returns a JsonNode object.
This is a simplified example. See Jackson Javadoc for how to process JsonNode or assign a raw response
to a JavaScript variable to process JSON in a JavaScript context.
During installation of the magnolia-resteasy-client module, RestEasyClientModuleVersionHandler adds restfn to renderer configurations. Make sure that the renderType of your template definition points to a renderer that is configured to use restfn. See Configure the functions in a renderer for how to add templating functions to a renderer.
Java using client registry
To learn how to obtain and use a REST client, here is a model class that uses the same client as in the above example:
Add info.magnolia.rest.client.registry.RestClientRegistry as a
parameter to the constructor and assign it as a final instance variable.
Obtain a info.magnolia.resteasy.client.RestEasyClient object from RestClientRegistry. Note that #getRestClient requires the configured name of the rest client definition.
Create an instance of the declared interface IcndbService.
Call the declared method(s) on the service.
Component example
You have the option to configure components (filters) to be executed on every request. One use case could be authentication.
Client request filter
Here is an example filter that would handle authorization for each request.