Skip to content

Commit

Permalink
GetProgressionsIndex operation implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-zhur committed May 23, 2024
1 parent 74373c0 commit 4d28da5
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using HarmonyDB.Index.Api.Services;
using HarmonyDB.Index.DownstreamApi.Client;
using HarmonyDB.Source.Api.Model;
using HarmonyDB.Source.Api.Model.VInternal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using OneShelf.Common.Api;

namespace HarmonyDB.Index.Api.Functions.V1;

public class GetProgressionsIndex : FunctionBase<GetProgressionsIndexRequest, GetProgressionsIndexResponse>
{
private readonly DownstreamApiClient _downstreamApiClient;

public GetProgressionsIndex(ILoggerFactory loggerFactory, DownstreamApiClient downstreamApiClient)
: base(loggerFactory)
{
_downstreamApiClient = downstreamApiClient;
}

[Function(SourceApiUrls.VInternalGetProgressionsIndex)]
public Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req, [FromBody] GetProgressionsIndexRequest request)
=> RunHandler(request);

protected override async Task<GetProgressionsIndexResponse> Execute(GetProgressionsIndexRequest request)
{
var results = (await Task.WhenAll(_downstreamApiClient.GetDownstreamSourceIndices(x => x.AreProgressionsProvidedForIndexing)
.Select(_downstreamApiClient.VInternalGetProgressionsIndex)))
.SelectMany(x => x.Progressions)
.ToDictionary(x => x.Key, x => x.Value);

return new()
{
Progressions = results,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using OneShelf.Authorization.Api.Client;
using OneShelf.Common;
using OneShelf.Common.Api.WithAuthorization;

namespace HarmonyDB.Index.Api.Functions.V1;
Expand Down
2 changes: 1 addition & 1 deletion HarmonyDB.Index/HarmonyDB.Index.Api/Functions/V1/Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override async Task<SearchResponse> Execute(HttpRequest httpRequest, S
throw new ArgumentOutOfRangeException(nameof(query));
}

var all = await Task.WhenAll(_downstreamApiClient.SearchableDownstreamSourceIndices.Select(i => _downstreamApiClient.V1Search(i,
var all = await Task.WhenAll(_downstreamApiClient.GetDownstreamSourceIndices(x => x.IsSearchSupported).Select(i => _downstreamApiClient.V1Search(i,
new()
{
Identity = request.Identity,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HarmonyDB.Source.Api.Client;
using HarmonyDB.Source.Api.Model.V1;
using HarmonyDB.Source.Api.Model.V1.Api;
using HarmonyDB.Source.Api.Model.VInternal;
using Microsoft.Extensions.Options;
using OneShelf.Authorization.Api.Model;
using OneShelf.Common;
Expand All @@ -26,14 +27,17 @@ public DownstreamApiClient(IHttpClientFactory httpClientFactory, IOptions<Downst

public IReadOnlyDictionary<string, int> DownstreamSourceIndicesBySourceKey { get; }

public IEnumerable<int> SearchableDownstreamSourceIndices => _options.DownstreamSources.WithIndices().Where(x => x.x.IsSearchSupported).Select(x => x.i);

public IEnumerable<int> GetDownstreamSourceIndices(Func<DownstreamApiClientOptions.DownstreamSourceOptions, bool> selector) => _options.DownstreamSources.WithIndices().Where(x => selector(x.x)).Select(x => x.i);
public int DownstreamSourcesCount => _options.DownstreamSources.Count;

public string GetSourceTitle(string sourceKey) => _options.DownstreamSources.SelectMany(x => x.Sources).Single(s => s.Key == sourceKey).Title;

public string GetSourceKey(string externalId) => _options.DownstreamSources.SelectMany(x => x.Sources).Single(s => externalId.StartsWith(s.ExternalIdPrefix)).Key;

public async Task<GetProgressionsIndexResponse> VInternalGetProgressionsIndex(int sourceIndex)
=> await _clients[sourceIndex].VInternalGetProgressionsIndex();

public async Task<GetSongResponse> V1GetSong(Identity identity, int sourceIndex, string externalId)
=> await _clients[sourceIndex].V1GetSong(identity, externalId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HarmonyDB.Source.Api.Model;
using HarmonyDB.Source.Api.Model.V1;
using HarmonyDB.Source.Api.Model.V1.Api;
using HarmonyDB.Source.Api.Model.VInternal;
using OneShelf.Authorization.Api.Model;
using OneShelf.Common.Api.Client;

Expand All @@ -22,6 +23,10 @@ public async Task<GetSongResponse> V1GetSong(Identity identity, string externalI
Identity = identity,
ExternalId = externalId,
});

public async Task<GetProgressionsIndexResponse> VInternalGetProgressionsIndex()
=> await PostWithCode<GetProgressionsIndexRequest, GetProgressionsIndexResponse>(SourceApiUrls.VInternalGetProgressionsIndex, new());

public async Task<GetSongsResponse> V1GetSongs(Identity identity, IReadOnlyList<string> externalIds)
=> await Post<GetSongsRequest, GetSongsResponse>(SourceApiUrls.V1GetSongs, new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\HarmonyDB.Index\HarmonyDB.Index.Analysis\HarmonyDB.Index.Analysis.csproj" />
<ProjectReference Include="..\..\OneShelf.Authorization\OneShelf.Authorization.Api.Model\OneShelf.Authorization.Api.Model.csproj" />
<ProjectReference Include="..\..\OneShelf.Common\OneShelf.Common\OneShelf.Common.csproj" />
<ProjectReference Include="..\..\HarmonyDB.Common\HarmonyDB.Common.Representations.OneShelf\HarmonyDB.Common.Representations.OneShelf.csproj" />
Expand Down
2 changes: 2 additions & 0 deletions HarmonyDB.Source/HarmonyDB.Source.Api.Model/SourceApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public static class SourceApiUrls
public const string V1GetSearchHeader = nameof(V1GetSearchHeader);
public const string V1GetSourcesAndExternalIds = nameof(V1GetSourcesAndExternalIds);
public const string V1Ping = nameof(V1Ping);

public const string VInternalGetProgressionsIndex = nameof(VInternalGetProgressionsIndex);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace HarmonyDB.Source.Api.Model.VInternal;

public class GetProgressionsIndexRequest
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using HarmonyDB.Index.Analysis.Models.CompactV1;

namespace HarmonyDB.Source.Api.Model.VInternal;

public class GetProgressionsIndexResponse
{
public required IReadOnlyDictionary<string, CompactChordsProgression> Progressions { get; init; }
}

0 comments on commit 4d28da5

Please sign in to comment.