Skip to content

Commit

Permalink
[Client SDK] Make the autocomplete resource optional (#481)
Browse files Browse the repository at this point in the history
Addresses #477
  • Loading branch information
loic-sharma authored Feb 16, 2020
1 parent e722836 commit 6e1f2a9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/BaGet.Protocol/Extensions/ServiceIndexModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using BaGet.Protocol.Models;

Expand Down Expand Up @@ -33,22 +34,22 @@ public static class ServiceIndexModelExtensions

public static string GetPackageContentResourceUrl(this ServiceIndexResponse serviceIndex)
{
return serviceIndex.GetResourceUrl(PackageBaseAddress);
return serviceIndex.GetRequiredResourceUrl(PackageBaseAddress, nameof(PackageBaseAddress));
}

public static string GetPackageMetadataResourceUrl(this ServiceIndexResponse serviceIndex)
{
return serviceIndex.GetResourceUrl(RegistrationsBaseUrl);
return serviceIndex.GetRequiredResourceUrl(RegistrationsBaseUrl, nameof(RegistrationsBaseUrl));
}

public static string GetCatalogResourceUrl(this ServiceIndexResponse serviceIndex)
public static string GetSearchQueryResourceUrl(this ServiceIndexResponse serviceIndex)
{
return serviceIndex.GetResourceUrl(Catalog);
return serviceIndex.GetRequiredResourceUrl(SearchQueryService, nameof(SearchQueryService));
}

public static string GetSearchQueryResourceUrl(this ServiceIndexResponse serviceIndex)
public static string GetCatalogResourceUrl(this ServiceIndexResponse serviceIndex)
{
return serviceIndex.GetResourceUrl(SearchQueryService);
return serviceIndex.GetResourceUrl(Catalog);
}

public static string GetSearchAutocompleteResourceUrl(this ServiceIndexResponse serviceIndex)
Expand All @@ -62,5 +63,19 @@ public static string GetResourceUrl(this ServiceIndexResponse serviceIndex, stri

return resource?.ResourceUrl.Trim('/');
}

public static string GetRequiredResourceUrl(this ServiceIndexResponse serviceIndex, string[] types, string resourceName)
{
// For more information on required resources,
// see: https://docs.microsoft.com/en-us/nuget/api/overview#resources-and-schema
var resourceUrl = serviceIndex.GetResourceUrl(types);
if (string.IsNullOrEmpty(resourceUrl))
{
throw new InvalidOperationException(
$"The service index does not have a resource named '{resourceName}'");
}

return resourceUrl;
}
}
}
7 changes: 6 additions & 1 deletion src/BaGet.Protocol/NuGetClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,18 @@ private async Task<T> GetAsync<T>(Func<NuGetClients, T> clientFactory, Cancellat
var searchResourceUrl = serviceIndex.GetSearchQueryResourceUrl();
var autocompleteResourceUrl = serviceIndex.GetSearchAutocompleteResourceUrl();

// Create clients for requires resources.

This comment has been minimized.

Copy link
@bgrainger

bgrainger Feb 16, 2020

Contributor

Minor typo: required

This comment has been minimized.

Copy link
@loic-sharma

loic-sharma Feb 16, 2020

Author Owner

Nice catch! Fixed in 48a438d

var contentClient = new RawPackageContentClient(_httpClient, contentResourceUrl);
var metadataClient = new RawPackageMetadataClient(_httpClient, metadataResourceUrl);
var searchClient = new RawSearchClient(_httpClient, searchResourceUrl);
var autocompleteClient = new RawAutocompleteClient(_httpClient, autocompleteResourceUrl);

// 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);

_clients = new NuGetClients
{
Expand Down
28 changes: 28 additions & 0 deletions src/BaGet.Protocol/Search/NullAutocompleteClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using BaGet.Protocol.Models;

namespace BaGet.Protocol.Internal
{
public class NullAutocompleteClient : IAutocompleteClient
{
public Task<AutocompleteResponse> AutocompleteAsync(string query = null, int skip = 0, int take = 20, bool includePrerelease = true, bool includeSemVer2 = true, CancellationToken cancellationToken = default)
{
return Task.FromResult(new AutocompleteResponse
{
TotalHits = 0,
Data = new List<string>()
});
}

public Task<AutocompleteResponse> ListPackageVersionsAsync(string packageId, bool includePrerelease = true, bool includeSemVer2 = true, CancellationToken cancellationToken = default)
{
return Task.FromResult(new AutocompleteResponse
{
TotalHits = 0,
Data = new List<string>()
});
}
}
}

0 comments on commit 6e1f2a9

Please sign in to comment.