OneShopAPI.cs

From Developer's API

(Difference between revisions)
(New page: <pre> 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.We...)
 
Line 24: Line 24:
 
     private Dictionary<string, string> _apiParameters;
 
     private Dictionary<string, string> _apiParameters;
  
     //This property sets your merchant id
+
     // This property sets your merchant id
 
     public string MerchantID
 
     public string MerchantID
 
     {
 
     {
Line 31: Line 31:
 
     }
 
     }
  
     //This property sets your merchant api key
+
     // This property sets your merchant api key
 
     public string MerchantKey
 
     public string MerchantKey
 
     {
 
     {
Line 38: Line 38:
 
     }
 
     }
  
     //this property sets the uri location of the api
+
     // This property sets the uri location of the api
 
     public string ApiUri
 
     public string ApiUri
 
     {
 
     {
Line 53: Line 53:
 
     }
 
     }
  
     //This methods takes the NextRecordSet node and  
+
     // This methods takes the NextRecordSet node and  
     //parses it into the the Api Parameters dictionary
+
     // parses it into the the Api Parameters dictionary
 
     public void BuildApiParameters(XmlNode nextRecordSet)
 
     public void BuildApiParameters(XmlNode nextRecordSet)
 
     {
 
     {
Line 68: Line 68:
 
     }
 
     }
  
     //This method will add required parameters to a parameter array
+
     // This method will add required parameters to a parameter array
     //This array will be used when building the request body to  
+
     // the array will be used when building the request body to  
     //send to the API
+
     // send to the API
 
     public void AddApiParameter(string parameterName, string parameterValue)
 
     public void AddApiParameter(string parameterName, string parameterValue)
 
     {
 
     {
         //Check if key already exists if so reset the value to the
+
         // Check if key already exists if so reset the value to the
         //value passed in to the method
+
         // value passed in to the method
 
         if (_apiParameters.ContainsKey(parameterName))
 
         if (_apiParameters.ContainsKey(parameterName))
 
         {
 
         {
Line 90: Line 90:
 
     }
 
     }
 
      
 
      
     //this method uses the HttpWebRequest object to make
+
     // This method uses the HttpWebRequest object to make
     //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 Stream SendHttpRequest(string uri, string requestBody)
 
     {
 
     {
Line 112: Line 112:
 
     }
 
     }
  
     //This method will call the SendHttpRequest method
+
     // This method will call the SendHttpRequest method
     //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 Stream ApiRequest(string apiPath)
 
     {
 
     {
Line 130: Line 130:
 
     }
 
     }
  
     //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 Stream XLinkApiRequest(string uri)
 
     {
 
     {

Revision as of 15:22, 27 May 2008

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 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 Stream SendHttpRequest(string uri, string requestBody)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

        // send POST data
        request.Method = "POST";
        Stream stream = request.GetRequestStream();
        StreamWriter streamWriter = new StreamWriter(stream);
        streamWriter.WriteLine(requestBody);
        streamWriter.Close();

        // get response
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        
        Stream apiResult = response.GetResponseStream();
       
        return apiResult;
    }

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

        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 Stream XLinkApiRequest(string uri)
    {
        string requestBody;
        Stream result;

        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 Stream GetOrdersList()
    {
        return ApiRequest("/ORDERS/LIST");
    }

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

    public Stream GetProductsList()
    {
        return ApiRequest("/PRODUCTS/LIST");
    }

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

    public Stream GetClientsList()
    {
        return ApiRequest("/CLIENTS/LIST");
    }

    public Stream GetClientsById(string clientId)
    {
        return ApiRequest("/CLIENTS/" + clientId + "/READ");
    }

    public Stream GetErrorsList()
    {
        return ApiRequest("/ERRORS/LIST");
    }

    public Stream GetAvailableAPIMethods()
    {
        return ApiRequest("");
    }
}