/// <summary> /// Initializes a new instance of the <see cref="NuGetClientFactory"/> class. /// </summary> /// <param name="httpClient">The client used for HTTP requests.</param> /// <param name="serviceIndexUrl"> /// The NuGet Service Index resource URL. /// /// For NuGet.org, use https://api.nuget.org/v3/index.json /// </param> public NuGetClientFactory(HttpClient httpClient, string serviceIndexUrl) { _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); _serviceIndexUrl = serviceIndexUrl ?? throw new ArgumentNullException(nameof(serviceIndexUrl)); _mutex = new SemaphoreSlim(1, 1); _clients = null; }
private async Task <T> GetAsync <T>(Func <NuGetClients, T> clientFactory, CancellationToken cancellationToken) { if (_clients == null) { await _mutex.WaitAsync(cancellationToken); try { if (_clients == null) { var serviceIndexClient = new RawServiceIndexClient(_httpClient, _serviceIndexUrl); var serviceIndex = await serviceIndexClient.GetAsync(cancellationToken); var contentResourceUrl = serviceIndex.GetPackageContentResourceUrl(); var metadataResourceUrl = serviceIndex.GetPackageMetadataResourceUrl(); var catalogResourceUrl = serviceIndex.GetCatalogResourceUrl(); var searchResourceUrl = serviceIndex.GetSearchQueryResourceUrl(); var autocompleteResourceUrl = serviceIndex.GetSearchAutocompleteResourceUrl(); // Create clients for required resources. var contentClient = new RawPackageContentClient(_httpClient, contentResourceUrl); var metadataClient = new RawPackageMetadataClient(_httpClient, metadataResourceUrl); var searchClient = new RawSearchClient(_httpClient, searchResourceUrl); // Create clients for optional resources. var catalogClient = catalogResourceUrl == null ? new NullCatalogClient() as ICatalogClient : new RawCatalogClient(_httpClient, catalogResourceUrl); var autocompleteClient = autocompleteResourceUrl == null ? new NullAutocompleteClient() as IAutocompleteClient : new RawAutocompleteClient(_httpClient, autocompleteResourceUrl); var publishClient = new RawPublishClient(_httpClient, serviceIndex); _clients = new NuGetClients { ServiceIndex = serviceIndex, PackageContentClient = contentClient, PackageMetadataClient = metadataClient, SearchClient = searchClient, AutocompleteClient = autocompleteClient, CatalogClient = catalogClient, PublishClient = publishClient, }; } } finally { _mutex.Release(); } } // TODO: This should periodically refresh the service index response. return(clientFactory(_clients)); }
private async Task <T> GetClientAsync <T>(Func <NuGetClients, T> clientFactory, CancellationToken cancellationToken) { // TODO: This should periodically refresh the service index response. if (_clients == null) { await _mutex.WaitAsync(cancellationToken); try { if (_clients == null) { var serviceIndexClient = new ServiceIndexClient(_httpClient, _serviceIndexUrl); var serviceIndex = await serviceIndexClient.GetAsync(cancellationToken); var contentClient = new PackageContentClient(_httpClient, GetResourceUrl(serviceIndex, PackageBaseAddress)); var metadataClient = new PackageMetadataClient(_httpClient, GetResourceUrl(serviceIndex, RegistrationsBaseUrl)); var catalogClient = new CatalogClient(_httpClient, GetResourceUrl(serviceIndex, Catalog)); var searchClient = new SearchClient( _httpClient, GetResourceUrl(serviceIndex, SearchQueryService), GetResourceUrl(serviceIndex, SearchAutocompleteService)); _clients = new NuGetClients { ServiceIndexClient = serviceIndexClient, PackageContentClient = contentClient, PackageMetadataClient = metadataClient, SearchClient = searchClient, CatalogClient = catalogClient, }; } } finally { _mutex.Release(); } } return(clientFactory(_clients)); }