Skip to content

Instantly share code, notes, and snippets.

@sweded
Forked from nifl/qbAPI.cfc
Created November 19, 2012 21:54
Show Gist options
  • Save sweded/4114296 to your computer and use it in GitHub Desktop.
Save sweded/4114296 to your computer and use it in GitHub Desktop.
Quickbase REST API calls
<!---
Name: qbAPI.cfc
Author: Gernot Bartels
Description: Methods to interact with QuickBase
History: Migrated from CF8 to Railo 2010-04-02.
Created: 2009-03-25
Updated: 2010-04-02
--->
<cfcomponent output="false">
<!--- Login --->
<cffunction name="qbLogin" returntype="any" access="public" output="false" hint="" description="Request login and get ticket">
<cfargument name="qbURL" type="string" required="true" />
<cfargument name="user" type="string" required="true" />
<cfargument name="pass" type="string" required="true" />
<!--- Auth --->
<cfhttp url="#qbURL#/main?act=API_Authenticate&username=#user#&password=#pass#" method="post" result="authGet">
<cfhttpparam type="Header" name="Content-Type" value="application/xml" />
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
</cfhttp>
<!--- Parse Filecontent --->
<cfset theContent = xmlParse(authGet.Filecontent) />
<!--- Get Ticket --->
<cfset theTicket = trim(theContent.xmlRoot.XmlChildren[4].XmlText) />
<cfreturn theTicket />
</cffunction>
<!--- Get Schema --->
<cffunction name="getSchema" returntype="any" access="public" output="false" hint="" description="Request application schema">
<cfargument name="qbURL" type="string" required="true" />
<cfargument name="appID" type="string" required="true" />
<cfargument name="tableID" type="string" required="false" />
<cfargument name="theTicket" type="string" required="true" />
<cfargument name="theToken" type="string" required="true" />
<!--- call GetSchema --->
<cfhttp url="#qbURL#/#appID#?act=API_GetSchema&ticket=#theTicket#&apptoken=#theToken#" method="post" result="schemaGet">
<cfhttpparam type="Header" name="Content-Type" value="application/xml" />
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
</cfhttp>
<!--- Parse Filecontent --->
<cfset appSchema = xmlParse(schemaGet.Filecontent) />
<cfreturn appSchema />
</cffunction>
<!--- Get table Schema --->
<cffunction name="tableSchema" returntype="any" access="public" output="false" hint="" description="Request application schema">
<cfargument name="qbURL" type="string" required="true" />
<cfargument name="appID" type="string" required="false" />
<cfargument name="tableID" type="string" required="false" />
<cfargument name="theTicket" type="string" required="true" />
<cfargument name="theToken" type="string" required="true" />
<cfhttp url="#qbURL#/#tableID#?act=API_GetSchema&ticket=#theTicket#&apptoken=#theToken#" method="post" result="schemaGet">
<cfhttpparam type="Header" name="Content-Type" value="application/xml" />
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
</cfhttp>
<!--- Parse Filecontent --->
<cfset appSchema = xmlParse(schemaGet.Filecontent) />
<cfreturn appSchema />
</cffunction>
<!--- Return List of Table Names --->
<cffunction name="returnTableNames" returntype="any" access="public" output="false" displayname="Return Table List" description="Return Table Names List">
<cfargument name="theSchema" type="string" required="true" hint="QB Application XML Schema" />
<cfscript>
// Search Schema for QB Application Tables
selectedElements = XmlSearch(theSchema, "qdbapi/table/chdbids/chdbid");
// Return Table Names
for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
arrTableName[i] = selectedElements[i].XmlAttributes.name;
// Convert to list
listTableNames = ArrayToList(arrTableName);
</cfscript>
<cfreturn listTableNames />
</cffunction>
<!--- Return Table ID --->
<cffunction name="returnTableID" returntype="any" access="public" output="false" displayname="Return Table ID" description="Return Table ID">
<cfargument name="theSchema" type="string" required="true" hint="QB Application XML Schema" />
<cfscript>
// Search Schema for QB Application Tables
selectedElements = XmlSearch(theSchema, "qdbapi/table/chdbids/chdbid");
// Return Table Names
for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
arrTableName[i] = selectedElements[i].XmlAttributes.name;
// Return Table ID's
for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
arrTableID[i] = selectedElements[i].XmlText;
// Create struct containing Table Names (key) & Table ID's (value)
for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
strTables["#arrTableName[i]#"] = arrTableID[i];
</cfscript>
<cfreturn strTables />
</cffunction>
<!--- Count Records --->
<cffunction name="countRecords" returntype="any" access="public" output="false" hint="" description="Count number of records">
<cfargument name="qbURL" type="string" required="true" />
<cfargument name="appID" type="string" required="true" />
<cfargument name="tableID" type="string" required="false" />
<cfargument name="theTicket" type="string" required="true" />
<cfargument name="theToken" type="string" required="true" />
<cfhttp url="#qbURL#/#appID#?act=API_GetNumRecords&ticket=#theTicket#&apptoken=#theToken#"
method="post" result="GetNumRecords">
<cfhttpparam type="Header" name="Content-Type" value="application/xml" />
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
</cfhttp>
<!--- Parse Filecontent --->
<cfset XMLResult = xmlParse(GetNumRecords.Filecontent) />
<!--- Get count --->
<cfset recordCount = trim(XMLResult.xmlRoot.XmlChildren[4].XmlText) />
<cfreturn recordCount />
</cffunction>
<!--- Create Record --->
<cffunction name="createRecord" returntype="any" access="public" output="false" description="Create new record">
<cfargument name="qbURL" type="string" required="true" />
<cfargument name="tableID" type="string" required="true" />
<cfargument name="newRecord" type="string" required="true" />
<cfargument name="theTicket" type="string" required="true" />
<cfargument name="theToken" type="string" required="true" />
<!--- call AddRecord --->
<cfhttp url="#qbURL#/#tableID#?" method="post" charset="utf-8" result="addRecord">
<cfhttpparam type="body" value="#newRecord#" />
<cfhttpparam type="Header" name="Content-Type" value="application/xml" />
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="Content-length" value="#len(newRecord)#"/>
<cfhttpparam type="URL" name="act" value="API_AddRecord">
<cfhttpparam type="URL" name="ticket" value="#theTicket#">
<cfhttpparam type="URL" name="apptoken" value="#theToken#">
</cfhttp>
<cfreturn addRecord.Filecontent />
</cffunction>
<!--- Update Record --->
<cffunction name="updateRecord" returntype="any" access="public" output="false" description="Updates existing record">
<cfargument name="qbURL" type="string" required="true" />
<cfargument name="tableID" type="string" required="true" />
<cfargument name="recordID" type="string" required="true" />
<cfargument name="content" type="string" required="true" />
<cfargument name="theTicket" type="string" required="true" />
<cfargument name="theToken" type="string" required="true" />
<!--- call EditRecord --->
<cfhttp url="#qbURL#/#tableID#?" method="post" charset="utf-8" result="recordUpdate">
<cfhttpparam type="body" value="#content#" />
<cfhttpparam type="URL" name="act" value="API_EditRecord">
<cfhttpparam type="URL" name="rid" value="#recordID#">
<cfhttpparam type="URL" name="ticket" value="#theTicket#">
<cfhttpparam type="URL" name="apptoken" value="#theToken#">
<cfhttpparam type="Header" name="Content-Type" value="application/xml" />
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
</cfhttp>
<cfreturn recordUpdate.Filecontent />
</cffunction>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment