示例#1
0
        private async Task <string> ExecuteRequestAsync(PrtgUrl url, Func <HttpResponseMessage, Task <string> > responseParser = null)
        {
            int retriesRemaining = prtgClient.RetryCount;

            do
            {
                try
                {
                    var response = await webClient.GetAsync(url.Url).ConfigureAwait(false);

                    var responseText = responseParser == null ? await response.Content.ReadAsStringAsync().ConfigureAwait(false) : await responseParser(response);

                    ValidateHttpResponse(response, responseText);

                    return(responseText);
                }
                catch (HttpRequestException ex) //todo: test making invalid requests
                {
                    var inner = ex.InnerException as WebException;

                    var result = HandleRequestException(inner, ex, url, ref retriesRemaining, null);

                    if (!result)
                    {
                        throw;
                    }
                }
            } while (true);
        }
示例#2
0
        private string ExecuteRequest(PrtgUrl url, Func <HttpResponseMessage, string> responseParser = null)
        {
            int retriesRemaining = prtgClient.RetryCount;

            do
            {
                try
                {
                    var response = webClient.GetSync(url.Url).Result;

                    var responseText = responseParser == null?response.Content.ReadAsStringAsync().Result : responseParser(response);

                    ValidateHttpResponse(response, responseText);

                    return(responseText);
                }
                catch (Exception ex) when(ex is AggregateException || ex is TaskCanceledException || ex is HttpRequestException)
                {
                    var result = HandleRequestException(ex.InnerException?.InnerException, ex.InnerException, url, ref retriesRemaining, () =>
                    {
                        if (ex.InnerException != null)
                        {
                            throw ex.InnerException;
                        }
                    });

                    if (!result)
                    {
                        throw;
                    }
                }
            } while (true);
        }
示例#3
0
        private async Task <string> ExecuteRequestAsync(PrtgUrl url, CancellationToken token, Func <HttpResponseMessage, Task <string> > responseParser = null)
        {
            prtgClient.Log($"Asynchronously executing request {url.Url}", LogLevel.Request);

            int retriesRemaining = prtgClient.RetryCount;

            do
            {
                try
                {
                    if (token == CancellationToken.None && DefaultCancellationToken != CancellationToken.None)
                    {
                        token = DefaultCancellationToken;
                    }

                    var response = await webClient.GetAsync(url.Url, token).ConfigureAwait(false);

                    string responseText = null;

                    if (responseParser != null)
                    {
                        responseText = await responseParser(response).ConfigureAwait(false);
                    }

                    if (responseText == null)
                    {
                        responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                    }

                    prtgClient.Log(responseText, LogLevel.Response);

                    ValidateHttpResponse(response, responseText);

                    return(responseText);
                }
                catch (HttpRequestException ex)
                {
                    var inner = ex.InnerException as WebException;

                    var result = HandleRequestException(inner, ex, url, ref retriesRemaining, null);

                    if (!result)
                    {
                        throw;
                    }
                }
                catch (TaskCanceledException ex)
                {
                    //If the token we specified was cancelled, throw the TaskCanceledException
                    if (token.IsCancellationRequested)
                    {
                        throw;
                    }

                    //Otherwise, a token that was created internally for use with timing out was cancelled, so throw a timeout exception
                    throw new TimeoutException($"The server timed out while executing request.", ex);
                }
            } while (true);
        }
示例#4
0
        internal async Task <string> ExecuteRequestAsync(HtmlFunction function, Parameters.Parameters parameters)
        {
            var url = new PrtgUrl(prtgClient.Server, prtgClient.UserName, prtgClient.PassHash, function, parameters);

            var response = await ExecuteRequestAsync(url);

            return(response);
        }
示例#5
0
        internal string ExecuteRequest(HtmlFunction function, Parameters.Parameters parameters)
        {
            var url = new PrtgUrl(prtgClient.Server, prtgClient.UserName, prtgClient.PassHash, function, parameters);

            var response = ExecuteRequest(url);

            return(response);
        }
示例#6
0
        internal string ExecuteRequest(CommandFunction function, Parameters.Parameters parameters, Func <HttpResponseMessage, string> responseParser = null)
        {
            var url = new PrtgUrl(prtgClient.Server, prtgClient.UserName, prtgClient.PassHash, function, parameters);

            var response = ExecuteRequest(url, responseParser);

            return(response);
        }
示例#7
0
        internal async Task <XDocument> ExecuteRequestAsync(XmlFunction function, Parameters.Parameters parameters)
        {
            var url = new PrtgUrl(prtgClient.Server, prtgClient.UserName, prtgClient.PassHash, function, parameters);

            var response = await ExecuteRequestAsync(url).ConfigureAwait(false);

            return(XDocument.Parse(XDocumentHelpers.SanitizeXml(response)));
        }
示例#8
0
        internal XDocument ExecuteRequest(XmlFunction function, Parameters.Parameters parameters)
        {
            var url = new PrtgUrl(prtgClient.Server, prtgClient.UserName, prtgClient.PassHash, function, parameters);

            var response = ExecuteRequest(url);

            return(XDocument.Parse(XDocumentHelpers.SanitizeXml(response)));
        }
示例#9
0
        private string ExecuteRequest(PrtgUrl url, Func <HttpResponseMessage, string> responseParser = null)
        {
            prtgClient.Log($"Synchronously executing request {url.Url}", LogLevel.Request);

            int retriesRemaining = prtgClient.RetryCount;

            do
            {
                try
                {
                    var response = webClient.GetSync(url.Url).Result;

                    string responseText = null;

                    if (responseParser != null)
                    {
                        responseText = responseParser(response);
                    }

                    if (responseText == null)
                    {
                        responseText = response.Content.ReadAsStringAsync().Result;
                    }

                    prtgClient.Log(responseText, LogLevel.Response);

                    ValidateHttpResponse(response, responseText);

                    return(responseText);
                }
                catch (Exception ex) when(ex is AggregateException || ex is TaskCanceledException || ex is HttpRequestException)
                {
                    var innerException = ex.InnerException;

                    if (ex.InnerException is TaskCanceledException)
                    {
                        innerException = new TimeoutException($"The server timed out while executing request.", ex.InnerException);
                    }

                    var result = HandleRequestException(ex.InnerException?.InnerException, innerException, url, ref retriesRemaining, () =>
                    {
                        if (innerException != null)
                        {
                            throw innerException;
                        }
                    });

                    if (!result)
                    {
                        throw;
                    }
                }
            } while (true);
        }
示例#10
0
        private async Task <string> ExecuteRequestAsync(PrtgUrl url, Func <HttpResponseMessage, Task <string> > responseParser = null)
        {
            prtgClient.Log($"Asynchronously executing request {url.Url}", LogLevel.Request);

            int retriesRemaining = prtgClient.RetryCount;

            do
            {
                try
                {
                    var response = await webClient.GetAsync(url.Url).ConfigureAwait(false);

                    string responseText = null;

                    if (responseParser != null)
                    {
                        responseText = await responseParser(response).ConfigureAwait(false);
                    }

                    if (responseText == null)
                    {
                        responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                    }

                    prtgClient.Log(responseText, LogLevel.Response);

                    ValidateHttpResponse(response, responseText);

                    return(responseText);
                }
                catch (HttpRequestException ex)
                {
                    var inner = ex.InnerException as WebException;

                    var result = HandleRequestException(inner, ex, url, ref retriesRemaining, null);

                    if (!result)
                    {
                        throw;
                    }
                }
                catch (TaskCanceledException ex)
                {
                    throw new TimeoutException($"The server timed out while executing request.", ex);
                }
            } while (true);
        }
示例#11
0
        internal async Task ExecuteRequestAsync(CommandFunction function, Parameters.Parameters parameters)
        {
            var url = new PrtgUrl(prtgClient.Server, prtgClient.UserName, prtgClient.PassHash, function, parameters);

            var response = await ExecuteRequestAsync(url).ConfigureAwait(false);
        }
示例#12
0
        private bool HandleRequestException(Exception innerMostEx, Exception fallbackHandlerEx, PrtgUrl url, ref int retriesRemaining, Action thrower)
        {
            if (innerMostEx != null)
            {
                if (retriesRemaining > 0)
                {
                    prtgClient.HandleEvent(prtgClient.retryRequest, new RetryRequestEventArgs(innerMostEx, url.Url, retriesRemaining));
                }
                else
                {
                    throw innerMostEx;
                }
            }
            else
            {
                if (fallbackHandlerEx != null && retriesRemaining > 0)
                {
                    prtgClient.HandleEvent(prtgClient.retryRequest, new RetryRequestEventArgs(fallbackHandlerEx, url.Url, retriesRemaining));
                }
                else
                {
                    thrower();

                    return(false);
                }
            }

            if (retriesRemaining > 0)
            {
                var attemptsMade = prtgClient.RetryCount - retriesRemaining + 1;
                var delay        = prtgClient.RetryDelay * attemptsMade;

                Thread.Sleep(delay * 1000);
            }

            retriesRemaining--;

            return(true);
        }
示例#13
0
        private string ExecuteRequest(PrtgUrl url, CancellationToken token, Func <HttpResponseMessage, string> responseParser = null)
        {
            prtgClient.Log($"Synchronously executing request {url.Url}", LogLevel.Request);

            int retriesRemaining = prtgClient.RetryCount;

            do
            {
                try
                {
                    if (token == CancellationToken.None && DefaultCancellationToken != CancellationToken.None)
                    {
                        token = DefaultCancellationToken;
                    }

                    var response = webClient.GetSync(url.Url, token).Result;

                    string responseText = null;

                    if (responseParser != null)
                    {
                        responseText = responseParser(response);
                    }

                    if (responseText == null)
                    {
                        responseText = response.Content.ReadAsStringAsync().Result;
                    }

                    prtgClient.Log(responseText, LogLevel.Response);

                    ValidateHttpResponse(response, responseText);

                    return(responseText);
                }
                catch (Exception ex) when(ex is AggregateException || ex is TaskCanceledException || ex is HttpRequestException)
                {
                    var innerException = ex.InnerException;

                    if (innerException is TaskCanceledException)
                    {
                        //If the token we specified was cancelled, throw the TaskCanceledException
                        if (token.IsCancellationRequested)
                        {
                            throw innerException;
                        }

                        //Otherwise, a token that was created internally for use with timing out was cancelled, so throw a timeout exception
                        innerException = new TimeoutException($"The server timed out while executing request.", ex.InnerException);
                    }

                    var result = HandleRequestException(ex.InnerException?.InnerException, innerException, url, ref retriesRemaining, () =>
                    {
                        if (innerException != null)
                        {
                            throw innerException;
                        }
                    });

                    if (!result)
                    {
                        throw;
                    }
                }
            } while (true);
        }
示例#14
0
        private async Task <PrtgResponse> ExecuteRequestAsync(PrtgUrl url, CancellationToken token, Func <HttpResponseMessage, Task <PrtgResponse> > responseParser = null)
        {
            prtgClient.Log($"Asynchronously executing request {url.Url}", LogLevel.Request);

            int retriesRemaining = prtgClient.RetryCount;

            do
            {
                try
                {
                    if (token == CancellationToken.None && DefaultCancellationToken != CancellationToken.None)
                    {
                        token = DefaultCancellationToken;
                    }

                    var responseMessage = await webClient.GetAsync(url.Url, token).ConfigureAwait(false);

                    PrtgResponse responseContent = null;

                    if (responseParser != null)
                    {
                        responseContent = await responseParser(responseMessage).ConfigureAwait(false);
                    }

                    if (responseContent == null)
                    {
                        responseContent = await GetAppropriateResponseAsync(responseMessage, prtgClient.LogLevel).ConfigureAwait(false);
                    }

                    //If we needed to log response, GetAppropriateResponseAsync will have handled this
                    if (responseContent.Type == PrtgResponseType.String)
                    {
                        prtgClient.Log(responseContent.StringValue, LogLevel.Response);
                    }

                    ValidateHttpResponse(responseMessage, responseContent);

                    return(responseContent);
                }
                catch (HttpRequestException ex)
                {
                    var inner = ex.InnerException as WebException;

                    var result = HandleRequestException(inner, ex, url, ref retriesRemaining, null);

                    if (!result)
                    {
                        throw;
                    }
                }
                catch (TaskCanceledException ex)
                {
                    //If the token we specified was cancelled, throw the TaskCanceledException
                    if (token.IsCancellationRequested)
                    {
                        throw;
                    }

                    //Otherwise, a token that was created internally for use with timing out was cancelled, so throw a timeout exception
                    throw new TimeoutException($"The server timed out while executing request.", ex);
                }
            } while (true); //Keep retrying as long as we have retries remaining
        }
示例#15
0
        private PrtgResponse ExecuteRequest(PrtgUrl url, CancellationToken token, Func <HttpResponseMessage, PrtgResponse> responseParser = null)
        {
            prtgClient.Log($"Synchronously executing request {url.Url}", LogLevel.Request);

            int retriesRemaining = prtgClient.RetryCount;

            do
            {
                try
                {
                    if (token == CancellationToken.None && DefaultCancellationToken != CancellationToken.None)
                    {
                        token = DefaultCancellationToken;
                    }

                    var responseMessage = webClient.GetSync(url.Url, token).Result;

                    PrtgResponse responseContent = null;

                    if (responseParser != null)
                    {
                        responseContent = responseParser(responseMessage);
                    }

                    if (responseContent == null)
                    {
                        responseContent = GetAppropriateResponse(responseMessage, prtgClient.LogLevel);
                    }

                    //If we needed to log response, GetAppropriateResponse will have handled this
                    if (responseContent.Type == PrtgResponseType.String)
                    {
                        prtgClient.Log(responseContent.StringValue, LogLevel.Response);
                    }

                    ValidateHttpResponse(responseMessage, responseContent);

                    return(responseContent);
                }
                catch (Exception ex) when(ex is AggregateException || ex is TaskCanceledException || ex is HttpRequestException)
                {
                    var innerException = ex.InnerException;

                    if (innerException is TaskCanceledException)
                    {
                        //If the token we specified was cancelled, throw the TaskCanceledException
                        if (token.IsCancellationRequested)
                        {
                            throw innerException;
                        }

                        //Otherwise, a token that was created internally for use with timing out was cancelled, so throw a timeout exception
                        innerException = new TimeoutException($"The server timed out while executing request.", ex.InnerException);
                    }

                    var result = HandleRequestException(ex.InnerException?.InnerException, innerException, url, ref retriesRemaining, () =>
                    {
                        if (innerException != null)
                        {
                            throw innerException;
                        }
                    });

                    if (!result)
                    {
                        throw;
                    }
                }
            } while (true); //Keep retrying as long as we have retries remaining
        }