Ejemplo n.º 1
0
        private string GetOrDeleteRequest(string method, string urlFormat, params object[] args)
        {
            lock (requestLock)
            {
                string relativeUrl = string.Format(urlFormat, args);

                string requestUrl = this.GetRequestUrl(relativeUrl);

                var request = WebRequest.Create(requestUrl);
                request.Method  = method;
                request.Timeout = Config.Discourse.ApiRequestTimeout;
                try
                {
                    using (var response = request.GetResponse())
                        using (var stream = response.GetResponseStream())
                        {
                            return(new StreamReader(stream).ReadToEnd());
                        }
                }
                catch (TimeoutException tex)
                {
                    DiscourseHelper.PauseDiscourseConnections(tex, 10);
                    throw;
                }
                catch (WebException wex)
                {
                    DiscourseHelper.PauseDiscourseConnections(wex, 10);
                    throw ParseFirstError(wex);
                }
            }
        }
Ejemplo n.º 2
0
        private static Exception ParseFirstError(WebException wex)
        {
            Logger.Debug("Attempting to get and parse the first error returned from Discourse JSON result...");
            try
            {
                string jsonErrors = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
                Logger.Debug("JSON result was: " + jsonErrors);
                wex.Response.Dispose();
                dynamic json = JsonConvert.DeserializeObject(jsonErrors);
                if (json == null)
                {
                    Logger.Debug("JSON error was null.");
                    return(wex);
                }

                return(new InvalidOperationException(json.errors[0].ToString(), wex));
            }
            catch
            {
                Logger.Debug("There was an error attempting to parse the first JSON error.");
                var ex = new InvalidOperationException("Unknown error connecting to the forum API. Ensure the host name and API key settings are correct in web.config. The comment and sidebar categories must also exist on Discourse.", wex);
                DiscourseHelper.PauseDiscourseConnections(ex, 10);
                return(ex);
            }
        }
Ejemplo n.º 3
0
        private string PutOrPostRequest(string relativeUrl, IEnumerable <KeyValuePair <string, string> > postData, string method)
        {
            lock (requestLock)
            {
                string requestUrl = this.GetRequestUrl(relativeUrl);

                var request = WebRequest.Create(requestUrl);
                request.Method      = method;
                request.Timeout     = Config.Discourse.ApiRequestTimeout;
                request.ContentType = "application/x-www-form-urlencoded";

                Logger.Debug("Sending Discourse {0} request to URL: {1}", method, request.RequestUri);

                try
                {
                    using (var body = request.GetRequestStream())
                        using (var writer = new StreamWriter(body))
                        {
                            foreach (var pair in postData)
                            {
                                writer.Write("&{0}={1}", HttpUtility.UrlEncode(pair.Key), HttpUtility.UrlEncode(pair.Value));
                            }
                        }

                    using (var response = request.GetResponse())
                        using (var stream = response.GetResponseStream())
                        {
                            Logger.Debug("Response received, response code was: {0}", GetResponseCode(response));
                            return(new StreamReader(stream).ReadToEnd());
                        }
                }
                catch (TimeoutException tex)
                {
                    Logger.Debug("Timeout exception for {0} request to URL: {1}", method, request.RequestUri);
                    DiscourseHelper.PauseDiscourseConnections(tex, 10);
                    throw;
                }
                catch (WebException wex)
                {
                    Logger.Debug("Web exception for {0} request to URL: {1}", method, request.RequestUri);
                    DiscourseHelper.PauseDiscourseConnections(wex, 10);
                    throw ParseFirstError(wex);
                }
            }
        }
Ejemplo n.º 4
0
        private static Exception ParseFirstError(WebException wex)
        {
            try
            {
                string jsonErrors = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
                wex.Response.Dispose();
                dynamic json = JsonConvert.DeserializeObject(jsonErrors);
                if (json == null)
                {
                    return(wex);
                }

                return(new InvalidOperationException(json.errors[0].ToString(), wex));
            }
            catch
            {
                var ex = new InvalidOperationException("Unknown error connecting to the forum API. Ensure the host name and API key settings are correct in web.config. The comment and sidebar categories must also exist on Discourse.", wex);
                DiscourseHelper.PauseDiscourseConnections(ex, 10);
                return(ex);
            }
        }
Ejemplo n.º 5
0
        private string GetOrDeleteRequest(string method, string urlFormat, params object[] args)
        {
            lock (requestLock)
            {
                string relativeUrl = string.Format(urlFormat, args);

                string requestUrl = this.GetRequestUrl(relativeUrl);

                var request = WebRequest.Create(requestUrl);
                request.Method  = method;
                request.Timeout = Config.Discourse.ApiRequestTimeout;

                Logger.Debug("Sending Discourse {0} request to URL: {1}", method, request.RequestUri);

                try
                {
                    using (var response = request.GetResponse())
                        using (var stream = response.GetResponseStream())
                        {
                            Logger.Debug("Response received, response code was: {0}", GetResponseCode(response));
                            return(new StreamReader(stream).ReadToEnd());
                        }
                }
                catch (TimeoutException tex)
                {
                    Logger.Debug("Timeout exception for {0} request to URL: {1}", method, request.RequestUri);
                    DiscourseHelper.PauseDiscourseConnections(tex, 10);
                    throw;
                }
                catch (WebException wex)
                {
                    Logger.Debug("Web exception for {0} request to URL: {1}", method, request.RequestUri);
                    DiscourseHelper.PauseDiscourseConnections(wex, 10);
                    throw ParseFirstError(wex);
                }
            }
        }