Beispiel #1
0
        public override Task <JObject> GetPackageMetadata(string id, Versioning.NuGetVersion version)
        {
            return(Task.Factory.StartNew(() =>
            {
                NuGetTraceSources.V2SourceRepository.Verbose("getpackage", "Getting metadata for {0} {1}", id, version);
                var semver = CoreConverters.SafeToSemanticVersion(version);
                var package = _repository.FindPackage(id, semver);

                // Sometimes, V2 APIs seem to fail to return a value for Packages(Id=,Version=) requests...
                if (package == null)
                {
                    var packages = _repository.FindPackagesById(id);
                    package = packages.FirstOrDefault(p => Equals(p.Version, semver));
                }

                // If still null, fail
                if (package == null)
                {
                    return null;
                }

                string repoRoot = null;
                IPackagePathResolver resolver = null;
                if (_lprepo != null)
                {
                    repoRoot = _lprepo.Source;
                    resolver = _lprepo.PathResolver;
                }

                return PackageJsonLd.CreatePackage(package, repoRoot, resolver);
            }));
        }
        private static string MakeResults(IndexSearcher searcher, TopDocs topDocs, int skip, int take, bool includeExplanation, Query query, long elapsed, IDictionary<string, int> rankings, PackageSearcherManager manager)
        {
            //  note the use of a StringBuilder because we have the response data already formatted as JSON in the fields in the index

            StringBuilder strBldr = new StringBuilder();

            string timestamp;
            if (!searcher.IndexReader.CommitUserData.TryGetValue("commit-time-stamp", out timestamp))
            {
                timestamp = null;
            }

            strBldr.AppendFormat("{{\"totalHits\":{0},\"timeTakenInMs\":{1},\"index\":\"{2}\"", topDocs.TotalHits, elapsed, manager.IndexName);
            if (!String.IsNullOrEmpty(timestamp))
            {
                strBldr.AppendFormat(",\"indexTimestamp\":\"{0}\"", timestamp);
            }
            if (includeExplanation)
            {
                // JsonConvert.Serialize does escaping and quoting.
                strBldr.AppendFormat(",\"executedQuery\":{0}", Newtonsoft.Json.JsonConvert.SerializeObject(query.ToString()));
            }
            strBldr.Append(",\"data\":[");

            bool hasResult = false;

            for (int i = skip; i < topDocs.ScoreDocs.Length; i++)
            {
                ScoreDoc scoreDoc = topDocs.ScoreDocs[i];

                Document doc = searcher.Doc(scoreDoc.Doc);
                string data = doc.Get("Data");

                string id = doc.Get("Id");               
                NuGet.Versioning.NuGetVersion ngVersion = new Versioning.NuGetVersion(doc.Get("Version"));               
                     
                if (!String.IsNullOrEmpty(id) && ngVersion != null)
                {
                    Tuple<int,int> countRecord = manager.GetDownloadCount(id,ngVersion.ToNormalizedString());
                    if (countRecord != null)
                    {
                        // Patch the data in to the JSON
                        JObject parsed = JObject.Parse(data);
                        parsed["DownloadCount"] = countRecord.Item1;
                        parsed["PackageRegistration"]["DownloadCount"] = countRecord.Item2;                      
                        data = parsed.ToString(Formatting.None);
                    }
                }

                if (includeExplanation)
                {
                    data = AddExplanation(searcher, data, query, scoreDoc, rankings);
                }

                strBldr.Append(data);
                strBldr.Append(",");

                hasResult = true;
            }

            if (hasResult)
            {
                strBldr.Remove(strBldr.Length - 1, 1);
            }

            strBldr.Append("]}");

            string result = strBldr.ToString();

            return result;
        }
        public override async Task <Newtonsoft.Json.Linq.JObject> GetPackageMetadata(string id, Versioning.NuGetVersion version)
        {
            foreach (var source in _sources)
            {
                var result = await source.GetPackageMetadata(id, version);

                if (result != null)
                {
                    return(result);
                }
            }

            return(null);
        }
        public override async Task <Newtonsoft.Json.Linq.JObject> GetPackageMetadata(string id, Versioning.NuGetVersion version)
        {
            await DetectVersionWhenNeccessary();

            return(await _repo.GetPackageMetadata(id, version));
        }