コード例 #1
0
        /// <summary>
        /// Returns a list of pages that are related to the provided topic
        /// </summary>
        /// <param name="topic">Topic name (can be partial page name)</param>
        /// <returns>List of page information metadata. May be empty, but never NULL</returns>
        public List <PageMetadata> GetPagesRelatedTo(string topic)
        {
            if (string.IsNullOrWhiteSpace(topic))
            {
                throw new ArgumentNullException(nameof(topic));
            }

            List <PageMetadata> results = null;
            string resultData           = GET($"page/related/{topic}");

            if (!string.IsNullOrWhiteSpace(resultData))
            {
                PageMetadataList result = JsonSerializer.Deserialize <PageMetadataList>(resultData);
                results = result?.Pages;
            }

            return(results ?? new List <PageMetadata>());
        }
コード例 #2
0
        /// <summary>
        /// Gets metadata for a random page from Wikipedia.
        /// </summary>
        /// <param name="formatType">Format of the data to be returned. This is the data format matching one of the other query types</param>
        /// <returns>Data in the requested format (as dynamic). Caller must use/cast to the right format. Will be NULL if there was a problem</returns>
        public dynamic GetRandomPage(MetadataFormatType formatType = MetadataFormatType.Title)
        {
            if (!Enum.IsDefined(typeof(MetadataFormatType), formatType))
            {
                throw new ArgumentOutOfRangeException(nameof(formatType));
            }

            string paramFormatType = Enum.GetName(typeof(MetadataFormatType), formatType).ToLower().Replace("_", "-");
            string resultData      = GET($"page/random/{paramFormatType}");

            if (!string.IsNullOrWhiteSpace(resultData))
            {
                switch (formatType)
                {
                case MetadataFormatType.Title:
                    return(JsonSerializer.Deserialize <PageBasicMetadata>(resultData));

                case MetadataFormatType.Related:
                    PageMetadataList result = JsonSerializer.Deserialize <PageMetadataList>(resultData);
                    return(result?.Pages ?? new List <PageMetadata>());

                case MetadataFormatType.Summary:
                    return(JsonSerializer.Deserialize <PageMetadata>(resultData));

                case MetadataFormatType.Html:
                    return(resultData);

                case MetadataFormatType.Mobile_Sections:
                    return(JsonSerializer.Deserialize <PageMobileInfo>(resultData));

                case MetadataFormatType.Mobile_Sections_Lead:
                    return(JsonSerializer.Deserialize <PageMobileLead>(resultData));
                }
            }

            return(null);
        }