コード例 #1
0
        /// <summary>
        /// Wrapper around REST API for making data request.
        /// </summary>
        /// <typeparam name="TSchema">Schema to use</typeparam>
        /// <param name="config">Query configuration.</param>
        /// <param name="maxRecords">Upper limit for records returned.</param>
        /// <param name="pageIndex">The zero-based index of the page that corresponds to the items to retrieve.</param>
        /// <param name="parser">IParser implementation for interpreting results.</param>
        /// <returns>Strongly typed list of results.</returns>
        protected override async Task <IEnumerable <TSchema> > GetDataAsync <TSchema>(BingSearchConfig config, int maxRecords, int pageIndex, Parsers.IParser <TSchema> parser)
        {
            var countryValue      = config.Country.GetStringValue();
            var languageValue     = config.Language.GetStringValue();
            var languageParameter = string.IsNullOrEmpty(languageValue) ? string.Empty : $"language:{languageValue}+";

            if (string.IsNullOrEmpty(countryValue))
            {
                if (CultureInfo.CurrentCulture.IsNeutralCulture)
                {
                    countryValue = BingCountry.None.GetStringValue();
                }
                else
                {
                    countryValue = CultureInfo.CurrentCulture.Name.Split('-')[1].ToLower();
                }
            }

            var locParameter       = $"loc:{countryValue}+";
            var queryTypeParameter = string.Empty;

            switch (config.QueryType)
            {
            case BingQueryType.Search:
                queryTypeParameter = string.Empty;
                break;

            case BingQueryType.News:
                queryTypeParameter = "/news";
                break;
            }

            var uri = new Uri($"{BaseUrl}{queryTypeParameter}/search?q={locParameter}{languageParameter}{WebUtility.UrlEncode(config.Query)}&format=rss&count={maxRecords}&first={(pageIndex * maxRecords) + (pageIndex > 0 ? 1 : 0)}");

            using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri))
            {
                using (var response = await HttpClient.SendAsync(request).ConfigureAwait(false))
                {
                    string data = response.Content == null ? null : await response.Content.ReadAsStringAsync();

                    if (response.IsSuccessStatusCode && !string.IsNullOrEmpty(data))
                    {
                        return(parser.Parse(data));
                    }

                    throw new RequestFailedException(response.StatusCode, data);
                }
            }
        }
コード例 #2
0
        public async Task <IEnumerable <TSchema> > LoadDataAsync <TSchema>(TConfig config, int maxRecords, int pageIndex, Parsers.IParser <TSchema> parser)
            where TSchema : Parsers.SchemaBase
        {
            if (config == null)
            {
                throw new ConfigNullException();
            }

            if (parser == null)
            {
                throw new ParserNullException();
            }

            ValidateConfig(config);

            var result = await GetDataAsync(config, maxRecords, pageIndex, parser);

            if (result != null)
            {
                return(result
                       .Take(maxRecords)
                       .ToList());
            }

            return(Array.Empty <TSchema>());
        }
コード例 #3
0
 /// <summary>
 /// Derived classes will have to implement this method to return provider data
 /// </summary>
 /// <param name="config">Configuration to use</param>
 /// <param name="maxRecords">Maximum number of records to return</param>
 /// <param name="pageIndex">The zero-based index of the page that corresponds to the items to retrieve.</param>
 /// <param name="parser">Parser to use</param>
 /// <typeparam name="TSchema">Schema defining data returned</typeparam>
 /// <returns>List of data</returns>
 protected abstract Task <IEnumerable <TSchema> > GetDataAsync <TSchema>(TConfig config, int maxRecords, int pageIndex, Parsers.IParser <TSchema> parser)
     where TSchema : Parsers.SchemaBase;
コード例 #4
0
 public Provider(Parsers.IParser configParser)
 {
     _configParser = configParser;
 }