Exemplo n.º 1
0
        /// <summary>
        /// Resets the doc regardless if <see cref="ExpiresOn"/> has expired yet.
        /// Note, this still *does* do a conditional get with the <see cref="ETag"/>
        /// (where the ETag has to be set and Doc has to be not nulle). To ignore that
        /// as well set <paramref name="ignoreEtag"/> to true.
        /// </summary>
        /// <param name="docUrl">The url to download. Enter this to override
        /// the current <see cref="DocUrl"/>.</param>
        /// <param name="ignoreEtag">Set to true to ignore the etag and force
        /// a full download no matter what.</param>
        /// <returns>True if a full reset occurred, false if conditional get
        /// determined current document was already up to date.</returns>
        public async Task <bool> Reset(string docUrl = null, bool ignoreEtag = false)
        {
            if (docUrl.IsNulle())
            {
                docUrl = DocUrl.NullIfEmptyTrimmed();
            }

            if (docUrl.IsNulle())
            {
                throw new ArgumentException($"{nameof(DocUrl)} was null.");
            }

            if (!Uri.TryCreate(docUrl, UriKind.Absolute, out Uri uriResult))
            {
                throw new ArgumentException($"{nameof(DocUrl)} was not a valid URI.");
            }

            string etag = ignoreEtag || Doc.IsNulle()             // never even set etag if doc is null
                                ? null
                                : ETag;

            string _doc = null;

            var notModProps = new HttpNotModifiedProperties()
            {
                ETag = etag
            };

            var httpRespInfo = await XHttp.GetAsync(
                DocUrl,
                settings : notModProps,
                timeout : TimeSpan.FromSeconds(10));

            if (httpRespInfo.NotModified)
            {
                return(false);                // NotModified can never be true if we didn't send in the etag, but...
            }
            _doc = httpRespInfo.Failed
                                ? null
                                : httpRespInfo.GetContentString();

            bool isValid = ValidateDownload(_doc);

            if (!isValid)
            {
                throw new Exception("Downloaded string is null or invalid.");
            }

            Doc = _doc;

            this.ETag = httpRespInfo.ETag;

            LastUpdated = DateTimeOffset.UtcNow;

            return(true);
        }
Exemplo n.º 2
0
        public async Task <SimpleFeed> SetFeed(
            bool saveToFileIfPossible             = true,
            Action <SFFeedSettings> alterSettings = null)
        {
            if (_testFeedPath.NotNulle())
            {
                _feedContent = await File.ReadAllTextAsync(_testFeedPath);
            }
            else if (_testFeedUrl.NotNulle())
            {
                _feedContent = await XHttp.GetStringAsync(_testFeedUrl);

                if (saveToFileIfPossible && _feedContent.NotNulle() && _testFeedPath.NotNulle())
                {
                    await File.WriteAllTextAsync(_testFeedPath, _feedContent);
                }
            }

            Assert.True(_feedContent.NotNulle());

            var settings = new SFFeedSettings()
            {
                KeepXmlDocument             = true,
                ConvertCategoryUrlsToLinks  = true,
                ConvertContentUrlsToLinks   = true,
                AlterUTCDatesToThisTimezone = TimeZoneInfo.Local,
                //GetImageUrlsFromContentImgTag = true,
            };

            alterSettings?.Invoke(settings);

            _feed = new SimpleFeed {
                Settings = settings
            };

            return(_feed);
        }