OneShopAPI.cfc

From Developer's API

Revision as of 20:12, 27 May 2008 by Chrisn (Talk | contribs)
<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="nextRecordsetNode" type="xml">
		
		<cfset nodes = xmlSearch(arguments.nextRecordsetNode, "/NextRecordSet")>	
		
		<cfloop array=#nodes# index=node>
			<AddApiParameter(node.name, node.xmlText)>
		</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._apiParameter, arguments.parameterName) >
			<cfset variables._apiParameters[arguments.parameterName] = arguments.parameterValue>
		<cfelse>		
			<cfset val = StructInsert(variables._apiParameters, arguments.parameterName, arguments.parameterValue)/>
		</cfif>
		
	</cffunction>		
	
	<cffunction name="ClearApiParameters" access="public" returntype="void" output="false" hint="Clears all values from the api parameters">
		<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 ="GetOrdersList" access="public" returntype="string" output="true" hint="">
		<cfset var orderList = #api_request("/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 ="GetErrorsList" 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>