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.6KB

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