Ejemplo n.º 1
0
        /// <summary>
        /// Search for packages in the CKAN repository and just return tag counts with caching.
        /// </summary>
        /// <param name="parameters">Provides that parameters to use in the search.</param>
        /// <param name="settings">The cache settings.</param>
        /// <returns>Search results</returns>
        public List <Tag> SearchPackagesGetTagCounts(PackageSearchParameters parameters, CacheSettings settings)
        {
            // Execute the search request
            parameters.Offset = 0;
            parameters.Limit  = GetPackageCount(parameters, settings);

            PackageSearchResponse <Package> response = SearchPackages <Package>(parameters, settings);

            List <Package> packages = response.Results;

            List <Tag> tags = TagHelper.GetTagCounts(packages);

            return(tags);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Search for packages in the CKAN repository with caching.
        /// </summary>
        /// <param name="parameters">Provides that parameters to use in the search.</param>
        /// <param name="settings">The cache settings.</param>
        /// <returns>Search results</returns>
        public PackageSearchResponse <T> SearchPackages <T>(PackageSearchParameters parameters, CacheSettings settings)
        {
            var request = new RestRequest();

            request.Resource = "2/search/package";

            // Apply query parameter
            foreach (var group in parameters.Groups)
            {
                request.AddParameter("q", "organization:" + group);
            }

            // Apply title parameter
            if (!String.IsNullOrEmpty(parameters.Title))
            {
                request.AddParameter("title", parameters.Title);
            }

            // Apply tag parameters
            foreach (var tag in parameters.Tags)
            {
                request.AddParameter("tags", tag);
            }

            // Apply notes parameter
            if (!String.IsNullOrEmpty(parameters.Notes))
            {
                request.AddParameter("notes", parameters.Notes);
            }

            //// Apply group parameters
            //foreach (var group in parameters.Groups)
            //{
            //    request.AddParameter("groups", group);
            //}

            // Apply author parameter
            if (!String.IsNullOrEmpty(parameters.Author))
            {
                request.AddParameter("author", parameters.Author);
            }

            // Apply update_frequency parameter
            if (!String.IsNullOrEmpty(parameters.UpdateFrequency))
            {
                request.AddParameter("update_frequency", parameters.UpdateFrequency);
            }

            // Apply maintainer parameter
            if (!String.IsNullOrEmpty(parameters.Maintainer))
            {
                request.AddParameter("maintainer", parameters.Maintainer);
            }

            // Apply all_fields parameter
            if (typeof(T) == typeof(Package))
            {
                request.AddParameter("all_fields", 1);
            }

            // Apply filter_by_openness parameter
            if (parameters.FilterByOpenness)
            {
                request.AddParameter("filter_by_openness", "1");
            }

            // Apply filter_by_downloadable parameter
            if (parameters.FilterByDownloadable)
            {
                request.AddParameter("filter_by_downloadable", "1");
            }

            // Apply order_by parameter
            if (!String.IsNullOrEmpty(parameters.OrderBy))
            {
                request.AddParameter("order_by", parameters.OrderBy);
            }

            // Set the offset and limit parameters
            if (parameters.Offset > -1)
            {
                request.AddParameter("offset", parameters.Offset);
            }

            if (parameters.Limit > -1)
            {
                request.AddParameter("limit", parameters.Limit);
            }

            // Execute the search request
            PackageSearchResponse <T> response = Execute <PackageSearchResponse <T> >(request, settings);

            // If no results, return an empty results object
            if (response == null)
            {
                response = new PackageSearchResponse <T>();
            }
            else
            {
                if (typeof(T) == typeof(Package) && parameters.AggregateTagCounts && response.Results.Count > 0)
                {
                    if (response.Results.Count == response.Count)
                    {
                        // If we have all of the results on the current page just aggregate the tags
                        if (typeof(T) == typeof(Package))
                        {
                            List <Package> packages = response.Results.ConvertAll(item => (Package)((object)item));
                            response.Tags = TagHelper.GetTagCounts(packages);
                        }
                    }
                    else
                    {
                        // If we don't have all of the results submit another request to
                        // get all of the tags
                        response.Tags = SearchPackagesGetTagCounts(parameters, settings);
                    }
                }
            }

            return(response);
        }