private void configure_webrequest(string uri = null, RequestContainer rc = null)
 {
     A.CallTo(() => HttpRequestFactory.CreateRequest(null))
     .WithAnyArguments()
     .ReturnsLazily(
         x =>
     {
         uri         = uri ?? x.GetArgument <string>(0);
         var request = new HttpRequestFactory().CreateRequest(uri);
         if (rc != null)
         {
             rc.Request = request;
         }
         return(request);
     });
 }
Exemple #2
0
        protected async Task <T> Get <T>(string endpoint)
        {
            var message = _requestFactory.CreateRequest(HttpMethod.Get, endpoint, "");

            using (HttpClient httpClient = new HttpClient())
            {
                var result = await httpClient.SendAsync(message);

                var data = await result.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(data))
                {
                    return(JsonConvert.DeserializeObject <T>(data));
                }
            }

            return(default(T));
        }
Exemple #3
0
        public RemoteContentResponse ReadRemoteSource(LocalFeedInfo feedInfo)
        {
            _log.Debug("Trying to read remote source '{0}' (etag: {1}, last fetch: {2})", feedInfo.Url, feedInfo.Etag, feedInfo.LastFetch);

            try
            {
                if (feedInfo.LastFetch.HasValue && _configProvider.ProvideConfig().PrefetchHeadRequest)
                {
                    var head_request = _httpRequestFactory.CreateRequest(feedInfo.Url);
                    head_request.Method = "HEAD";
                    using (var head_response = (HttpWebResponse)head_request.GetResponseEx())
                    {
                        DateTime last_modified = head_response.LastModified.ToUniversalTime();
                        DateTime last_fetch    = feedInfo.LastFetch.Value.ToUniversalTime();
                        if (last_modified < last_fetch)
                        {
                            _log.Debug("HEAD request indicates that there is no new content available (last_modified={0}, last_fetch={1}), returning NotModified.", last_modified, last_fetch);
                            return(RemoteContentResponse.NotModified(head_response.StatusCode));
                        }
                    }
                }

                var get_request = _httpRequestFactory.CreateRequest(feedInfo.Url);
                if (feedInfo.LastFetch != null)
                {
                    get_request.IfModifiedSince = feedInfo.LastFetch.Value;
                }

                if (string.IsNullOrWhiteSpace(feedInfo.Etag) == false)
                {
                    get_request.Headers[HttpRequestHeader.IfNoneMatch] = feedInfo.Etag;
                }

                using (var response = (HttpWebResponse)get_request.GetResponseEx())
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string etag = response.Headers[HttpResponseHeader.ETag];

                        Stream responseStream = response.GetResponseStream();
                        using (var streamReader = new StreamReader(responseStream))
                        {
                            string responseText = streamReader.ReadToEnd();

                            responseText = _contentPreProcessor.PreProcess(responseText);

                            using (var stringReader = new StringReader(responseText))
                            {
                                using (var reader = XmlReader.Create(stringReader))
                                {
                                    var feed = SyndicationFeed.Load(reader);

                                    _log.Info("Returning new content for remote source '{0}' with {1} items", feedInfo.Url, feed.Items.Count());

                                    return(new RemoteContentResponse(true, etag, feed, response.StatusCode));
                                }
                            }
                        }
                    }
                    else
                    {
                        string message = string.Format("No new content for remote source '{0}', response status is {1}", feedInfo.Url, response.StatusCode);

                        if (response.StatusCode == HttpStatusCode.NotModified)
                        {
                            _log.Debug(message);
                        }
                        else
                        {
                            _log.Warn(message);
                        }

                        return(RemoteContentResponse.NotModified(response.StatusCode));
                    }
                }
            }
            catch (Exception exc)
            {
                _log.ErrorException(string.Format("Failed to read remote source '{0}'", feedInfo.Url), exc);

                throw new CannotReachRemoteSourceException(feedInfo.Url, exc);
            }
        }