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) { 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)); }
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; } }