Rev Author Line No. Line
5 clempaul 1 using System;
2 using System.Collections.Generic;
26 clempaul 3 using System.IO;
5 clempaul 4 using System.Linq;
6 clempaul 5 using System.Net;
6 using System.Xml.Linq;
26 clempaul 7 using clempaul.Dreamhost;
5 clempaul 8  
6 clempaul 9 namespace clempaul
5 clempaul 10 {
11     public class DreamhostAPI
12     {
6 clempaul 13         #region Private Variables
9 clempaul 14  
6 clempaul 15         private string apikey = string.Empty;
34 clempaul 16         private string accountNumber = string.Empty;
14 clempaul 17         private DomainRequests domain = null;
18         private DNSRequests dns = null;
19         private UserRequests user = null;
20         private MailRequests mail = null;
16 clempaul 21         private MySQLRequests mysql = null;
22         private AnnouncementListRequests announcementlist = null;
34 clempaul 23         private AccountRequests account = null;
24         private OneClickRequests oneclick = null;
6 clempaul 25  
26         #endregion
27  
28         #region Constructor
29  
13 clempaul 30         public DreamhostAPI(string apikey)
6 clempaul 31         {
32             this.apikey = apikey;
33         }
34  
34 clempaul 35         public DreamhostAPI(string apikey, string accountNumber)
36         {
37             this.apikey = apikey;
38             this.accountNumber = accountNumber;
39         }
40  
6 clempaul 41         #endregion
42  
43         #region Accessors
44  
14 clempaul 45         public DomainRequests Domain
6 clempaul 46         {
47             get
48             {
49                 if (this.domain == null)
50                 {
14 clempaul 51                     this.domain = new DomainRequests(this);
6 clempaul 52                 }
53  
54                 return this.domain;
55             }
56  
57         }
58  
14 clempaul 59         public DNSRequests DNS
10 clempaul 60         {
61             get
62             {
63                 if (this.dns == null)
64                 {
14 clempaul 65                     this.dns = new DNSRequests(this);
10 clempaul 66                 }
67  
68                 return this.dns;
69             }
70         }
71  
14 clempaul 72         public UserRequests User
11 clempaul 73         {
74             get
75             {
76                 if (this.user == null)
77                 {
14 clempaul 78                     this.user = new UserRequests(this);
11 clempaul 79                 }
80  
81                 return this.user;
82             }
83         }
84  
14 clempaul 85         public MailRequests Mail
12 clempaul 86         {
87             get
88             {
89                 if (this.mail == null)
90                 {
14 clempaul 91                     this.mail = new MailRequests(this);
12 clempaul 92                 }
93  
94                 return this.mail;
95             }
96         }
97  
16 clempaul 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  
34 clempaul 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  
6 clempaul 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  
19 clempaul 159         internal XDocument SendCommand(string method, List<QueryData> parameters)
160         {
161             return this.SendCommand(method, parameters.ToArray());
162         }
163  
9 clempaul 164         internal XDocument SendCommand(string method, QueryData[] parameters)
165         {
6 clempaul 166  
167             XDocument response = this.GetResponse(method, parameters);
168  
9 clempaul 169             if (!this.ValidResponse(response))
6 clempaul 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  
10 clempaul 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  
13 clempaul 188             postdata += "key=" + Uri.EscapeDataString(this.apikey);
10 clempaul 189             postdata += "&unique_id=" + Uri.EscapeDataString(Guid.NewGuid().ToString());
190             postdata += "&format=xml";
191             postdata += "&cmd=" + Uri.EscapeDataString(method);
192  
34 clempaul 193             if (this.accountNumber != string.Empty)
194             {
195                 postdata += "&account=" + Uri.EscapeDataString(this.accountNumber);
196             }
197  
6 clempaul 198             foreach (QueryData parameter in parameters)
199             {
10 clempaul 200                 postdata += "&" + Uri.EscapeDataString(parameter.Key) + "=" + Uri.EscapeDataString(parameter.Value);
6 clempaul 201             }
202  
10 clempaul 203             wr.ContentLength = postdata.Length;
19 clempaul 204  
205             StreamWriter stOut = new StreamWriter(wr.GetRequestStream(), System.Text.Encoding.ASCII);
10 clempaul 206             stOut.Write(postdata);
207             stOut.Close();
208  
18 clempaul 209             return XDocument.Parse(new StreamReader(wr.GetResponse().GetResponseStream()).ReadToEnd());
6 clempaul 210         }
211  
212         internal Boolean ValidResponse(XDocument response)
213         {
18 clempaul 214             var query = from c in response.Elements("dreamhost").Elements("result") select c.Value;
6 clempaul 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         {
18 clempaul 229             var query = from c in response.Elements("dreamhost").Elements("data") select c.Value;
6 clempaul 230  
231             foreach (string result in query)
232             {
233                 return result;
234             }
235  
236             return string.Empty;
237         }
238  
239         #endregion
7 clempaul 240  
19 clempaul 241         #region api-list_accessible_cmds
7 clempaul 242  
243         public IEnumerable<String> ListAccessibleMethods()
244         {
245             XDocument response = this.SendCommand("api-list_accessible_cmds");
246  
19 clempaul 247             return from data in response.Element("dreamhost").Elements("data")
18 clempaul 248                        select data.Element("cmd").AsString();
7 clempaul 249         }
250  
251         #endregion
5 clempaul 252     }
253 }