Exemplo n.º 1
0
        public static async Task <Error> NpmDeletePackageAsync(this NpmClient client, string packageId, string version)
        {
            var uri      = new Uri(client.Uri);
            var hostUri  = uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
            var feedName = uri.Segments.Last().Replace("/", "");

            var sourceClient = new ProGetClient(hostUri, client.ApiKey);
            var sourceFeeds  = await sourceClient.Feeds_GetFeedsAsync(false);

            var sourceFeed = sourceFeeds.SingleOrDefault(f => f.Feed_Name.Equals(feedName, StringComparison.CurrentCultureIgnoreCase));

            if (sourceFeed == null)
            {
                return(new Error(HttpStatusCode.NotFound, $"NPM Feed not found {client.FeedName} on {client.Uri}"));
            }
            var(name, scope) = ParseNpmId(packageId);
            var result = await sourceClient.NpmPackages_DeletePackageAsync(sourceFeed.Feed_Id, name, scope, version);

            if (result)
            {
                return(null);
            }

            return(new Error(HttpStatusCode.ServiceUnavailable, "Error"));
        }
Exemplo n.º 2
0
        public static async Task <(IEnumerable <Package> Packages, Error Error)> NpmFetchPackagesAsync(this NpmClient client, string searchTerm, bool includePreRelease, bool downloadPackageContent)
        {
            var uri      = new Uri(client.Uri);
            var hostUri  = uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
            var feedName = uri.Segments.Last().Replace("/", "");

            var  sourceClient = new ProGetClient(hostUri, client.ApiKey);
            Feed sourceFeed;

            try
            {
                sourceFeed = (await sourceClient.Feeds_GetFeedsAsync(false))
                             .SingleOrDefault(f => f.Feed_Name.Equals(feedName, StringComparison.CurrentCultureIgnoreCase));
            }
            catch (FlurlHttpException ex)
            {
                var statusCode = ex.Message.Contains("403")
                    ? HttpStatusCode.Forbidden
                    : HttpStatusCode.NotFound;

                return(null, new Error(statusCode, ex.Message));
            }

            if (sourceFeed == null)
            {
                return(null, new Error(HttpStatusCode.NotFound, $"Feed not found ({client.FeedName}) on {hostUri}"));
            }

            var packageVersions = await sourceClient.NpmFeeds_GetAllPackageVersionsAsync(sourceFeed.Feed_Id);

            var npmPackageVersionsArray = packageVersions as NpmPackageAllVersions[] ?? packageVersions.ToArray();

            var tasks = npmPackageVersionsArray
                        .Select(p => ConvertNpmPackageVersionToPackageAsync(client, p))
                        .ToArray();

            await Task.WhenAll(tasks);

            searchTerm ??= string.Empty;

            var packages = tasks
                           .Select(x => x.Result)
                           .Where(p => p.Id.Contains(searchTerm, StringComparison.InvariantCultureIgnoreCase) && (includePreRelease || !p.IsPrerelease))
                           .ToList();

            if (downloadPackageContent)
            {
                foreach (var package in packages)
                {
                    var(content, error) = await client.NpmDownloadPackageAsync(package);

                    if (error != null)
                    {
                        throw new WebException(error.ErrorMessage);
                    }

                    package.Content = content;
                }
            }

            return(packages, null);
        }