/// <inheritdoc /> /// <exception cref="WebException">Thrown if the operation is unsuccessful.</exception> public override async Task <Package> FetchAsync(string packageId, string version) { if (string.IsNullOrWhiteSpace(packageId)) { throw new ArgumentException("A value of null, empty, or whitespace is not valid.", nameof(packageId)); } if (string.IsNullOrWhiteSpace(version)) { throw new ArgumentException("A value of null, empty, or whitespace is not valid.", nameof(version)); } var(foundNpmPackage, error) = await Client.NpmGetPackageAsync(packageId, version); if (error != null) { if (error.StatusCode == HttpStatusCode.NotFound) { return(null); } throw new WebException(error.ErrorMessage); } var foundPackage = foundNpmPackage.ToPackage(); var(content, downloadError) = await Client.NpmDownloadPackageAsync(foundPackage); if (downloadError != null) { throw new WebException(downloadError.ErrorMessage); } foundPackage.Content = content; return(foundPackage); }
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); }