Blazor & WASM in combination to get statistics from Spotify API for performing the song analysis. With separate microservices for auth, Spotify, user data tracking, and application, connected through gRPC with Polly.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

StatsService.cs 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Grpc.Net.Client;
  2. using GrpcShared.DTO;
  3. using GrpcShared.DTO.TopItem;
  4. using GrpcShared.DTO.Track;
  5. using GrpcShared.Interfaces;
  6. using Microsoft.Net.Http.Headers;
  7. using Newtonsoft.Json;
  8. using ProtoBuf.Grpc.Client;
  9. using SpotifyService.HttpUtils;
  10. namespace SpotifyService.Services
  11. {
  12. public class StatsService : IStatsService
  13. {
  14. private readonly IHttpClientFactory _httpClientFactory;
  15. private IIdentityService _identityService;
  16. private IAuthService _authService;
  17. public StatsService(IHttpClientFactory httpClientFactory, IAuthService authService)
  18. {
  19. _httpClientFactory = httpClientFactory;
  20. _identityService = GrpcChannel.ForAddress("http://127.0.0.1:5002/").CreateGrpcService<IIdentityService>();
  21. _authService = authService;
  22. }
  23. public async Task<CurrentTrackResponse> GetCurrentlyPlayingTrack(SessionMessage message)
  24. {
  25. string url = "me/player/currently-playing";
  26. var response = await HttpUtils<CurrentTrackResponse>
  27. .GetData(_httpClientFactory,
  28. url,
  29. message.UserId!,
  30. _identityService,
  31. _authService);
  32. string savedUrl = $"me/tracks/contains?ids={response.Item!.Id}";
  33. var savedResponse = await HttpUtils<List<bool>>
  34. .GetData(_httpClientFactory,
  35. savedUrl,
  36. message.UserId!,
  37. _identityService,
  38. _authService);
  39. if (response != null)
  40. {
  41. response.IsSaved = savedResponse[0];
  42. }
  43. return response;
  44. }
  45. public async Task<TopItemResponse> GetTopItems(TopItemRequest request)
  46. {
  47. //https://api.spotify.com/v1/me/top/albums?limit=10&offset=5
  48. //URL PARAMS
  49. string url = "me/top/";
  50. url += !request.IsTracks ? "artists" : "tracks";
  51. url += request.Limit == null ? "" : $"?limit={request.Limit}";
  52. if (request.Limit == null && request.Offset != null) url += $"?offset={request.Offset}";
  53. else url += request.Offset == null ? "" : $"&offset={request.Offset}";
  54. return await HttpUtils<TopItemResponse>
  55. .GetData(_httpClientFactory,
  56. url,
  57. request.UserId!,
  58. _identityService,
  59. _authService);
  60. }
  61. }
  62. }