OneShopAPI.asp

From Developer's API

Revision as of 19:31, 26 May 2008 by Chrisn (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
<%
'This class is a wrapper class to the API and will be used to contact the 1ShoppingCart API
Class OneShopAPI

	Private m_MerchantID
	Private m_MerchantKey
	Private m_ApiUrl
	Private	m_ApiCallParameters		
	
	'This property sets your merchant id	
	Public Property Let MerchantID(value)
		m_merchantID = value
	End Property 
	
	'This property sets your merchant api key
	Public Property Let MerchantKey(value)
		m_merchantKey = value
	End Property 
	
	'this property sets the url location of the api 
	Public Property Let ApiUrl(value)
		m_apiUrl = value
	End Property
	
	Private sub class_initialize()
		set m_ApiCallParameters	= Server.CreateObject("Scripting.Dictionary")
	End sub
	
	'This method will add required parameters to a parameter array
	'This array will be used when building the request body to 
	'send to the API		
	Public sub AddApiParameter(parameterName, parameterValue)
		
		'Check if key already exists if so reset the value to the
		'value passed in to the method
		if m_apiCallParameters.Exists(parameterName) = false then
			m_apiCallParameters.Add parameterName,parameterValue
		else
			m_apiCallParameters.item(parameterName) = parameterValue
		End if			
		
	End Sub	
	
	'This method will clear all the values in the api parameters dictionary
	Public Sub ClearAPIParameters()
		m_ApiCallParameters.RemoveAll
	End Sub	

	'this method uses the WinHttpRequest object to make
	'a POST request to the api and return the response
	'from the API
	Private Function SendHttpRequest(url, request_body)
	
		dim winHttp
		dim apiResult 
							
		Set winHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")			
		winHttp.Open "POST", url, false			
		winHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 						
		winHttp.Send request_body					
		apiResult = winHttp.ResponseBody
			
		set winHttp = nothing
		
		SendHttpRequest = apiResult
		
	End Function
	
	'This method will call the win_http request method
	'after appEnding the proper information to the url
	'and creating the request body
	Private Function ApiRequest(api_path)
		
		dim url
		dim request_body
		dim result

		url = m_apiUrl & "/API/" & m_merchantID & api_path			
		request_body = CreateRequestString()			
		result = SendHttpRequest(url, request_body)			
		ApiRequest = result					

	End Function		
	
	'This method will take a properly formatted api url
	'and create the response body then call the http request method
	Public Function XlinkApiRequest(url)
	
		dim request_body
		dim result
		
		request_body = CreateRequestString()			
		result = SendHttpRequest(url, request_body)			
		
		XlinkApiRequest = result
		
	End Function
	
	'This method will create the request body
	'which consists of the merchant key wrapped
	'in XML expected by the API
	Private Function CreateRequestString()
		dim requestBody
		
		requestBody = "<Request><Key>" & m_merchantKey & "</Key>" &	ParseAPIParameters() & "</Request>"			
		CreateRequestString = requestBody
	End Function
	
	'This method will take an array of parameters and create
	'the rest of the request body to send to the API
	Private Function ParseAPIParameters()
		dim bodyParameters
		bodyParameters = ""
		
		for each key in m_apiCallParameters.Keys
			
			bodyParameters = bodyParameters & _
				"<" & key & ">" & m_apiCallParameters.Item(key) & "</"  & key & ">"
		next
			
		ParseAPIParameters = bodyParameters
	
	End Function
				
	'Retrieve Order Information from API
	
	'ORDERS LIST
	Public Function GetOrderList()
		GetOrderList = ApiRequest("/ORDERS/LIST")
	End Function
	
	'SPECIFIC ORDER
	Public Function GetOrderById(order_id)
		GetOrderById = ApiRequest("/ORDERS/" & order_id & "/READ")
	End Function
	
	'Retrieve Product Information from API
			
	'PRODUCT LIST
	Public Function GetProductList()
		GetProductList = ApiRequest("/PRODUCTS/LIST")
	End Function
	
	'SPECIFIC PRODUCT
	Public Function GetProductById(product_id)
		GetProductById = ApiRequest("/PRODUCTS/" & product_id & "/READ")
	End Function
	
	'Retrieve Client Information from API
	
	'CLIENT LIST
	Public Function GetClientList()
		GetClientList = ApiRequest("/CLIENTS/LIST")
	End Function
	
	'SPECIFIC CLIENT
	Public Function GetClientById(client_id)
		GetClientById = ApiRequest("/CLIENTS/" & client_id & "/READ")
	End Function
	
	'Retrieve the list of available methods
	Public Function GetAvailableAPIMethods()
		GetAvailableAPIMethods = ApiRequest("")
	End Function	
	
	'Retrieve list of possible Error messages
	Public Function GetErrorList()
		GetErrorList = ApiRequest("/ERRORS/LIST")
	End Function		

End Class
%>