Gel script calling REST API
search cancel

Gel script calling REST API

book

Article ID: 218785

calendar_today

Updated On:

Products

Clarity PPM On Premise Clarity PPM SaaS

Issue/Introduction

  • We are creating a GEL script to invoke the Rest API.
  • Is there a working example we can use

Environment

Release : Any

Resolution

  • Here is a sample gel script to update a task and it works fine, please feel to use this as example:
<gel:script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wss="http://www.boomi.com/connector/wss"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xog="http://www.niku.com/xog"
xmlns:x="jelly:org.apache.commons.jelly.tags.xml.XMLTagLibrary"
xmlns:util="jelly:util" xmlns:sql="jelly:sql"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:jelly="jelly:core"
xmlns:nikuq="http://www.niku.com/xog/Query"
xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
xmlns:file="jelly:com.niku.union.gel.FileTagLibrary"
xmlns:core="jelly:core">

<!--***************************************Get the session from the system Start ***********************************-->
<gel:parameter var="gel_host_user" default="admin"/>
<gel:parameter var="gel_host_pass" default="%password%"/>
<gel:setDataSource dbId="niku"/>
<!--Set parameter here End -->
<!--***************************************Get the session from the system End ***********************************-->



<!-- Generate SessionID -->
<core:new className="com.niku.union.security.DefaultSecurityIdentifier" var="secId" />
<core:invokeStatic var="userSessionCtrl" className="com.niku.union.security.UserSessionControllerFactory" method="getInstance" />
<core:set var="secId" value="${userSessionCtrl.init(gel_host_user, secId)}"/>
<core:set var="sessionID" value="${secId.getSessionId()}"/>

<core:choose>
<core:when test="${sessionID == null}">
<gel:log level="ERROR">Cannot login to Clarity XOG. Check username.</gel:log>
</core:when>
<core:otherwise>
<gel:log>SessionID: ${sessionID}</gel:log>
</core:otherwise>
</core:choose>



<!--core:invoke method="setHeader" on="${patch}"> <core:arg type="java.lang.String" value="Cookie"/>
<core:arg type="java.lang.String" value="${v_Cookie}"/> </core:invoke-->



<core:new var="patch" className="org.apache.http.client.methods.HttpPatch">
<core:arg value="http://clarity/ppm/rest/v1/projects/5001083/tasks/5001170" type="java.lang.String"/>
</core:new>
<core:invoke on="${patch}" method="setHeader">
<core:arg value="Authorization" type="java.lang.String"/>
<core:arg value="Basic YWRtaW46YWRtaW4=" type="java.lang.String"/>
</core:invoke>

<core:invoke on="${patch}" method="setHeader">
<core:arg value="Content-Type" type="java.lang.String"/>
<core:arg value="application/json" type="java.lang.String"/>
</core:invoke>

<core:set var="createProject" escapeText="false">{"percentComplete": "1", "name": "Initiation Phase2" }</core:set>
<!-- <core:set var="createProject" escapeText="false">{"code": "${row.CODE}", "name": "${row.NAME}", "description":"${row.DESCRIPTION}" }</core:set> -->

<core:new var="entity" className="org.apache.http.entity.StringEntity">
<core:arg value="${createProject}" type="java.lang.String"/>
</core:new>
<gel:log>entity: ${entity}</gel:log>

<core:invoke on="${patch}" method="setEntity">
<core:arg value="${entity}" type="org.apache.http.entity.StringEntity"/>
</core:invoke>
<gel:log>patch: ${patch}</gel:log>

<core:invokeStatic var="httpclient" className="org.apache.http.impl.client.HttpClients" method="createDefault"/>
<core:invoke var="response" on="${httpclient}" method="execute">
<core:arg value="${patch}" type="org.apache.http.client.methods.HttpPatch"/>
</core:invoke>
<gel:log>response: ${response}</gel:log>

<core:invoke var="entityResponse" on="${response}" method="getEntity"/>
<core:invoke var="ResponseStatus" on="${response}" method="getStatusLine"/>
<gel:log>ResponseStatus getProtocolVersion: ${ResponseStatus.getProtocolVersion()}</gel:log>
<gel:log>ResponseStatus getReasonPhrase: ${ResponseStatus.getReasonPhrase()}</gel:log>
<gel:log>ResponseStatus getStatusCode: ${ResponseStatus.getStatusCode()}</gel:log>
<gel:log>entityResponse: ${entityResponse}</gel:log>
<core:set var="void" value="${response.close()}"/>

<gel:log>exception::${exception}</gel:log>
</gel:script>

Additional Information

Note: Troubleshooting custom code is out of scope for Clarity Support

Please check the thread on the Communities on this for more information or comments