public static string CreateUrl(IParameters parameters, bool truncate = true)
        {
            var request = new PrtgRequestMessage(new ConnectionDetails("prtg.example.com", "username", "12345678"), XmlFunction.TableData, parameters);

            if (truncate)
            {
                var suffix = "https://prtg.example.com/api/table.xml?";
                var prefix = $"&username=username&passhash=12345678";
                try
                {
                    Assert.IsTrue(request.Url.StartsWith(suffix), "URL did not start with suffix");
                }
                catch
                {
                    Assert.IsTrue(request.Url.StartsWith(suffix.Substring(0, suffix.Length - 1) + "&"));
                }
                Assert.IsTrue(request.Url.EndsWith(prefix), "URL did not end with prefix");

                var length = request.Url.Length - (suffix.Length + prefix.Length);

                if (length == -1)
                {
                    return(string.Empty);
                }

                return(request.Url.Substring(suffix.Length, length));
            }

            return(request.Url);
        }
        public Task <HttpResponseMessage> SendAsync(PrtgRequestMessage request, CancellationToken token)
        {
            if (successfulUrls.Count < 1)
            {
                successfulUrls.Add(request.Url);
                return(realWebClient.SendAsync(request, token));
            }
            else
            {
                if (ignoreUrls.Count < 2)
                {
                    if (!ignoreUrls.Contains(request.Url))
                    {
                        ignoreUrls.Add(request.Url);
                    }
                }
                else
                {
                    Assert.IsTrue(ignoreUrls.Contains(request.Url), $"IgnoreUrls did not contain address '{request.ToString()}'");
                }

                var exception = new HttpRequestException("Outer Exception", new WebException("Inner Exception"));

                throw exception;
            }
        }
Esempio n. 3
0
        private void ExecuteOperation(PrtgObject obj, string url)
        {
            var server = PrtgRequestMessage.AddUrlPrefix(client.Server);

            if (ShouldProcess($"'{obj}' (ID: {obj.Id}, Type: {obj.Type})"))
            {
                ExecuteOperation(() => Process.Start($"{server}{url}"), $"Opening {obj.GetTypeDescription()} '{obj.Name}'");
            }
        }
Esempio n. 4
0
        public async Task <HttpResponseMessage> SendAsync(PrtgRequestMessage request, CancellationToken token)
        {
            var frames = new System.Diagnostics.StackTrace().GetFrames();

            if (SwitchContext)
            {
                await Task.Yield();
            }

            var address = request.ToString();

            var statusCode = GetStatusCode();

            if (token.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }

            var method = frames.Last(f => f.GetMethod().Module.Name == "PrtgAPI.dll").GetMethod();

            var responseStr = string.Empty;

            if (method.Name.StartsWith("Stream"))
            {
                var streamer = response as IWebStreamResponse;

                if (streamer != null)
                {
                    responseStr = await streamer.GetResponseTextStream(address).ConfigureAwait(false);
                }
                else
                {
                    throw new NotImplementedException($"Could not stream as response does not implement {nameof(IWebStreamResponse)}");
                }
            }
            else
            {
                //If the method is in fact async, or is called as part of a streaming method, we execute the request as async
                //This implies we do not consider nested streaming methods to be an implemented scenario
                responseStr = await Task.FromResult(response.GetResponseText(ref address)).ConfigureAwait(false);
            }
            //we should check whether the method is a streamer or an async, and if its async we should to task.fromresult

            var message = new HttpResponseMessage(statusCode)
            {
                Content        = new StringContent(responseStr),
                RequestMessage = new HttpRequestMessage()
                {
                    RequestUri = new Uri(address)
                }
            };

            SetContentHeaders(message);

            return(message);
        }
        public Task <HttpResponseMessage> SendSync(PrtgRequestMessage request, CancellationToken token)
        {
            if (!synchronous)
            {
                return(realWebClient.SendSync(request, token));
            }
            else
            {
                var exception = new HttpRequestException("Outer Exception", new WebException("Inner Exception"));

                throw exception;
            }
        }
Esempio n. 6
0
        public Task <HttpResponseMessage> SendSync(PrtgRequestMessage request, CancellationToken token)
        {
            var address = request.ToString();

            var statusCode = GetStatusCode();

            if (token.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }

            var message = new HttpResponseMessage(statusCode)
            {
                Content        = new StringContent(response.GetResponseText(ref address)),
                RequestMessage = new HttpRequestMessage()
                {
                    RequestUri = new Uri(address)
                }
            };

            SetContentHeaders(message);

            return(Task.FromResult(message));
        }
Esempio n. 7
0
        private void ExecuteOperation(PrtgObject obj, string url)
        {
            var server = PrtgRequestMessage.AddUrlPrefix(client.Server);

            ExecuteOperation(() => Process.Start($"{server}{url}"), $"Opening {obj.GetTypeDescription()} '{obj.Name}'");
        }
        private void Server_Prefix(string server, string prefixedServer)
        {
            var request = new PrtgRequestMessage(new ConnectionDetails(server, "username", "password"), XmlFunction.TableData, new BaseParameters());

            Assert.IsTrue(request.Url.StartsWith(prefixedServer));
        }