Hi All,
I am new to the AEM World. What is the best way to make a api all on page load and use the data from the api on all the components in the page.
Note: Currently I have a OSGI Service and a model that is invokes the OSGi call. Not sure what is the correct design approach.
Thanks,
Rajakumar
Solved! Go to Solution.
Views
Replies
Total Likes
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
Hi @rajakumare1,
I am not sure if you are wanting to trigger the API call on a particular page or every page with particular resource type.
You can do the below things:
Hope this helps.
Thanks,
Kiran Vedantam.
Thanks for your Response.
But how do I share the data accress multiple component in the same page.
all components use the data from the same api call which again is calling a 3rd party. We dont want to make the same call repeatedly for each component
You can do multiple things for it.
But I am still not clear why every component needs the data. In that case why cant we create a single component by including the resources of different components.
Thanks,
Kiran Vedantam.
Thanks again. The site we are designing is like web app. We have multiple components like Tracking Information, Product Information and so on. The single 3rd party api call has all the details.
I have not gone in the approach of storing in jcr node as api call is real time data for each unique page load
I have a use case for doing it in the third way. Can you share more details?
Views
Replies
Total Likes
Hi @rajakumare1
There are couple of approaches and how frequently the data is going to change is important thing.
Approach 1:
Approach 2:
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
Views
Likes
Replies