示例#1
0
        public CacheRegistrationIndexModule(IMirrorService mirror)
        {
            this._mirror = mirror ?? throw new ArgumentNullException(nameof(mirror));
            this.Get("cache/v3/registration/{id}/index.json", async(req, res, routeData) =>
            {
                string id = routeData.As <string>("id");
                // Documentation: https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource
                var upstreamPackages = (await _mirror.FindUpstreamMetadataAsync(id, CancellationToken.None)).ToList();
                var versions         = upstreamPackages.Select(p => p.Identity.Version).ToList();

                if (!upstreamPackages.Any())
                {
                    res.StatusCode = 404;
                    return;
                }

                // TODO: Paging of registration items.
                // "Un-paged" example: https://api.nuget.org/v3/registration3/newtonsoft.json/index.json
                // Paged example: https://api.nuget.org/v3/registration3/fake/index.json
                await res.AsJson(new
                {
                    Count          = upstreamPackages.Count,
                    TotalDownloads = upstreamPackages.Sum(p => p.DownloadCount),
                    Items          = new[]
                    {
                        new RegistrationIndexItem(
                            packageId: id,
                            items: upstreamPackages.Select(p => ToRegistrationIndexLeaf(req, p)).ToList(),
                            lower: versions.Min().ToNormalizedString(),
                            upper: versions.Max().ToNormalizedString()
                            ),
                    }
                });
            });

            this.Get("cache/v3/registration/{id}/{version}.json", async(req, res, routeData) =>
            {
                string id      = routeData.As <string>("id");
                string version = routeData.As <string>("version");

                if (!NuGetVersion.TryParse(version, out var nugetVersion))
                {
                    res.StatusCode = 400;
                    return;
                }

                // Allow read-through caching to happen if it is confiured.
                await _mirror.MirrorAsync(id, nugetVersion, CancellationToken.None);

                var package = await _mirror.FindAsync(new PackageIdentity(id, nugetVersion));

                if (package == null)
                {
                    res.StatusCode = 404;
                    return;
                }

                // Documentation: https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource
                var result = new RegistrationLeaf(
                    registrationUri: req.PackageRegistration(id, nugetVersion, "cache"),
                    listed: package.IsListed,
                    downloads: package.DownloadCount.GetValueOrDefault(),
                    packageContentUri: req.PackageDownload(id, nugetVersion, "cache"),
                    published: package.Published.GetValueOrDefault(),
                    registrationIndexUri: req.PackageRegistration(id, "cache"));

                await res.AsJson(result);
            });
        }