Ejemplo n.º 1
0
        public async Task <int> GetPackageCountAsync(CancellationToken token)
        {
            string queryString = string.Format(_searchUriFormat, 0, _pageSize);
            Uri    queryUri    = new Uri(queryString);

            using (HttpClient client = new HttpClient())
                using (HttpResponseMessage response = await client.GetAsync(queryUri, token).ConfigureAwait(false))
                {
                    response.EnsureSuccessStatusCode();
                    string responseText = await response.Content.ReadAsStringAsync(token).ConfigureAwait(false);

                    NuGetPackageSearchResult resultsForPage = NuGetPackageSearchResult.FromJObject(JObject.Parse(responseText));
                    return(resultsForPage.TotalHits);
                }
        }
Ejemplo n.º 2
0
        //property names are explained here: https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource
        internal static NuGetPackageSearchResult FromJObject(JObject entry)
        {
            NuGetPackageSearchResult searchResult = new NuGetPackageSearchResult();

            searchResult.TotalHits = entry.ToInt32("totalHits");
            var dataArray = entry.Get <JArray>("data");

            if (dataArray != null)
            {
                foreach (JToken data in dataArray)
                {
                    JObject?dataObj = data as JObject;
                    if (dataObj != null)
                    {
                        searchResult.Data.Add(NuGetPackageSourceInfo.FromJObject(dataObj));
                    }
                }
            }
            return(searchResult);
        }
Ejemplo n.º 3
0
        public async IAsyncEnumerable <ITemplatePackageInfo> GetCandidatePacksAsync([EnumeratorCancellation] CancellationToken token)
        {
            int  skip      = 0;
            bool done      = false;
            int  packCount = 0;

            int totalPackCount = 0;
            int pageSize       = _pageSize;

            do
            {
                //NuGet search API limit is 3000, so try to get all the packages exceeding the limit.
                if (skip + pageSize > 3000)
                {
                    //get all the packages up to 3000
                    pageSize = skip + pageSize - 3000;
                }
                if (skip >= 3000)
                {
                    //try to get all remaining packages
                    skip     = 3000;
                    pageSize = totalPackCount - 3000;

                    //pageSize limit is 1000
                    //therefore max amount of packages that can be retrieved is 4000.
                    if (pageSize > 1000)
                    {
                        pageSize = 1000;
                    }
                }
                string queryString = string.Format(_searchUriFormat, skip, pageSize);

                Uri queryUri = new Uri(queryString);
                using (HttpClient client = new HttpClient())
                    using (HttpResponseMessage response = await client.GetAsync(queryUri, token).ConfigureAwait(false))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            string responseText = await response.Content.ReadAsStringAsync(token).ConfigureAwait(false);

                            NuGetPackageSearchResult resultsForPage = NuGetPackageSearchResult.FromJObject(JObject.Parse(responseText));
                            totalPackCount = resultsForPage.TotalHits;
                            if (resultsForPage.Data.Count > 0)
                            {
                                skip      += pageSize;
                                packCount += resultsForPage.Data.Count;
                                foreach (NuGetPackageSourceInfo sourceInfo in resultsForPage.Data)
                                {
                                    yield return(sourceInfo);
                                }
                            }
                            //4000 is NuGet limit, stop after 4000 is processed.
                            if (totalPackCount == packCount || totalPackCount > 4000 && packCount == 4000)
                            {
                                if (totalPackCount > 4000)
                                {
                                    Console.WriteLine($"Warning: {totalPackCount} packages were found, but only first 4000 packages can be retrieved. Other packages will be skipped.");
                                }
                                done = true;
                            }
                            else if (skip > 3000 || skip >= totalPackCount)
                            {
                                Console.WriteLine($"Failed to get all search results from NuGet: expected {totalPackCount}, retrieved: {packCount}.");
                                throw new Exception("Failed to get search results from NuGet search API.");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"Unexpected response from NuGet: code {response.StatusCode}, details: {response.ToString()}.");
                            throw new Exception("Failed to get search results from NuGet search API.");
                        }
                    }
            }while (!done && !_runOnlyOnePage);
        }