Forum Discussion

jordanepotter's avatar
12 years ago

How do you call an endpoint from freemarker?

We have a use case where we would like to have and enpoint processed on the serverside. It would help our presentation a lot. I know rest calls can be made in java to get the data back from an endpoint. Is there a way to do that in freemaker on lithium?

9 Replies

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)
    12 years ago

    Hi jordane,

     

    I don't think this is currently supported - I suggest raising this as an idea on the ideas board to see if this is something on the roadmap or wheter there are limitations around this.

     

    Thanks,

     

     

  • AdamN's avatar
    AdamN
    Khoros Oracle
    12 years ago

    I think this would be beneficial from a convenience perspective, but you can already accomplish basically the same thing. If you consider that an endpoint is essentially a stand-alone custom component, all you're really doing with an endpoint is rendering output from Freemarker code. If you find yourself wanting to use code "within the community" that you've already added to an endpoint, here's what I would suggest doing...

     

    1. Pull the common parts of your code into a Studio Macro. You can create Studio Macros on the same page as Endpoints in Studio. A Studio Macro is essentially just another custom component with the added benefit that it can be included in both custom components and Studio Endpoints (normal custom components cannot be included in Endpoints).
    2. From your endpoint, include your Studio Macro and reference the common code you added. You may want to create a Freemarker Macro (not to be confused with Studio Macro) or a Freemarker Function to make this easier.
    3. From your custom component, include your Studio Macro and reference the common code you added, similar to step number 2.

    I hope this helps!

  • Inactive User's avatar
    Inactive User
    12 years ago

    Could you help me wrap my head around this? If I want to make a server to server call with the endpoint, and then want to use the resulting response with FreeMarker in a component (ie. use returned JSON data in a component without using javscript)... would that be possible? Thanks

  • HaidongG's avatar
    HaidongG
    Lithium Alumni (Retired)
    12 years ago

    Hi jlutterloh, 

     

    sorry, as I know, it is not possible to do this in component (including endpoint result without JavaScript) at this moment.

     

    if you worries about the UI "flashing" effect by including endpoint result with JavaScript, you may want to hide the outter div first, call the endpoint, add generated html and show the div. for exmaple,  I created an Endpoint in Studio which reaches some 3-rd party API and generate a HTML, and in my another component, I just do something like this.

     

    <#assign kb_endpoint_url = "/plugins/custom/customer-id-goes-here/community-id-goes-here/endpoint-goes-name-here" />
    
    <@liaAddScript>
    (function($) {
            $(document).ready(function() {
            
                    function callLithiumEndpoint() {
                    
                        $('.my-div-css-class-goes-here').hide();
                        
                        var jqxhr = $.get("${kb_endpoint_url}" function(data) {
                          console.log("success");                     
                          $('.my-div-css-class-goes-here').prepend(data); 
                        })
                        .done(function() { console.log("second success"); })
                        .fail(function() { console.log("error"); })
                        .always(function() { console.log("finished"); $('.tmy-div-css-class-goes-here').show(); });                   
                    }                
                    callLithiumEndpoint();
            });
    })(LITHIUM.jQuery);
    </@liaAddScript>

     I hope that this is helpful for you.

  • cblown's avatar
    cblown
    Boss
    12 years ago

    Thanks Adam and Haidong. While I can't speak for the OP we are specifically interested in using EndPoints inside Custom Components simply becuase they don't support http.client and EndPoints do :p

     

     

  • AdamN's avatar
    AdamN
    Khoros Oracle
    12 years ago

    Thanks Chris, I see your point and understand where you're coming from. From what I understand, the dev team is still looking into making http.client a possibility for custom components. For the time being, Haidong's suggested approach of using JavaScript/Ajax is probably going to be your best.

     

    If custom components supported http.client, would you still see a need to call an endpoint via FreeMarker?

  • cblown's avatar
    cblown
    Boss
    12 years ago

    AdamN wrote:

     

    If custom components supported http.client, would you still see a need to call an endpoint via FreeMarker?


     

    No I can't see why we'd need that unless there is some sort of caching going on inside Endpoints that's not happening in Custom Components. 

     

     

  • HaidongG's avatar
    HaidongG
    Lithium Alumni (Retired)
    12 years ago

    Regarding caching, maybe DougS can correct me, I remember that we do have certain level of caching with http.client itself.