Hi @rajakumare1 ,
Consider below approach -
At a page level, preferably in a Page component Model you can make an API call and save the API response in the request object attribute. Specifically like,
String apiResponse = customService.getResponse();
// Set the api response to the request object attribute
request.setAttribute("result", apiResponse);
To reuse this in component, read the attribute value from the request object itself in the corresponding Sling Model for the component. Sample code below:
// To get the request object
@SlingObject
SlingHttpServletRequest request;
// To read the API response
String apiResponse = request.getAttribute("result");
This way, your API will be invoked only once, as the Page component model executes only once per page load and the response can then be reused across any component on the page.
Hope this helps!
Thanks,
Fani