public void DeleteMonitor(string monitorId)
        {
            var sb = new StringBuilder(_baseUri);

            sb.Append("/deleteMonitor?");
            sb.AppendFormat("apiKey={0}", _apiKey);
            sb.AppendFormat("&monitorID={0}", monitorId);

            var    wc     = new WebClient();
            string result = wc.DownloadString(sb.ToString());

            XDocument xDoc = XDocument.Parse(result);

            // Error handling
            if (xDoc.Root.Name == "error")
            {
                var exception = new UptimeRobotClientException(null);
                UptimeRobotExceptionType exType;

                string errorCode = (string)xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();


                Enum.TryParse(errorCode, out exType);
                exception.ExceptionType = exType == 0 ? UptimeRobotExceptionType.ServerError : exType;
                throw exception;
            }
        }
Пример #2
0
        private List <AlertContact> RequestAlertContact(List <string> alertContactIds = null)
        {
            var sb = new StringBuilder(_baseUri);

            sb.Append("/getAlertContacts?");
            sb.AppendFormat("apiKey={0}", _apiKey);
            if (alertContactIds != null && alertContactIds.Count > 0)
            {
                sb.AppendFormat("&alertcontacts={0}", String.Join("-", alertContactIds));
            }

            sb.AppendFormat("&format={0}", _responseFormat);

            var    wc     = new WebClient();
            string result = wc.DownloadString(sb.ToString());

            XDocument xDoc = XDocument.Parse(result);

            // Error handling
            if (xDoc.Root.Name == "error")
            {
                var exception = new UptimeRobotClientException(null);
                UptimeRobotExceptionType exType;

                string errorCode = (string)xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();


                Enum.TryParse(errorCode, out exType);
                exception.ExceptionType = exType == 0 ? UptimeRobotExceptionType.ServerError : exType;
                throw exception;
            }

            var alertContactNodes = xDoc.Element("alertcontacts").Descendants("alertcontact");

            var alertContacts = alertContactNodes.Select(ac => new AlertContact
            {
                Id   = ac.Attribute("id").Value,
                Type =
                    (AlertContactType)
                    Enum.Parse(typeof(AlertContactType),
                               ac.Attribute("type").Value),
                Status =
                    (AlertContactStatus)
                    Enum.Parse(typeof(AlertContactStatus), ac.Attribute("status").Value),
                Value = ac.Attribute("value").Value
            });

            return(alertContacts.ToList());
        }
        public Monitor AddMonitor(Monitor monitor)
        {
//            •apiKey - required
//•monitorFriendlyName - required
//•monitorURL - required
//•monitorType - required
//•monitorSubType - optional (required for port monitoring)
//•monitorPort - optional (required for port monitoring)
//•monitorKeywordType - optional (required for keyword monitoring)
//•monitorKeywordValue - optional (required for keyword monitoring)
//•monitorHTTPUsername - optional
//•monitorHTTPPasswırd - optional

            if (string.IsNullOrWhiteSpace(monitor.FriendlyName) ||
                string.IsNullOrWhiteSpace(monitor.Url))
            {
                throw new ApplicationException("Some values are required for monitor creation.");
            }


            var sb = new StringBuilder(_baseUri);

            sb.Append("/newMonitor?");
            sb.AppendFormat("apiKey={0}", _apiKey);
            sb.AppendFormat("&monitorFriendlyName={0}", monitor.FriendlyName);
            //sb.AppendFormat("&monitorURL={0}", monitor.Url);
            sb.AppendFormat("&monitorType={0}", (int)monitor.Type);

            if (monitor.Type == MonitorType.Port)
            {
                sb.AppendFormat("&monitorSubType={0}", (int)monitor.Subtype);
                sb.AppendFormat("&monitorPort={0}", monitor.Port);
            }

            if (monitor.Type == MonitorType.Keyword)
            {
                sb.AppendFormat("&monitorKeywordType=1");
                sb.AppendFormat("&monitorKeywordValue={0}", monitor.KeywordValue);
            }


            sb.AppendFormat("&format={0}", _responseFormat);

            string result = string.Empty;

            try
            {
                var wc = new WebClient();
                result = wc.DownloadString(sb.ToString());
            }
            catch (WebException webex)
            {
                var ex = new UptimeRobotClientException(webex);
                ex.ExceptionType = UptimeRobotExceptionType.ServerError;
                throw ex;
            }



            XDocument xDoc = XDocument.Parse(result);

            // Error handling
            if (xDoc.Root.Name == "error")
            {
                var exception = new UptimeRobotClientException(null);
                UptimeRobotExceptionType exType;

                string errorCode = (string)xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();


                Enum.TryParse(errorCode, out exType);
                exception.ExceptionType = exType == 0 ? UptimeRobotExceptionType.ServerError : exType;
                throw exception;
            }

            monitor.Id            = (int)xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();
            monitor.CurrentStatus =
                (Status)
                Enum.Parse(typeof(Status),
                           (string)xDoc.Descendants().Select(doc => doc.Attribute("status")).FirstOrDefault());

            return(monitor);
        }
        private List <Monitor> RequestMonitor(string monitorId = null)
        {
            var sb = new StringBuilder(_baseUri);

            sb.Append("/getMonitors?");
            sb.AppendFormat("apiKey={0}", _apiKey);
            if (monitorId != null)
            {
                sb.AppendFormat("&monitors={0}", monitorId);
            }

            sb.AppendFormat("&logs={0}", 1);
            sb.AppendFormat("&format={0}", _responseFormat);

            var    wc     = new WebClient();
            string result = wc.DownloadString(sb.ToString());

            XDocument xDoc = XDocument.Parse(result);


            // Error handling
            if (xDoc.Root.Name == "error")
            {
                var exception = new UptimeRobotClientException(null);
                UptimeRobotExceptionType exType;

                string errorCode = (string)xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();


                Enum.TryParse(errorCode, out exType);
                exception.ExceptionType = exType == 0 ? UptimeRobotExceptionType.ServerError : exType;
                throw exception;
            }

            IEnumerable <Monitor> monitors = from m in xDoc.Descendants("monitor")
                                             select new Monitor
            {
                Id           = Convert.ToInt32(m.Attribute("id").Value),
                FriendlyName = m.Attribute("friendlyname").Value,
                Type         =
                    (MonitorType)
                    Enum.Parse(typeof(MonitorType),
                               m.Attribute("type").Value),
                Subtype =
                    m.Attribute("subtype").Value != ""
                                                                   ? (MonitorSubtype)
                    Enum.Parse(typeof(MonitorSubtype),
                               m.Attribute("subtype").Value)
                                                                   : MonitorSubtype.Unknow,
                CurrentStatus =
                    (Status)
                    Enum.Parse(typeof(Status), m.Attribute("status").Value),
                UptimeRatio =
                    Double.Parse(m.Attribute("alltimeuptimeratio").Value,
                                 NumberStyles.AllowDecimalPoint,
                                 CultureInfo.InvariantCulture),
                KeywordValue = m.Attribute("keywordvalue").Value,
                Port         =
                    m.Attribute("port").Value == ""
                                                                   ? 0
                                                                   : Convert.ToInt32(m.Attribute("port").Value)
            };

            return(monitors.ToList());
        }
        public void UpdateMonitor(Monitor monitor)
        {
//apiKey - required
//monitorID - required
//monitorFriendlyName -optional
//monitorURL -optional
//monitorType -optional
//monitorSubType -optional (used only for port monitoring)
//monitorPort -optional (used onlyfor port monitoring)
//monitorKeywordType - optional (used only for keyword monitoring)
//monitorKeywordValue - optional (used only for keyword monitoring)
//monitorHTTPUsername - optional
//monitorHTTPPasswırd - optional

            if (monitor.Id == 0)
            {
                throw new ApplicationException("Some values are required for monitor creation.");
            }


            var sb = new StringBuilder(_baseUri);

            sb.Append("/editMonitor?");
            sb.AppendFormat("apiKey={0}", _apiKey);
            sb.AppendFormat("&monitorID={0}", monitor.Id);
            if (!string.IsNullOrEmpty(monitor.FriendlyName))
            {
                sb.AppendFormat("&monitorFriendlyName={0}", monitor.FriendlyName);
            }
            if (!string.IsNullOrEmpty(monitor.Url))
            {
                sb.AppendFormat("&monitorURL={0}", monitor.Url);
            }

            sb.AppendFormat("&monitorType={0}", (int)monitor.Type);

            if (monitor.Type == MonitorType.Port)
            {
                sb.AppendFormat("&monitorSubType={0}", (int)monitor.Subtype);
                sb.AppendFormat("&monitorPort={0}", monitor.Port);
            }

            if (monitor.Type == MonitorType.Keyword)
            {
                sb.AppendFormat("&monitorKeywordType=1");
                sb.AppendFormat("&monitorKeywordValue={0}", monitor.KeywordValue);
            }


            sb.AppendFormat("&format={0}", _responseFormat);

            var    wc     = new WebClient();
            string result = wc.DownloadString(sb.ToString());

            XDocument xDoc = XDocument.Parse(result);

            // Error handling
            if (xDoc.Root.Name == "error")
            {
                var exception = new UptimeRobotClientException(null);
                UptimeRobotExceptionType exType;

                string errorCode = (string)xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();


                Enum.TryParse(errorCode, out exType);
                exception.ExceptionType = exType == 0 ? UptimeRobotExceptionType.ServerError : exType;
                throw exception;
            }
        }
        public Monitor AddMonitor(Monitor monitor)
        {
//            •apiKey - required
//•monitorFriendlyName - required
//•monitorURL - required
//•monitorType - required
//•monitorSubType - optional (required for port monitoring)
//•monitorPort - optional (required for port monitoring)
//•monitorKeywordType - optional (required for keyword monitoring)
//•monitorKeywordValue - optional (required for keyword monitoring)
//•monitorHTTPUsername - optional
//•monitorHTTPPasswırd - optional

            if (string.IsNullOrWhiteSpace(monitor.FriendlyName)
                || string.IsNullOrWhiteSpace(monitor.Url))
            {
                throw new ApplicationException("Some values are required for monitor creation.");
            }


            var sb = new StringBuilder(_baseUri);
            sb.Append("/newMonitor?");
            sb.AppendFormat("apiKey={0}", _apiKey);
            sb.AppendFormat("&monitorFriendlyName={0}", monitor.FriendlyName);
            //sb.AppendFormat("&monitorURL={0}", monitor.Url);
            sb.AppendFormat("&monitorType={0}", (int) monitor.Type);

            if (monitor.Type == MonitorType.Port)
            {
                sb.AppendFormat("&monitorSubType={0}", (int) monitor.Subtype);
                sb.AppendFormat("&monitorPort={0}", monitor.Port);
            }

            if (monitor.Type == MonitorType.Keyword)
            {
                sb.AppendFormat("&monitorKeywordType=1");
                sb.AppendFormat("&monitorKeywordValue={0}", monitor.KeywordValue);
            }


            sb.AppendFormat("&format={0}", _responseFormat);

            string result = string.Empty;

            try
            {
                var wc = new WebClient();
                result = wc.DownloadString(sb.ToString());
            }
            catch (WebException webex)
            {
                var ex = new UptimeRobotClientException(webex);
                ex.ExceptionType = UptimeRobotExceptionType.ServerError;
                throw ex;
            }
      
            

            XDocument xDoc = XDocument.Parse(result);

            // Error handling
            if(xDoc.Root.Name == "error")
            {
                var exception = new UptimeRobotClientException(null); 
                UptimeRobotExceptionType exType;
                
                string errorCode = (string) xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();
                

                Enum.TryParse(errorCode, out exType);
                exception.ExceptionType = exType == 0 ? UptimeRobotExceptionType.ServerError : exType;
                throw exception;
            }

            monitor.Id = (int) xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();
            monitor.CurrentStatus =
                (Status)
                Enum.Parse(typeof (Status),
                           (string) xDoc.Descendants().Select(doc => doc.Attribute("status")).FirstOrDefault());

            return monitor;
        }
        private List<Monitor> RequestMonitor(string monitorId = null)
        {
            var sb = new StringBuilder(_baseUri);
            sb.Append("/getMonitors?");
            sb.AppendFormat("apiKey={0}", _apiKey);
            if (monitorId != null)
            {
                sb.AppendFormat("&monitors={0}", monitorId);
            }

            sb.AppendFormat("&logs={0}", 1);
            sb.AppendFormat("&format={0}", _responseFormat);

            var wc = new WebClient();
            string result = wc.DownloadString(sb.ToString());

            XDocument xDoc = XDocument.Parse(result);


            // Error handling
            if (xDoc.Root.Name == "error")
            {
                var exception = new UptimeRobotClientException(null);
                UptimeRobotExceptionType exType;

                string errorCode = (string)xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();


                Enum.TryParse(errorCode, out exType);
                exception.ExceptionType = exType == 0 ? UptimeRobotExceptionType.ServerError : exType;
                throw exception;
            }

            IEnumerable<Monitor> monitors = from m in xDoc.Descendants("monitor")
                                            select new Monitor
                                                       {
                                                           Id = Convert.ToInt32(m.Attribute("id").Value),
                                                           FriendlyName = m.Attribute("friendlyname").Value,
                                                           Type =
                                                               (MonitorType)
                                                               Enum.Parse(typeof (MonitorType),
                                                                          m.Attribute("type").Value),
                                                           Subtype =
                                                               m.Attribute("subtype").Value != ""
                                                                   ? (MonitorSubtype)
                                                                     Enum.Parse(typeof (MonitorSubtype),
                                                                                m.Attribute("subtype").Value)
                                                                   : MonitorSubtype.Unknow,
                                                           CurrentStatus =
                                                               (Status)
                                                               Enum.Parse(typeof (Status), m.Attribute("status").Value),
                                                           UptimeRatio =
                                                               Double.Parse(m.Attribute("alltimeuptimeratio").Value,
                                                                            NumberStyles.AllowDecimalPoint,
                                                                            CultureInfo.InvariantCulture),
                                                           KeywordValue = m.Attribute("keywordvalue").Value,
                                                           Port =
                                                               m.Attribute("port").Value == ""
                                                                   ? 0
                                                                   : Convert.ToInt32(m.Attribute("port").Value)
                                                       };

            return monitors.ToList();
        }
        public void DeleteMonitor(string monitorId)
        {
            var sb = new StringBuilder(_baseUri);
            sb.Append("/deleteMonitor?");
            sb.AppendFormat("apiKey={0}", _apiKey);
            sb.AppendFormat("&monitorID={0}", monitorId);

            var wc = new WebClient();
            string result = wc.DownloadString(sb.ToString());

            XDocument xDoc = XDocument.Parse(result);
            // Error handling
            if (xDoc.Root.Name == "error")
            {
                var exception = new UptimeRobotClientException(null);
                UptimeRobotExceptionType exType;

                string errorCode = (string)xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();


                Enum.TryParse(errorCode, out exType);
                exception.ExceptionType = exType == 0 ? UptimeRobotExceptionType.ServerError : exType;
                throw exception;
            }
        }
        public void UpdateMonitor(Monitor monitor)
        {
//apiKey - required
//monitorID - required
//monitorFriendlyName -optional
//monitorURL -optional
//monitorType -optional
//monitorSubType -optional (used only for port monitoring)
//monitorPort -optional (used onlyfor port monitoring)
//monitorKeywordType - optional (used only for keyword monitoring)
//monitorKeywordValue - optional (used only for keyword monitoring)
//monitorHTTPUsername - optional
//monitorHTTPPasswırd - optional

            if (monitor.Id == 0)
            {
                throw new ApplicationException("Some values are required for monitor creation.");
            }


            var sb = new StringBuilder(_baseUri);
            sb.Append("/editMonitor?");
            sb.AppendFormat("apiKey={0}", _apiKey);
            sb.AppendFormat("&monitorID={0}", monitor.Id);
            if (!string.IsNullOrEmpty(monitor.FriendlyName))
                sb.AppendFormat("&monitorFriendlyName={0}", monitor.FriendlyName);
            if (!string.IsNullOrEmpty(monitor.Url))
                sb.AppendFormat("&monitorURL={0}", monitor.Url);

            sb.AppendFormat("&monitorType={0}", (int) monitor.Type);

            if (monitor.Type == MonitorType.Port)
            {
                sb.AppendFormat("&monitorSubType={0}", (int) monitor.Subtype);
                sb.AppendFormat("&monitorPort={0}", monitor.Port);
            }

            if (monitor.Type == MonitorType.Keyword)
            {
                sb.AppendFormat("&monitorKeywordType=1");
                sb.AppendFormat("&monitorKeywordValue={0}", monitor.KeywordValue);
            }


            sb.AppendFormat("&format={0}", _responseFormat);

            var wc = new WebClient();
            string result = wc.DownloadString(sb.ToString());

            XDocument xDoc = XDocument.Parse(result);

            // Error handling
            if (xDoc.Root.Name == "error")
            {
                var exception = new UptimeRobotClientException(null);
                UptimeRobotExceptionType exType;

                string errorCode = (string)xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();


                Enum.TryParse(errorCode, out exType);
                exception.ExceptionType = exType == 0 ? UptimeRobotExceptionType.ServerError : exType;
                throw exception;
            }
        }
        private List<AlertContact> RequestAlertContact(List<string>alertContactIds = null)
        {
            var sb = new StringBuilder(_baseUri);
            sb.Append("/getAlertContacts?");
            sb.AppendFormat("apiKey={0}", _apiKey);
            if (alertContactIds != null && alertContactIds.Count > 0)
            {
                sb.AppendFormat("&alertcontacts={0}", String.Join("-", alertContactIds));
            }

            sb.AppendFormat("&format={0}", _responseFormat);

            var wc = new WebClient();
            string result = wc.DownloadString(sb.ToString());

            XDocument xDoc = XDocument.Parse(result);

            // Error handling
            if (xDoc.Root.Name == "error")
            {
                var exception = new UptimeRobotClientException(null);
                UptimeRobotExceptionType exType;

                string errorCode = (string)xDoc.Descendants().Select(doc => doc.Attribute("id")).FirstOrDefault();


                Enum.TryParse(errorCode, out exType);
                exception.ExceptionType = exType == 0 ? UptimeRobotExceptionType.ServerError : exType;
                throw exception;
            }

            var alertContactNodes = xDoc.Element("alertcontacts").Descendants("alertcontact");

            var alertContacts = alertContactNodes.Select(ac => new AlertContact
            {
                Id = ac.Attribute("id").Value,
                Type =
                    (AlertContactType)
                        Enum.Parse(typeof (AlertContactType),
                            ac.Attribute("type").Value),
                Status =
                    (AlertContactStatus)
                        Enum.Parse(typeof (AlertContactStatus), ac.Attribute("status").Value),
                Value = ac.Attribute("value").Value
            });

            return alertContacts.ToList();
        }