1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. using clempaul.Dreamhost.ResponseData;
  6.  
  7. namespace clempaul.Dreamhost
  8. {
  9.     public class DNSRequests
  10.     {
  11.  
  12.         #region Internal Variables
  13.  
  14.         DreamhostAPI api;
  15.  
  16.         #endregion
  17.  
  18.         #region Constructor
  19.  
  20.         internal DNSRequests(DreamhostAPI api)
  21.         {
  22.             this.api = api;
  23.         }
  24.  
  25.         #endregion
  26.  
  27.         #region dns-list_records
  28.  
  29.         public IEnumerable<DNSRecord> ListRecords()
  30.         {
  31.             XDocument response = api.SendCommand("dns-list_records");
  32.  
  33.             var records = from data in response.Element("dreamhost").Elements("data")
  34.                           select new DNSRecord
  35.                           {
  36.                               account_id = data.Element("account_id").AsString(),
  37.                               comment = data.Element("comment").AsString(),
  38.                               editable = data.Element("editable").AsBool(),
  39.                               type = data.Element("type").AsString(),
  40.                               value = data.Element("value").AsString(),
  41.                               record = data.Element("record").AsString(),
  42.                               zone = data.Element("zone").AsString()
  43.                           };
  44.  
  45.             return records;
  46.         }
  47.  
  48.         #endregion
  49.  
  50.         #region dns-add_record
  51.  
  52.         public void AddRecord(string record, string type, string value, string comment)
  53.         {
  54.             // Check parameters
  55.  
  56.             if (record == null || record == string.Empty)
  57.             {
  58.                 throw new Exception("Missing record parameter");
  59.             }
  60.             else if (type == null || type == string.Empty)
  61.             {
  62.                 throw new Exception("Missing type parameter");
  63.             }
  64.             else if (value == null || value == string.Empty)
  65.             {
  66.                 throw new Exception("Missing value parameter");
  67.             }
  68.  
  69.             // Build request
  70.  
  71.             List<QueryData> parameters = new List<QueryData>();
  72.  
  73.             parameters.Add(new QueryData("record", record));
  74.             parameters.Add(new QueryData("type", type));
  75.             parameters.Add(new QueryData("value", value));
  76.  
  77.             if (comment != string.Empty && comment != null)
  78.             {
  79.                 parameters.Add(new QueryData("comment", comment));
  80.             }
  81.  
  82.             api.SendCommand("dns-add_record", parameters);
  83.         }
  84.  
  85.         /*
  86.          * Overloads
  87.          */
  88.  
  89.         public void AddRecord(string record, string type, string value)
  90.         {
  91.             this.AddRecord(record, type, value, string.Empty);
  92.         }
  93.  
  94.         public void AddRecord(DNSRecord record)
  95.         {
  96.             this.AddRecord(record.record, record.type, record.value, record.comment);
  97.         }
  98.  
  99.         #endregion
  100.  
  101.         #region dns-remove_record
  102.  
  103.         public void RemoveRecord(string record, string type, string value)
  104.         {
  105.             // Check parameters
  106.  
  107.             if (record == null || record == string.Empty)
  108.             {
  109.                 throw new Exception("Missing record parameter");
  110.             }
  111.             else if (type == null || type == string.Empty)
  112.             {
  113.                 throw new Exception("Missing type parameter");
  114.             }
  115.             else if (value == null || value == string.Empty)
  116.             {
  117.                 throw new Exception("Missing value parameter");
  118.             }
  119.        
  120.             // Build request
  121.  
  122.             QueryData[] parameters = {
  123.                                          new QueryData("record", record),
  124.                                          new QueryData("type", type),
  125.                                          new QueryData("value", value)
  126.                                      };
  127.  
  128.             api.SendCommand("dns-remove_record", parameters);
  129.         }
  130.  
  131.         /*
  132.          * Overloads
  133.          */
  134.  
  135.         public void RemoveRecord(DNSRecord record)
  136.         {
  137.             this.RemoveRecord(record.record, record.type, record.value);
  138.         }
  139.  
  140.         #endregion
  141.  
  142.     }
  143. }
  144.