OneShopAPI.cfc

From Developer's API

(Difference between revisions)
Line 44: Line 44:
 
<cfargument name="parameterValue" type="string" required="yes">
 
<cfargument name="parameterValue" type="string" required="yes">
 
 
<cfif StructKeyExists(variables._apiParameter, arguments.parameterName) >
+
<cfif StructKeyExists(variables._apiParameters, arguments.parameterName) >
 
<cfset variables._apiParameters[arguments.parameterName] = arguments.parameterValue>
 
<cfset variables._apiParameters[arguments.parameterName] = arguments.parameterValue>
 
<cfelse>
 
<cfelse>
Line 50: Line 50:
 
</cfif>
 
</cfif>
 
 
 +
<cfdump var= variables._apiParameters>
 +
 
</cffunction>
 
</cffunction>
 
 
Line 99: Line 101:
 
<cfif StructIsEmpty(variables._apiParameters) IS false>
 
<cfif StructIsEmpty(variables._apiParameters) IS false>
 
 
<cfloop collection=variables._apiParameters item="parameterName">
+
<cfloop collection=#variables._apiParameters# item="parameterName">
 
<cfset bodyParameters = bodyParameters & "<" & #parameterName# & ">" & StructFind(variables._apiParameters, parameterName) & "</" & #parameterName#  & ">" >
 
<cfset bodyParameters = bodyParameters & "<" & #parameterName# & ">" & StructFind(variables._apiParameters, parameterName) & "</" & #parameterName#  & ">" >
 
 
Line 108: Line 110:
 
</cffunction>
 
</cffunction>
 
 
<cffunction name ="GetOrdersList" access="public" returntype="string" output="true" hint="">
+
<cffunction name ="GetOrderList" access="public" returntype="string" output="true" hint="">
<cfset var orderList = #api_request("/ORDERS/LIST")#>
+
<cfset var orderList = #ApiRequest("/ORDERS/LIST")#>
 
 
 
<cfreturn orderList>
 
<cfreturn orderList>
Line 151: Line 153:
 
</cffunction>
 
</cffunction>
 
 
<cffunction name ="GetErrorsList" access="public" returntype="string" output="true" hint="">
+
<cffunction name ="GetErrorList" access="public" returntype="string" output="true" hint="">
 
 
 
<cfset var errorList = #ApiRequest("/ERRORS/LIST")#>
 
<cfset var errorList = #ApiRequest("/ERRORS/LIST")#>

Revision as of 19:38, 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="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._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">
		<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>