| @@ -0,0 +1,18 @@ | |||
| using ProtoBuf; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace GrpcShared.DTO.Track.MultipleTrack | |||
| { | |||
| [ProtoContract] | |||
| public class MultipleTrackRequest | |||
| { | |||
| [ProtoMember(1)] | |||
| public List<string> Ids { get; set; } | |||
| [ProtoMember(2)] | |||
| public string Token { get; set; } | |||
| } | |||
| } | |||
| @@ -0,0 +1,74 @@ | |||
| using ProtoBuf; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace GrpcShared.DTO.Track.MultipleTrack | |||
| { | |||
| [ProtoContract] | |||
| public class MultipleTrackResponse | |||
| { | |||
| [ProtoMember(1)] | |||
| public AudioFeature AudioFeatures { get; set; } | |||
| } | |||
| [ProtoContract] | |||
| public class AudioFeature | |||
| { | |||
| [ProtoMember(1)] | |||
| public double Danceability { get; set; } | |||
| [ProtoMember(2)] | |||
| public double Energy { get; set; } | |||
| [ProtoMember(3)] | |||
| public long Key { get; set; } | |||
| [ProtoMember(4)] | |||
| public double Loudness { get; set; } | |||
| [ProtoMember(5)] | |||
| public long Mode { get; set; } | |||
| [ProtoMember(6)] | |||
| public double Speechiness { get; set; } | |||
| [ProtoMember(7)] | |||
| public double Acousticness { get; set; } | |||
| [ProtoMember(8)] | |||
| public long Instrumentalness { get; set; } | |||
| [ProtoMember(9)] | |||
| public double Liveness { get; set; } | |||
| [ProtoMember(10)] | |||
| public double Valence { get; set; } | |||
| [ProtoMember(11)] | |||
| public double Tempo { get; set; } | |||
| [ProtoMember(12)] | |||
| public string Type { get; set; } | |||
| [ProtoMember(13)] | |||
| public string Id { get; set; } | |||
| [ProtoMember(14)] | |||
| public string Uri { get; set; } | |||
| [ProtoMember(15)] | |||
| public Uri TrackHref { get; set; } | |||
| [ProtoMember(16)] | |||
| public Uri AnalysisUrl { get; set; } | |||
| [ProtoMember(17)] | |||
| public long DurationMs { get; set; } | |||
| [ProtoMember(18)] | |||
| public long TimeSignature { get; set; } | |||
| } | |||
| } | |||
| @@ -1,4 +1,5 @@ | |||
| using GrpcShared.DTO.Search; | |||
| using GrpcShared.DTO.Track.MultipleTrack; | |||
| using GrpcShared.DTO.Track.SingleTrack; | |||
| using ProtoBuf.Grpc.Configuration; | |||
| using System; | |||
| @@ -14,5 +15,6 @@ namespace GrpcShared.Interfaces | |||
| { | |||
| Task<SearchResponse> ListSearchAsync(SearchRequest request); | |||
| Task<SingleTrackResponse> ListSingleTrackAsync(SingleTrackRequest request); | |||
| Task<MultipleTrackResponse> ListMultipleTrackAsync(MultipleTrackRequest request); | |||
| } | |||
| } | |||
| @@ -1,5 +1,7 @@ | |||
| @page "/search" | |||
| @using GrpcShared.DTO.Search | |||
| @using GrpcShared.DTO.Track.MultipleTrack | |||
| @using GrpcShared.DTO.Track.SingleTrack | |||
| @using NemAnBlazor.Services.Interfaces | |||
| @*@inject HttpClient Http*@ | |||
| @inject Blazored.SessionStorage.ISessionStorageService sessionStorage | |||
| @@ -16,8 +18,14 @@ | |||
| { | |||
| var token = await sessionStorage.GetItemAsync<string>("token"); | |||
| SearchRequest request = new() { Query = "aitch", Type = "track", Token = token }; | |||
| SearchResponse searchResponse = await SearchService.GetListSearchAsync(request); | |||
| //SearchRequest request = new() { Query = "aitch", Type = "track", Token = token }; | |||
| //SearchResponse searchResponse = await SearchService.GetListSearchAsync(request); | |||
| //MultipleTrackRequest mreq = new() { Ids = new List<string>(){"3JAeYOjyJodI4PRs44lx2l", "6clZa1yrZe7pJrYFUcD9KW"}, Token = token }; | |||
| //MultipleTrackResponse multipleTrackResponse = await SearchService.GetListMultipleTrackAsync(mreq); | |||
| SingleTrackRequest singleTrackRequest = new() { Id = "3JAeYOjyJodI4PRs44lx2l", Token = token }; | |||
| SingleTrackResponse singleTrackResponse = await SearchService.GetListSingleTrackAsync(singleTrackRequest); | |||
| } | |||
| @@ -1,4 +1,5 @@ | |||
| using GrpcShared.DTO.Search; | |||
| using GrpcShared.DTO.Track.MultipleTrack; | |||
| using GrpcShared.DTO.Track.SingleTrack; | |||
| namespace NemAnBlazor.Services.Interfaces | |||
| @@ -7,5 +8,6 @@ namespace NemAnBlazor.Services.Interfaces | |||
| { | |||
| Task<SearchResponse> GetListSearchAsync(SearchRequest request); | |||
| Task<SingleTrackResponse> GetListSingleTrackAsync(SingleTrackRequest request); | |||
| Task<MultipleTrackResponse> GetListMultipleTrackAsync(MultipleTrackRequest request); | |||
| } | |||
| } | |||
| @@ -1,5 +1,6 @@ | |||
| using Grpc.Net.Client; | |||
| using GrpcShared.DTO.Search; | |||
| using GrpcShared.DTO.Track.MultipleTrack; | |||
| using GrpcShared.DTO.Track.SingleTrack; | |||
| using GrpcShared.Interfaces; | |||
| using NemAnBlazor.Services.Interfaces; | |||
| @@ -16,6 +17,7 @@ namespace NemAnBlazor.Services | |||
| _serviceClient = grpcChannel.CreateGrpcService<ITrackService>(); | |||
| } | |||
| public async Task<SearchResponse> GetListSearchAsync(SearchRequest request) | |||
| { | |||
| return await _serviceClient.ListSearchAsync(request); | |||
| @@ -25,5 +27,9 @@ namespace NemAnBlazor.Services | |||
| { | |||
| return await _serviceClient.ListSingleTrackAsync(request); | |||
| } | |||
| public async Task<MultipleTrackResponse> GetListMultipleTrackAsync(MultipleTrackRequest request) | |||
| { | |||
| return await _serviceClient.ListMultipleTrackAsync(request); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,12 +1,14 @@ | |||
| using Grpc.Core; | |||
| using GrpcShared; | |||
| using GrpcShared.DTO.Search; | |||
| using GrpcShared.DTO.Track.MultipleTrack; | |||
| using GrpcShared.DTO.Track.SingleTrack; | |||
| using GrpcShared.Interfaces; | |||
| using Microsoft.Net.Http.Headers; | |||
| using Newtonsoft.Json; | |||
| using System.Text; | |||
| using System.Text.Json; | |||
| using System.Web; | |||
| namespace SpotifyService.Services | |||
| { | |||
| @@ -20,6 +22,7 @@ namespace SpotifyService.Services | |||
| _httpClientFactory = httpClientFactory; | |||
| } | |||
| public async Task<SearchResponse> ListSearchAsync(SearchRequest request) | |||
| { | |||
| var client = _httpClientFactory.CreateClient("HttpClient"); | |||
| @@ -69,6 +72,44 @@ namespace SpotifyService.Services | |||
| TimeSignature = responses!.TimeSignature, | |||
| }; | |||
| } | |||
| public async Task<MultipleTrackResponse> ListMultipleTrackAsync(MultipleTrackRequest request) | |||
| { | |||
| var client = _httpClientFactory.CreateClient("HttpClient"); | |||
| client.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Bearer " + request.Token); | |||
| bool startingQuestionMarkAdded = false; | |||
| var sb = new StringBuilder(); | |||
| var param = new Dictionary<string, List<string>>(); | |||
| param["ids"] = request.Ids; | |||
| foreach(var id in param) | |||
| { | |||
| sb.Append(startingQuestionMarkAdded ? '&' : '?'); | |||
| sb.Append(id.Key); | |||
| sb.Append('='); | |||
| foreach(var Xid in id.Value) | |||
| { | |||
| sb.Append(Xid); | |||
| sb.Append("%2C"); | |||
| startingQuestionMarkAdded = true; | |||
| } | |||
| } | |||
| sb.Length = sb.Length - 3; | |||
| sb.ToString(); | |||
| var trackMultipleResult = await client.GetAsync($"audio-features{sb}"); | |||
| var responses = JsonConvert.DeserializeObject<MultipleTrackResponse>(await trackMultipleResult.Content.ReadAsStringAsync())!; | |||
| return new MultipleTrackResponse | |||
| { | |||
| AudioFeatures = responses!.AudioFeatures | |||
| }; | |||
| } | |||
| } | |||
| } | |||