|
|
|
@@ -1,7 +1,9 @@ |
|
|
|
@page "/home" |
|
|
|
@using Grpc.Core |
|
|
|
@using GrpcShared.DTO |
|
|
|
@using GrpcShared.DTO.Track |
|
|
|
@using NemAnBlazor.Services.Interfaces |
|
|
|
@using System.Net |
|
|
|
@inject Blazored.LocalStorage.ILocalStorageService localStorage |
|
|
|
@inject IStatsClientService spotifyService |
|
|
|
@inject ITrackClientService trackService |
|
|
|
@@ -14,32 +16,57 @@ |
|
|
|
@code { |
|
|
|
protected override async Task OnInitializedAsync() |
|
|
|
{ |
|
|
|
|
|
|
|
string tokenS = await localStorage.GetItemAsync<string>("token"); |
|
|
|
string refreshT = await localStorage.GetItemAsync<string>("refresh_token"); |
|
|
|
|
|
|
|
//tokenS = "BQBMgFm6jnFNWWeZEMGIRP_f-ENPid7Kw8JubAyuWAe4JK0S1DPFGlaAdZ_Fey6ePkCnz8-cqC0oyRmrciWUy5ISUTQKDe8PTQn4iBRMYCgM0n4GnS1xAErHJcm4Vpu2TAngk-4vQUOfTQRcedNTfCaHKP4uFJgTlTI7JHGrtB-_EZLnFcZ2OQe31oFQIJ1wM3ZtvwnN"; |
|
|
|
TokenMessage token = new TokenMessage { Token = tokenS, RefreshToken = refreshT }; |
|
|
|
|
|
|
|
CurrentTrackResponse response = await spotifyService.GetCurrentlyPlayingTrack(token); |
|
|
|
try |
|
|
|
{ |
|
|
|
CurrentTrackResponse track = await spotifyService.GetCurrentlyPlayingTrack(token); |
|
|
|
|
|
|
|
//if token expired, refresh it |
|
|
|
if (response.ResponseMsg == System.Net.HttpStatusCode.Unauthorized) |
|
|
|
//if token expired, refresh it |
|
|
|
if (track.ResponseMsg == System.Net.HttpStatusCode.Unauthorized) |
|
|
|
{ |
|
|
|
string? tempToken = await SpotifyHelper.TryRefreshToken(AuthService, token, localStorage); |
|
|
|
tokenS = tempToken == null ? tokenS : tempToken; |
|
|
|
//if refreshed token is null, that means that refresh token was invalid, so you should redirect to login |
|
|
|
} |
|
|
|
} |
|
|
|
catch (RpcException e) |
|
|
|
{ |
|
|
|
string? tempToken = await SpotifyHelper.TryRefreshToken(AuthService, token, localStorage); |
|
|
|
tokenS = tempToken == null ? tokenS : tempToken; |
|
|
|
//if refreshed token is null, that means that refresh token was invalid, so you should redirect to login |
|
|
|
if (e.StatusCode == StatusCode.Cancelled) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
throw; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//napravi komponentu koja ce da prikazuje sta trenutno slusas i passuj joj parametre |
|
|
|
|
|
|
|
|
|
|
|
//var trackById = await trackService.GetById(new GrpcShared.DTO.TrackByID.TrackRequest { TrackID = "4fy1A2WBTPX55mUI16TQXa", Token = tokenS }); |
|
|
|
try |
|
|
|
{ |
|
|
|
var items = await spotifyService.GetTopItems(new GrpcShared.DTO.TopItem.TopItemRequest { Token = tokenS, IsTracks = false, Offset = 5 }); |
|
|
|
|
|
|
|
var items = await spotifyService.GetTopItems(new GrpcShared.DTO.TopItem.TopItemRequest { Token = tokenS, IsTracks = false, Offset = 5 }); |
|
|
|
|
|
|
|
if (items.ResponseMsg == System.Net.HttpStatusCode.Unauthorized) |
|
|
|
if (items.ResponseMsg == System.Net.HttpStatusCode.Unauthorized) |
|
|
|
{ |
|
|
|
string? tempToken = await SpotifyHelper.TryRefreshToken(AuthService, token, localStorage); |
|
|
|
tokenS = tempToken == null ? tokenS : tempToken; |
|
|
|
} |
|
|
|
} |
|
|
|
catch (RpcException e) |
|
|
|
{ |
|
|
|
string? tempToken = await SpotifyHelper.TryRefreshToken(AuthService, token, localStorage); |
|
|
|
tokenS = tempToken == null ? tokenS : tempToken; |
|
|
|
if (e.StatusCode == StatusCode.Cancelled) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
throw; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |