Пример #1
0
        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));
        }
Пример #2
0
        public async Task GetsServiceIndex()
        {
            var result = await _target.GetAsync();

            Assert.Equal("3.0.0", result.Version);
            Assert.Equal(5, result.Resources.Count);

            Assert.Equal(TestData.CatalogIndexUrl, result.GetCatalogResourceUrl());
            Assert.Equal(TestData.PackageMetadataUrl, result.GetPackageMetadataResourceUrl());
            Assert.Equal(TestData.PackageContentUrl, result.GetPackageContentResourceUrl());
            Assert.Equal(TestData.SearchUrl, result.GetSearchQueryResourceUrl());
            Assert.Equal(TestData.AutocompleteUrl, result.GetSearchAutocompleteResourceUrl());
        }