OneShopAPI.cfc

From Developer's API

(Difference between revisions)
 
Line 1: Line 1:
<pre>
+
<pre><cfcomponent hint="This component is a Client wrapper to call the methods of the API and returns the resulting XML">
<cfcomponent hint="This component is a Client wrapper to call the methods of the API and returns the resulting XML">
+
 
 
 
<cfset variables._merchantId = "" />
 
<cfset variables._merchantId = "" />
Line 30: Line 29:
 
 
 
<cffunction name="BuildApiParameters" access="public" returntype="void" output="false" hint="Parses the nextRecordSet node into apiParameters">
 
<cffunction name="BuildApiParameters" access="public" returntype="void" output="false" hint="Parses the nextRecordSet node into apiParameters">
<cfargument name="nextRecordsetNode" type="xml">
+
<cfargument name="nextRecordset" type="array">
+
<cfset nodes = xmlSearch(arguments.nextRecordsetNode, "/NextRecordSet")>
+
<cfloop from="1" to="#arraylen(arguments.nextRecordset)#" index="i">
+
<cfloop array=#nodes# index=node>
+
<cfset ClearApiParameters()>
<AddApiParameter(node.name, node.xmlText)>
+
 +
<cfloop from="1" to="#arraylen(arguments.nextrecordset[i].XmlChildren)#" index="j">
 +
<cfset AddApiParameter(arguments.nextrecordset[i].XmlChildren[j].Xmlname, arguments.nextrecordset[i].XmlChildren[j].XmlText)>
 +
</cfloop>
 +
 
</cfloop>
 
</cfloop>
+
 
</cffunction>
 
</cffunction>
 
 
Line 55: Line 58:
 
 
 
<cffunction name="ClearApiParameters" access="public" returntype="void" output="false" hint="Clears all values from the api parameters">
 
<cffunction name="ClearApiParameters" access="public" returntype="void" output="false" hint="Clears all values from the api parameters">
<StructClear(variables._apiParameters) />
+
<cfset StructClear(variables._apiParameters) />
 
</cffunction>
 
</cffunction>
 
 

Latest revision as of 20:29, 28 May 2008

<cfcomponent hint="This component is a Client wrapper to call the methods of the API and returns the resulting XML">
	
	<cfset variables._merchantId = "" />
	<cfset variables._merchantKey = "" />
	<cfset variables._apiUrl = "" />
	<cfset variables._apiParameters = StructNew()>
	
	<cffunction name="MerchantId" access="public" returntype="void" output="false" hint="Set the Merchant Id property">
  		<cfargument name="merchantId" type="string" required="yes" hint="Merchant Account Id" />

  		<cfset variables._merchantId = arguments.merchantId />		
	</cffunction>
	
	<cffunction name="MerchantKey" access="public" returntype="void" output="false" hint="Set the Merchant API Key property">
  		<cfargument name="merchantKey" type="string" required="yes" hint="Merchant API Key" />
				
		<cfset variables._merchantKey = arguments.merchantKey />
	</cffunction>
	
	<cffunction name="ApiUri" access="public" returntype="void" output="false" hint="Set the API URI property">
  		<cfargument name="apiUri" type="string" required="yes" hint="API URI" />

  		<cfset variables._apiUri = arguments.apiUri />
	</cffunction>
	
	<cffunction name="init" access="public" returnType="OneShopAPI" output="false" hint="Instantiate The OneShopAPI Proxy">
  		 <cfreturn this />
	</cffunction>
	
	<cffunction name="BuildApiParameters" access="public" returntype="void" output="false" hint="Parses the nextRecordSet node into apiParameters">
		<cfargument name="nextRecordset" type="array">		
						
		<cfloop from="1" to="#arraylen(arguments.nextRecordset)#" index="i">			
						
				<cfset ClearApiParameters()>
				
				<cfloop from="1" to="#arraylen(arguments.nextrecordset[i].XmlChildren)#" index="j">				
					<cfset AddApiParameter(arguments.nextrecordset[i].XmlChildren[j].Xmlname, arguments.nextrecordset[i].XmlChildren[j].XmlText)>
				</cfloop>				
	
		</cfloop>
				
	</cffunction>
	
	<cffunction name="AddApiParameter" access="public" returntype="void" output="false" hint="Add a parameter to the parameters array">
		<cfargument name="parameterName" type="string" required="yes">
		<cfargument name="parameterValue" type="string" required="yes">
		
		<cfif StructKeyExists(variables._apiParameters, arguments.parameterName) >
			<cfset variables._apiParameters[arguments.parameterName] = arguments.parameterValue>
		<cfelse>		
			<cfset val = StructInsert(variables._apiParameters, arguments.parameterName, arguments.parameterValue)/>
		</cfif>
		
		<cfdump var= variables._apiParameters>
				
	</cffunction>		
	
	<cffunction name="ClearApiParameters" access="public" returntype="void" output="false" hint="Clears all values from the api parameters">
		<cfset StructClear(variables._apiParameters) />
	</cffunction>	
	
	<cffunction name = "SendHttpRequest" access="private" returntype="string" output="true" hint="Make the http request to the API">
		<cfargument name = "url" type="string" required="yes" hint="the url to the API">
		<cfargument name = "request_body" type="string"  required="yes" hint="the formatted xml string to the API">
				
		<cfhttp url= #arguments.url# method="POST" >	
			<cfhttpparam type="body" Name="requestData" value = #arguments.request_body# >			
		</cfhttp>

		<cfreturn #cfhttp.fileContent# />
		
	</cffunction>
	
	<cffunction name ="ApiRequest" access="private" returntype="string" output="true" hint="Call the http calling method">
		<cfargument name = "apiPath" type="string" required="yes" hint="">
		
		<cfset var uri = variables._apiUri & "/API/" & variables._merchantId & arguments.apiPath>		
		<cfset var requestBody = #CreateRequestString()#>				
		<cfset var apiResult = #SendHttpRequest(uri,requestBody)#>
		<cfreturn apiResult>		
		
	</cffunction>
	
	<cffunction name="XLinkApiRequest" access="public" returntype="string" output="true" hint="Use a preformatted string to call API">
		<cfargument name="uri" type="string" required="yes" hint="">
		
		<cfset var requestBody = #CreateRequestString()# >
		<cfset var apiResult = #SendHttpRequest(arguments.uri, requestBody)#>
		
		<cfreturn apiResult>
		
	</cffunction>
	
	<cffunction name="CreateRequestString" access="private" returntype="string" output="true" hint="Create the xml for the body of request" >
		<cfset var requestString = "<Request><Key>" & variables._merchantKey & "</Key>" & #ParseApiParameters()# & "</Request>" >
		<cfreturn requestString >		
	</cffunction>
	
	<cffunction name="ParseApiParameters" access="private" returntype="string" output="true" hint="" >
		
		<cfset var bodyParameters = ""/>
		
		<cfif StructIsEmpty(variables._apiParameters) IS false>
		
			<cfloop collection=#variables._apiParameters# item="parameterName">
					<cfset bodyParameters = bodyParameters & "<" & #parameterName# & ">" & StructFind(variables._apiParameters, parameterName) & "</" & #parameterName#  & ">" >					
			
			</cfloop>
			
		</cfif>		
		<cfreturn bodyParameters >		
	</cffunction>	
	
	<cffunction name ="GetOrderList" access="public" returntype="string" output="true" hint="">
		<cfset var orderList = #ApiRequest("/ORDERS/LIST")#>
		
		<cfreturn orderList>		
	</cffunction>	
	
	<cffunction name ="GetOrderById" access="public" returntype="string" output="true" hint="">
		<cfargument name = "orderId" type="string" required = "yes" hint="">
		
		<cfset var order = #ApiRequest("/ORDERS/" & arguments.orderId & "/READ")#>
	
		<cfreturn order>
	</cffunction>
		
	<cffunction name ="GetProductList" access="public" returntype="string" output="true" hint="">
		<cfset var orderList = #ApiRequest("/PRODUCTS/LIST")#>
		
		<cfreturn orderList>		
	</cffunction>
	
	<cffunction name ="GetProductById" access="public" returntype="string" output="true" hint="">
		<cfargument name = "productId" type="string" required = "yes" hint="">
		
		<cfset var product = #ApiRequest("/PRODUCTS/" & arguments.productId & "/READ")#>
	
		<cfreturn product>
	</cffunction>	
		
	<cffunction name ="GetClientList" access="public" returntype="string" output="true" hint="">
		
		<cfset var clientList = #ApiRequest("/CLIENTS/LIST")#>
		
		<cfreturn clientList>		
	</cffunction>
	
	<cffunction name ="GetClientById" access="public" returntype="string" output="true" hint="">
		<cfargument name = "clientId" type="string" required = "yes" hint="">
		
		<cfset var client = #ApiRequest("/CLIENTS/" & arguments.productId & "/READ")#>
	
		<cfreturn order>
	</cffunction>
	
	<cffunction name ="GetErrorList" access="public" returntype="string" output="true" hint="">
		
		<cfset var errorList = #ApiRequest("/ERRORS/LIST")#>
	
		<cfreturn errorList>
	</cffunction>
	
	<cffunction name ="GetAvailableApiMethods" access="public" returntype="string" output="true" hint="">
		
		<cfset var methodList = #ApiRequest("")#>
	
		<cfreturn methodList>
	</cffunction>			
	
</cfcomponent>