1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Xml.Linq;
  7. using clempaul.Dreamhost;
  8.  
  9. namespace clempaul
  10. {
  11.     public class DreamhostAPI
  12.     {
  13.         #region Private Variables
  14.  
  15.         private string apikey = string.Empty;
  16.         private string accountNumber = string.Empty;
  17.         private DomainRequests domain = null;
  18.         private DNSRequests dns = null;
  19.         private UserRequests user = null;
  20.         private MailRequests mail = null;
  21.         private MySQLRequests mysql = null;
  22.         private AnnouncementListRequests announcementlist = null;
  23.         private AccountRequests account = null;
  24.         private OneClickRequests oneclick = null;
  25.  
  26.         #endregion
  27.  
  28.         #region Constructor
  29.  
  30.         public DreamhostAPI(string apikey)
  31.         {
  32.             this.apikey = apikey;
  33.         }
  34.  
  35.         public DreamhostAPI(string apikey, string accountNumber)
  36.         {
  37.             this.apikey = apikey;
  38.             this.accountNumber = accountNumber;
  39.         }
  40.  
  41.         #endregion
  42.  
  43.         #region Accessors
  44.  
  45.         public DomainRequests Domain
  46.         {
  47.             get
  48.             {
  49.                 if (this.domain == null)
  50.                 {
  51.                     this.domain = new DomainRequests(this);
  52.                 }
  53.  
  54.                 return this.domain;
  55.             }
  56.  
  57.         }
  58.  
  59.         public DNSRequests DNS
  60.         {
  61.             get
  62.             {
  63.                 if (this.dns == null)
  64.                 {
  65.                     this.dns = new DNSRequests(this);
  66.                 }
  67.  
  68.                 return this.dns;
  69.             }
  70.         }
  71.  
  72.         public UserRequests User
  73.         {
  74.             get
  75.             {
  76.                 if (this.user == null)
  77.                 {
  78.                     this.user = new UserRequests(this);
  79.                 }
  80.  
  81.                 return this.user;
  82.             }
  83.         }
  84.  
  85.         public MailRequests Mail
  86.         {
  87.             get
  88.             {
  89.                 if (this.mail == null)
  90.                 {
  91.                     this.mail = new MailRequests(this);
  92.                 }
  93.  
  94.                 return this.mail;
  95.             }
  96.         }
  97.  
  98.         public MySQLRequests MySQL
  99.         {
  100.             get
  101.             {
  102.                 if (this.mysql == null)
  103.                 {
  104.                     this.mysql = new MySQLRequests(this);
  105.                 }
  106.  
  107.                 return this.mysql;
  108.             }
  109.         }
  110.  
  111.         public AnnouncementListRequests AnnouncementList
  112.         {
  113.             get
  114.             {
  115.                 if (this.announcementlist == null)
  116.                 {
  117.                     this.announcementlist = new AnnouncementListRequests(this);
  118.                 }
  119.  
  120.                 return this.announcementlist;
  121.             }
  122.         }
  123.  
  124.         public AccountRequests Account
  125.         {
  126.             get
  127.             {
  128.                 if (this.account == null)
  129.                 {
  130.                     this.account = new AccountRequests(this);
  131.                 }
  132.  
  133.                 return this.account;
  134.             }
  135.         }
  136.  
  137.         public OneClickRequests OneClick
  138.         {
  139.             get
  140.             {
  141.                 if (this.oneclick == null)
  142.                 {
  143.                     this.oneclick = new OneClickRequests(this);
  144.                 }
  145.  
  146.                 return this.oneclick;
  147.             }
  148.         }
  149.  
  150.         #endregion
  151.  
  152.         #region Internal Methods
  153.  
  154.         internal XDocument SendCommand(string method)
  155.         {
  156.             return this.SendCommand(method, new QueryData[0]);
  157.         }
  158.  
  159.         internal XDocument SendCommand(string method, List<QueryData> parameters)
  160.         {
  161.             return this.SendCommand(method, parameters.ToArray());
  162.         }
  163.  
  164.         internal XDocument SendCommand(string method, QueryData[] parameters)
  165.         {
  166.  
  167.             XDocument response = this.GetResponse(method, parameters);
  168.  
  169.             if (!this.ValidResponse(response))
  170.             {
  171.                 throw new Exception(GetErrorCode(response));
  172.             }
  173.  
  174.             return this.GetResponse(method, parameters);
  175.         }
  176.  
  177.         internal XDocument GetResponse(string method, QueryData[] parameters)
  178.         {
  179.             string uri = "https://api.dreamhost.com/";
  180.  
  181.             HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(uri);
  182.  
  183.             wr.Method = "POST";
  184.             wr.ContentType = "application/x-www-form-urlencoded";
  185.  
  186.             string postdata = string.Empty;
  187.  
  188.             postdata += "key=" + Uri.EscapeDataString(this.apikey);
  189.             postdata += "&unique_id=" + Uri.EscapeDataString(Guid.NewGuid().ToString());
  190.             postdata += "&format=xml";
  191.             postdata += "&cmd=" + Uri.EscapeDataString(method);
  192.  
  193.             if (this.accountNumber != string.Empty)
  194.             {
  195.                 postdata += "&account=" + Uri.EscapeDataString(this.accountNumber);
  196.             }
  197.  
  198.             foreach (QueryData parameter in parameters)
  199.             {
  200.                 postdata += "&" + Uri.EscapeDataString(parameter.Key) + "=" + Uri.EscapeDataString(parameter.Value);
  201.             }
  202.  
  203.             wr.ContentLength = postdata.Length;
  204.  
  205.             StreamWriter stOut = new StreamWriter(wr.GetRequestStream(), System.Text.Encoding.ASCII);
  206.             stOut.Write(postdata);
  207.             stOut.Close();
  208.  
  209.             return XDocument.Parse(new StreamReader(wr.GetResponse().GetResponseStream()).ReadToEnd());
  210.         }
  211.  
  212.         internal Boolean ValidResponse(XDocument response)
  213.         {
  214.             var query = from c in response.Elements("dreamhost").Elements("result") select c.Value;
  215.  
  216.             foreach (string result in query)
  217.             {
  218.                 if (result == "success")
  219.                 {
  220.                     return true;
  221.                 }
  222.             }
  223.  
  224.             return false;
  225.         }
  226.  
  227.         internal string GetErrorCode(XDocument response)
  228.         {
  229.             var query = from c in response.Elements("dreamhost").Elements("data") select c.Value;
  230.  
  231.             foreach (string result in query)
  232.             {
  233.                 return result;
  234.             }
  235.  
  236.             return string.Empty;
  237.         }
  238.  
  239.         #endregion
  240.  
  241.         #region api-list_accessible_cmds
  242.  
  243.         public IEnumerable<String> ListAccessibleMethods()
  244.         {
  245.             XDocument response = this.SendCommand("api-list_accessible_cmds");
  246.  
  247.             return from data in response.Element("dreamhost").Elements("data")
  248.                        select data.Element("cmd").AsString();
  249.         }
  250.  
  251.         #endregion
  252.     }
  253. }
  254.