OneShopAPI.cfc

From Developer's API

<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>