Clarity

 View Only
  • 1.  Clarity's Java REST API Client

    Posted Apr 02, 2025 11:55 AM

    Has anyone used the Java REST API Client?

    This works:

    <gel:script 
      xmlns:core="jelly:core"
      xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    >
      <gel:setDataSource dbId="Niku"/>
      <core:expr value="${context.getConnection().setAutoCommit(true)}"/>
     
      <core:set value="schpa28" var="restUser"/>
      
      <core:invokeStatic className="com.niku.union.config.ConfigurationManager" method="getInstance" var="config"/>
      <core:set value="${config.getProperties().getWebServer().getWebServerInstance(0).getEntryUrl()}" var="apiURL"/>
      <core:set value="${config.getProperties().getWebServer().getWebServerInstance(0).getRestContext()}" var="restContext"/>
      <core:set var="RestapiURL" value="${apiURL}${restContext}"/>
      
      <gel:log>Rest API URL: ${RestapiURL}</gel:log>
     
      <!-- Connect via JAVA REST Client -->
      
      <core:new var="javaRestClient" className="com.ca.ppm.rest.client.RestApiJavaClient">
        <core:arg type="java.lang.String" value="${RestapiURL}" />
        <core:arg type="java.lang.String" value="${restUser}" />
      </core:new>
     
      <!-- If we reach here, we've been authenticated -->
      
      <core:set escapeText="false" encode="false" var="restPayLoad">
    {
       "description": "Updated by API"
    }
      </core:set>
      <gel:log>Rest Payload ${restPayLoad}</gel:log>
      
      <core:invoke var="jsonResponse" on="${javaRestClient}" method="doPatch">
        <core:arg type="java.lang.String" value="/projects5002000"/>
    <core:arg type="java.lang.String" value="${restPayLoad}"/>
      </core:invoke>
     
      <core:choose>
      
        <core:when test="${jsonResponse.wasSuccessful()}">
      <gel:log level="INFO">Patch Worked!</gel:log>
      <core:set value="${jsonResponse.getObject().toString()}" var="jsonObject"/>
          <gel:log level="INFO">${jsonObject}</gel:log>
     
        </core:when>
     
    <core:otherwise>
     
      <gel:log level="WARN">Patch failed! ${jsonResponse.getErrors()} , ${jsonResponse.getWarnings()}</gel:log>
     
    </core:otherwise>
      </core:choose>
      
      <core:invoke var="dummy" on="${javaRestClient}" method="close"/>  
    </gel:script>
    But when I use the doGet method instead of doPatch as follows:
      <core:invoke var="jsonResponse" on="${javaRestClient}" method="doGet">
        <core:arg type="java.lang.String" value="/projects/5002000"/>
        <core:arg type="java.lang.String" value="application/json"/>
      </core:invoke>
    It always fails.
    Does anyone have any ideas?
    All suggestions gratefully accepted!
    Paul


  • 2.  RE: Clarity's Java REST API Client

    Posted Apr 02, 2025 01:17 PM

    Apologies, there should be a / in the invoke clause:     <core:arg type="java.lang.String" value="/projects/5002000"/>

    I was trying to disguise things




  • 3.  RE: Clarity's Java REST API Client

    Posted Apr 11, 2025 01:31 PM

    This can work in some scenarios, but we've seen issues with that class in the past.  There were some endpoints that didn't work, and the doDelete method would throw exceptions every time, even though it actually did perform the delete.  We (Rego Consulting) have developed a suite of additional tags to be used in GEL that we call the jam tags (a play on words since GEL is really based on the Apache jelly project).  We give those away for free, and they have tags that are very useful for REST.  For example, your REST could be as simple as this:

    <!-- Use the getRestAPIEntryURL method to get the REST URL from the configuration manager -->
    <core:invokeStatic var="config" className="com.niku.union.config.ConfigurationManager" method="getInstance"/>
    <core:set var="restUrl" value="${config.getRestAPIEntryURL()}"/>
            
    <!-- Rego jam tag to get session for specified user to pass to the rest tag -->
    <clarity:restAuth var="restAuth" userName="${username}" />
     
    <core:set encode="false" escapeText="false" var="restResource"><![CDATA[${restUrl}/rest/v1/projects/5002000]]></core:set>
     
    <!-- Rego jam tag that will take care of escaping illegal json characters in strings -->
    <rest:json var="restBody">
        <![CDATA[{
        "description": "Updated by API"
    }]]>
    </rest:json>
     
    <!-- Rego jam tag that allows you to perform a PATCH on any REST API -->
    <rest:patch var="restResponse" url="${restResource}" headers="${restAuth}" content="${restBody}" />
     
    <core:if test="${!restResponse.success}">
        <!-- Rego jam tag that allows you to throw an exception, and essentially break out of the code at this point.  Typically this entire script would be contained inside a core:catch -->
        <jam:throw message="Error updating project. restResource: ${restResource} | restBody: ${restBody} | Response ${restResponse.code}: ${restResponse.body}"/>
    </core:if>
    Feel free to reach out if you're interested in these tags.  They can be deployed on BC hosted environments as well as an Rego hosted or On Premise environment.



  • 4.  RE: Clarity's Java REST API Client

    Posted Apr 12, 2025 02:56 AM

    Hi there,

    Yep, I found that the doDelete always throws an error even after successfully deleting the task.

    What I also found is that a different class, com.ca.ppm.rest.client.ClarityRestApi, works for doGet, doPost, doPatch and doDelete. But, and it's a very big but, it worked on a test 16.2.2 system. When I try it on a 16.3.1 system it universally returns a 401 error for every call, even though I have every global right in the system, and making the same call via PostMan works fine.

    This is on an-premise customer and I didn't think the jam/rest tags would be available.

    See you in September?

    Paul




  • 5.  RE: Clarity's Java REST API Client

    Posted Apr 14, 2025 03:24 PM

    I don't know that I've played with that class before.  We have given the jam tags out to several on-premise customers.  They just have to throw the jar in the custom-lib and restart services.  If organizations are hesitant to introduce custom jars like that (even though BC allows them on their hosted environments), then you could also create a generic script to handle all of the rest stuff and call it as an include file inside your gel script.  So, there are options.

    I'll definitely be there in September, so if you're there, make sure you come say hi.




  • 6.  RE: Clarity's Java REST API Client

    Posted Apr 14, 2025 04:30 PM

    Thanks James!