Api - Making an API call

Making an API call

While making any API call first ensure that the client has the Connection object with the required set of cookie information in it (SessionId and SessionInfo). Check the API Authentication document to learn about how to obtain the Connection object. The two values are sent as cookies in the HTTP API call. The username corresponding to the real user who is supposedly making the API call is sent in the HTTP request header. All other required parameters are sent as regular request parameters. The return string is an XML with a predefined standard structure. Each call would have its own defined structure which would be enclosed inside a standard wrapper XML. Also every service returns a HTTP status code through the HTTPServletResponse object. HTTPServletResponse.SC_OK is set for all successfully completed service call, otherwise appropriate codes are returned. Client code can check the returned value to take appropriate action.

 

Code Snippet

 

API Call

The invokeApi() method in the Connection object makes the actual API call. The invokeApi() method sends the two cookies along with username (second parameter) in the header and the other required parameters (third parameter) as request parameters. The return is a standard XML wrapper containing the actual XML data. The HTTP response code would also be available in the Connection object after every API call.

/*
 * Populate the request parameters needed for the specific API call
*/
Map<String, String> parameters = new HashMap<String, String>();
parameters.put(DISPATCH, "subscribeByAuthCode");
parameters.put("eventId", EVENT_ID);
parameters.put("code", AUTHCODE);
           
/*
 * Makes the actual API call.
 * Param1 : Service URI
 * Param2 : Site user (username) who is making the API call
 * Param3 : Request parameter map
*/
connection.invokeApi("eventService.do", "username", parameters);

           
/*
 * Returned XML data
*/
String responseXml = connection.getLastResponseXml();
           
/*
 * Returned HTTP status code
*/
int httpCode = connection.getLastHTTPCode();

 

Response


Response of all API calls is the HTTP response code and a standard XML structure, which is shown below

HTTP Response Code = HttpServletResponse.SC_OK (Or some other code)

Response XML

<!--
Response is enclosed in a response wrapper which would have two separate parts, Message and Data. Response code indicates the overall success/failure of the API call.
     0 indicates success and any negative value indicates failure.
    
     Message can have a list of messages with each having a code and a text value.
     Actual data is enclosed inside <data> </data> construct. The structure inside data node varies from one API call to another.
-->
<response code="0">
    <msgs>
        <msg>
            <code>0</code>
            <value><![CDATA[Success]]></value>
        </msg>
    </msgs>

    <data>
        <events numItems="1" currPage="0" pageSize="10">
            <event id="3df4617a-5b72-11e1-8a65-0030489d05ee" type="CBT" status="IN_PROGRESS">
                <name><![CDATA[American History Exam 1]]></name>    
                <startDate>1329706769000</startDate>
                <endDate>1330140589000</endDate>
                <resultDate>1330139989000</resultDate>
                <subscribed>false</subscribed>
                <partiallySubscribed>false</partiallySubscribed>
                <canSubscribe>false</canSubscribe>
                <eventItems>
                </eventItems>
            </event>
        </events>
    </data>
</response>


Rating: