Gel Script calling REST API using API Key
search cancel

Gel Script calling REST API using API Key

book

Article ID: 370148

calendar_today

Updated On:

Products

Clarity PPM On Premise Clarity PPM SaaS

Issue/Introduction

How to use an API Key in a Gel Script?

Resolution

The following example is derived from:

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">

    <gel:parameter var="gel_host_user" default="admin"/>
    <gel:parameter var="gel_host_pass" default="%password%"/>
    <gel:setDataSource dbId="niku"/>

    <!-- 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>

    <!-- Set Endpoint -->
    <core:new var="post" className="org.apache.http.client.methods.HttpPost">
        <core:arg value="http://server/ppm/rest/v1/obaTodos" type="java.lang.String"/>
    </core:new>

    <!-- Set Headers -->
    <core:invoke on="${post}" method="setHeader">
        <core:arg value="Authorization" type="java.lang.String"/>
        <core:arg value="Bearer <Replace with your API Key>" type="java.lang.String"/>
    </core:invoke>

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

    <core:invoke on="${post}" method="setHeader">
        <core:arg value="x-api-association-key" type="java.lang.String"/>
        <core:arg value="obaTodos~projects" type="java.lang.String"/>
    </core:invoke>

    <core:invoke on="${post}" method="setHeader">
        <core:arg value="x-api-ppm-client" type="java.lang.String"/>
        <core:arg value="gel_scripts" type="java.lang.String"/>
    </core:invoke>    

    <!-- Set Body -->
    <core:set var="createToDo" escapeText="false">{"name": "<Replace with ToDo Name>", "obaAssociation": "5014910"}</core:set>

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

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

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

    <!-- Look at the responses -->
    <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