OneShopAPI.asp

From Developer's API

(Difference between revisions)
(New page: <pre> <% '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 Priva...)
 
Line 1: Line 1:
 
<pre>
 
<pre>
 
<%
 
<%
'This class is a wrapper class to the API and will be used to contact the 1ShoppingCart API
+
'This class is a wrapper class to the API and will be used to contact the 1ShoppingCart API
Class OneShopAPI
+
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
+
private m_MerchantID
'This array will be used when building the request body to
+
private m_MerchantKey
'send to the API
+
private m_ApiUrl
Public sub AddApiParameter(parameterName, parameterValue)
+
private m_ApiCallParameters
 
 
'Check if key already exists if so reset the value to the
+
'This property sets your merchant id
'value passed in to the method
+
public Property Let MerchantID(value)
if m_apiCallParameters.Exists(parameterName) = false then
+
    m_merchantID = value
m_apiCallParameters.Add parameterName,parameterValue
+
end Property
else
+
m_apiCallParameters.item(parameterName) = parameterValue
+
End if
+
 
 
End Sub
+
'This property sets your merchant api key
+
public Property Let MerchantKey(value)
'This method will clear all the values in the api parameters dictionary
+
    m_merchantKey = value
Public Sub ClearAPIParameters()
+
end Property
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
+
'this property sets the url location of the api
 +
public Property Let ApiUrl(value)
 +
    m_apiUrl = value
 +
end Property
 
 
End Function
+
private sub class_initialize()
+
set m_ApiCallParameters = Server.CreateObject("Scripting.Dictionary")
'This method will call the win_http request method
+
end sub
'after appEnding the proper information to the url
+
'and creating the request body
+
Private Function ApiRequest(api_path)
+
 
 
dim url
+
'This methods takes the NextRecordSet node and
dim request_body
+
    'parses it into the the Api Parameters dictionary
dim result
+
public sub BuildApiParameters(nextRecordSetNode)
 
+
url = m_apiUrl & "/API/" & m_merchantID & api_path
+
if nextRecordSetNode.nodename = "NextRecordSet" then
request_body = CreateRequestString()
+
result = SendHttpRequest(url, request_body)
+
ClearAPIParameters
ApiRequest = result
+
 
+
for each node in nextRecordSetNode.ChildNodes
End Function
+
AddApiParameter node.nodename, node.text
+
next
'This method will take a properly formatted api url
+
'and create the response body then call the http request method
+
end if
Public Function XlinkApiRequest(url)
+
+
end sub
dim request_body
+
dim result
+
 
 
request_body = CreateRequestString()
+
'This method will add required parameters to a parameter array
result = SendHttpRequest(url, request_body)
+
'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
 
 
XlinkApiRequest = result
+
'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)
 
 
End Function
+
dim winHttp
+
dim apiResult
'This method will create the request body
+
'which consists of the merchant key wrapped
+
Set winHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
'in XML expected by the API
+
winHttp.Open "POST", url, false
Private Function CreateRequestString()
+
winHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
dim requestBody
+
winHttp.Send request_body
 +
apiResult = winHttp.ResponseBody
 +
 +
set winHttp = nothing
 +
 +
SendHttpRequest = apiResult
 +
 +
end function
 
 
requestBody = "<Request><Key>" & m_merchantKey & "</Key>" & ParseAPIParameters() & "</Request>"
+
'This method will call the win_http request method
CreateRequestString = requestBody
+
'after appending the proper information to the url
End Function
+
'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
 
 
'This method will take an array of parameters and create
+
end function
'the rest of the request body to send to the API
+
Private Function ParseAPIParameters()
+
dim bodyParameters
+
bodyParameters = ""
+
 
 
for each key in m_apiCallParameters.Keys
+
'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
 
 
bodyParameters = bodyParameters & _
+
request_body = CreateRequestString()
"<" & key & ">" & m_apiCallParameters.Item(key) & "</"  & key & ">"
+
result = SendHttpRequest(url, request_body)
next
+
 
 
ParseAPIParameters = bodyParameters
+
XlinkApiRequest = result
+
End Function
+
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
 
 
'Retrieve Order Information from API
+
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
 
 
'ORDERS LIST
+
end Class
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
+
 
%>
 
%>
 
</pre>
 
</pre>

Revision as of 18:55, 28 May 2008

<%
	'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 methods takes the NextRecordSet node and 
    	'parses it into the the Api Parameters dictionary
		public sub BuildApiParameters(nextRecordSetNode)
								
			if nextRecordSetNode.nodename = "NextRecordSet" then
				
				ClearAPIParameters
				
				for each node in nextRecordSetNode.ChildNodes
					AddApiParameter node.nodename, node.text
				next
				
			end if	
				
		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
%>