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