public async Task <PushResponse> GetDelayedAsync(string requestUrl, string verb, string content,
                                                         int staggeredDelay, int requestGrouping,
                                                         CancellationToken token)
        {
            await Task.Delay(staggeredDelay, token);

            _logger.LogResponse(requestGrouping, BaseUrl + requestUrl, true, "", staggeredDelay, "");

            return(new PushResponse
            {
                IsSuccessful = true,
                RequestUrl = BaseUrl + requestUrl,
                RequestGrouping = requestGrouping,
                RequestTime = DateTime.UtcNow,
                TimeTakenMilliseconds = staggeredDelay
            });
        }
Beispiel #2
0
        public async Task <PushResponse> GetDelayedAsync(string relativeRequestUrl, string verb, string content,
                                                         int staggeredDelay,
                                                         int requestGrouping, CancellationToken token)
        {
            await Task.Delay(staggeredDelay, token).ConfigureAwait(false);

            var pushResponse = new PushResponse
            {
                RequestGrouping = requestGrouping,
                RequestTime     = DateTime.UtcNow
            };

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            try
            {
                //var response = await _httpClient.GetAsync(relativeRequestUrl, token)
                //    .ConfigureAwait(continueOnCapturedContext: false);
                HttpResponseMessage response = null;

                switch (verb)
                {
                case "POST":
                {
                    var Content = new StringContent(content, Encoding.UTF8,
                                                    _httpClient.DefaultRequestHeaders.Accept.ToString());
                    response = await _httpClient.PostAsync(_httpClient.BaseAddress, Content, token)
                               .ConfigureAwait(false);

                    break;
                }

                case "GET":
                    response = await _httpClient.GetAsync(relativeRequestUrl, token)
                               .ConfigureAwait(false);

                    break;

                case "PUT":
                {
                    var Content = new StringContent(content, Encoding.UTF8, "application/json");
                    response = await _httpClient.PutAsync(_httpClient.BaseAddress, Content, token)
                               .ConfigureAwait(false);

                    break;
                }

                default:     //Verb GET
                    response = await _httpClient.GetAsync(relativeRequestUrl, token)
                               .ConfigureAwait(false);

                    break;
                }


                stopwatch.Stop();

                pushResponse = PopulatePushResponse(pushResponse, response, stopwatch);
            }
            catch (TimeoutException)
            {
                pushResponse.IsSuccessful = false;
                pushResponse.Body         = "Timeout";
            }
            catch (Exception ex)
            {
                pushResponse.IsSuccessful = false;
                pushResponse.Body        += ex.GetType().Name;
            }

            _logger.LogResponse(requestGrouping, pushResponse.RequestUrl, pushResponse.IsSuccessful,
                                pushResponse.ResponseHeaders, pushResponse.TimeTakenMilliseconds, pushResponse.Body);

            return(pushResponse);
        }