OneShopAPI.cs

From Developer's API

(Difference between revisions)
(Changed the usage and returns of streams to XmlDocuments)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<pre>
+
<pre>using System;
using System;
+
 
using System.Data;
 
using System.Data;
 
using System.Configuration;
 
using System.Configuration;
Line 19: Line 18:
 
public class OneShopApi
 
public class OneShopApi
 
{
 
{
     private string _merchantID;
+
     private string _merchantId;
 
     private string _merchantKey;
 
     private string _merchantKey;
 
     private string _apiUri;
 
     private string _apiUri;
Line 27: Line 26:
 
     public string MerchantID
 
     public string MerchantID
 
     {
 
     {
         get {return _merchantID;}
+
         get {return _merchantId;}
         set {_merchantID = value;}
+
         set {_merchantId = value;}
 
     }
 
     }
  
Line 45: Line 44:
 
     }
 
     }
  
     public OneShopApi(string merchantID, string merchantKey, string apiUri)
+
     public OneShopApi(string APIUri)
 
     {
 
     {
         _merchantID = merchantID;
+
         _apiUri = APIUri;
 +
    }
 +
   
 +
    public OneShopApi(string merchantId, string merchantKey, string apiUri)
 +
    {
 +
        _merchantId = merchantId;
 
         _merchantKey = merchantKey;
 
         _merchantKey = merchantKey;
 
         _apiUri = apiUri;
 
         _apiUri = apiUri;
Line 93: Line 97:
 
     // a POST request to the api and return the response
 
     // a POST request to the api and return the response
 
     // from the API
 
     // from the API
     private Stream SendHttpRequest(string uri, string requestBody)
+
     private XmlDocument SendHttpRequest(string uri, string requestBody)
 
     {
 
     {
 
         HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
 
         HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
Line 100: Line 104:
 
         request.Method = "POST";
 
         request.Method = "POST";
 
         Stream stream = request.GetRequestStream();
 
         Stream stream = request.GetRequestStream();
         StreamWriter streamWriter = new StreamWriter(stream);
+
 
         streamWriter.WriteLine(requestBody);
+
         using (StreamWriter streamWriter = new StreamWriter(stream))
         streamWriter.Close();
+
         {
 +
            streamWriter.WriteLine(requestBody);
 +
         }
  
 
         // get response
 
         // get response
 
         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 
         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 
          
 
          
         Stream apiResult = response.GetResponseStream();
+
         using (response)
     
+
        {
        return apiResult;
+
            Stream APIResult = response.GetResponseStream();
 +
            XmlDocument xmlResult = new XmlDocument();
 +
            xmlResult.Load(APIResult);
 +
            return xmlResult;
 +
        }
 
     }
 
     }
  
Line 115: Line 125:
 
     // after appending the proper information to the uri
 
     // after appending the proper information to the uri
 
     // and creating the request body
 
     // and creating the request body
     private Stream ApiRequest(string apiPath)
+
     private XmlDocument ApiRequest(string apiPath)
 
     {
 
     {
 +
        XmlDocument result = new XmlDocument();
 +
 
         string  uri,
 
         string  uri,
 
                 requestbody;
 
                 requestbody;
       
 
        Stream result;
 
  
         uri = _apiUri + "/API/" + _merchantID + apiPath;
+
         uri = _apiUri + "/API/" + _merchantId + apiPath;
 
         requestbody = CreateRequestString();
 
         requestbody = CreateRequestString();
  
Line 132: Line 142:
 
     // This method will take a properly formatted api uri
 
     // This method will take a properly formatted api uri
 
     // and create the response body then call the http request method
 
     // and create the response body then call the http request method
     public Stream XLinkApiRequest(string uri)
+
     public XmlDocument XLinkApiRequest(string uri)
 
     {
 
     {
 
         string requestBody;
 
         string requestBody;
         Stream result;
+
         XmlDocument result = new XmlDocument();
  
 
         requestBody = CreateRequestString();
 
         requestBody = CreateRequestString();
Line 146: Line 156:
 
     {
 
     {
 
         string requestBody;
 
         string requestBody;
         requestBody = "<Request><Key>" + _merchantKey + "</Key>" + ParseApiParameters() + "</Request>";
+
         requestBody = "<Request>" +
 +
                            "<Key>" + _merchantKey + "</Key>" +  
 +
                            ParseApiParameters() +  
 +
                      "</Request>";
 
         return requestBody;
 
         return requestBody;
 
     }
 
     }
Line 161: Line 174:
 
     }
 
     }
  
     public Stream GetOrdersList()
+
     public XmlDocument GetOrderCount()
 +
    {
 +
        return ApiRequest("/ORDERS/COUNT");
 +
    }
 +
 
 +
    public XmlDocument GetOrderList()
 
     {
 
     {
 
         return ApiRequest("/ORDERS/LIST");
 
         return ApiRequest("/ORDERS/LIST");
 
     }
 
     }
  
     public Stream GetOrderById(string orderId)
+
     public XmlDocument GetOrderById(string orderId)
 
     {
 
     {
 
         return ApiRequest("/ORDERS/" + orderId + "/READ");
 
         return ApiRequest("/ORDERS/" + orderId + "/READ");
 
     }
 
     }
  
     public Stream GetProductsList()
+
     public XmlDocument GetProductList()
 
     {
 
     {
 
         return ApiRequest("/PRODUCTS/LIST");
 
         return ApiRequest("/PRODUCTS/LIST");
 
     }
 
     }
  
     public Stream GetProductById(string productId)
+
     public XmlDocument GetProductById(string productId)
 
     {
 
     {
 
         return ApiRequest("/PRODUCTS/" + productId + "/READ");
 
         return ApiRequest("/PRODUCTS/" + productId + "/READ");
 
     }
 
     }
  
     public Stream GetClientsList()
+
     public XmlDocument GetClientList()
 
     {
 
     {
 
         return ApiRequest("/CLIENTS/LIST");
 
         return ApiRequest("/CLIENTS/LIST");
 
     }
 
     }
  
     public Stream GetClientsById(string clientId)
+
     public XmlDocument GetClientById(string clientId)
 
     {
 
     {
 
         return ApiRequest("/CLIENTS/" + clientId + "/READ");
 
         return ApiRequest("/CLIENTS/" + clientId + "/READ");
 
     }
 
     }
  
     public Stream GetErrorsList()
+
     public XmlDocument GetErrorList()
 
     {
 
     {
 
         return ApiRequest("/ERRORS/LIST");
 
         return ApiRequest("/ERRORS/LIST");
 
     }
 
     }
  
     public Stream GetAvailableAPIMethods()
+
     public XmlDocument GetAvailableAPIMethods()
 
     {
 
     {
         return ApiRequest("");
+
         return ApiRequest(string.Empty);
 
     }
 
     }
 
}
 
}
 
</pre>
 
</pre>

Latest revision as of 18:27, 22 January 2010

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using System.Net;
using System.Collections.Generic;

/// <summary>
/// This class is a wrapper class to the API and will be used to contact the 1ShoppingCart API
/// </summary>
public class OneShopApi
{
    private string _merchantId;
    private string _merchantKey;
    private string _apiUri;
    private Dictionary<string, string> _apiParameters;

    // This property sets your merchant id
    public string MerchantID
    {
        get {return _merchantId;}
        set {_merchantId = value;}
    }

    // This property sets your merchant api key
    public string MerchantKey
    {
        get {return _merchantKey;}
        set {_merchantKey = value;}
    }

    // This property sets the uri location of the api
    public string ApiUri
    {
        get {return _apiUri;}
        set {_apiUri = value;}
    }

    public OneShopApi(string APIUri)
    {
        _apiUri = APIUri;
    }
    
    public OneShopApi(string merchantId, string merchantKey, string apiUri)
    {
        _merchantId = merchantId;
        _merchantKey = merchantKey;
        _apiUri = apiUri;
        _apiParameters = new Dictionary<string,string>();
    }

    // This methods takes the NextRecordSet node and 
    // parses it into the the Api Parameters dictionary
    public void BuildApiParameters(XmlNode nextRecordSet)
    {
        if (nextRecordSet.Name == "NextRecordSet")
        {
            ClearApiParameters();

            foreach (XmlNode node in nextRecordSet)
            {
                AddApiParameter(node.Name, node.InnerText);
            }
        }
    }

    // This method will add required parameters to a parameter array
    // the array will be used when building the request body to 
    // send to the API	
    public void AddApiParameter(string parameterName, string parameterValue)
    {
        // Check if key already exists if so reset the value to the
        // value passed in to the method
        if (_apiParameters.ContainsKey(parameterName))
        {
            _apiParameters[parameterName] = parameterValue;
        }
        else
        {
            _apiParameters.Add(parameterName, parameterValue);
        }
    }

    public void ClearApiParameters()
    {
        _apiParameters.Clear();
    }
    
    // This method uses the HttpWebRequest object to make
    // a POST request to the api and return the response
    // from the API
    private XmlDocument SendHttpRequest(string uri, string requestBody)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

        // send POST data
        request.Method = "POST";
        Stream stream = request.GetRequestStream();

        using (StreamWriter streamWriter = new StreamWriter(stream))
        {
            streamWriter.WriteLine(requestBody);
        }

        // get response
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        
        using (response)
        {
            Stream APIResult = response.GetResponseStream();
            XmlDocument xmlResult = new XmlDocument();
            xmlResult.Load(APIResult);
            return xmlResult;
        }
    }

    // This method will call the SendHttpRequest method
    // after appending the proper information to the uri
    // and creating the request body
    private XmlDocument ApiRequest(string apiPath)
    {
        XmlDocument result = new XmlDocument();

        string  uri,
                requestbody;

        uri = _apiUri + "/API/" + _merchantId + apiPath;
        requestbody = CreateRequestString();

        result = SendHttpRequest(uri, requestbody);

        return result;
    }

    // This method will take a properly formatted api uri
    // and create the response body then call the http request method
    public XmlDocument XLinkApiRequest(string uri)
    {
        string requestBody;
        XmlDocument result = new XmlDocument();

        requestBody = CreateRequestString();
        result = SendHttpRequest(uri, requestBody);

        return result;
    }

    private string CreateRequestString()
    {
        string requestBody;
        requestBody = "<Request>" +
                            "<Key>" + _merchantKey + "</Key>" + 
                            ParseApiParameters() + 
                      "</Request>";
        return requestBody;
    }

    private string ParseApiParameters()
    {
        string bodyParameters = "";

        foreach (string key in _apiParameters.Keys)
        {
            bodyParameters += "<" + key + ">" + _apiParameters[key] + "</" + key + ">";
        }
        return bodyParameters;
    }

    public XmlDocument GetOrderCount()
    {
        return ApiRequest("/ORDERS/COUNT");
    }

    public XmlDocument GetOrderList()
    {
        return ApiRequest("/ORDERS/LIST");
    }

    public XmlDocument GetOrderById(string orderId)
    {
        return ApiRequest("/ORDERS/" + orderId + "/READ");
    }

    public XmlDocument GetProductList()
    {
        return ApiRequest("/PRODUCTS/LIST");
    }

    public XmlDocument GetProductById(string productId)
    {
        return ApiRequest("/PRODUCTS/" + productId + "/READ");
    }

    public XmlDocument GetClientList()
    {
        return ApiRequest("/CLIENTS/LIST");
    }

    public XmlDocument GetClientById(string clientId)
    {
        return ApiRequest("/CLIENTS/" + clientId + "/READ");
    }

    public XmlDocument GetErrorList()
    {
        return ApiRequest("/ERRORS/LIST");
    }

    public XmlDocument GetAvailableAPIMethods()
    {
        return ApiRequest(string.Empty);
    }
}