| @@ -2,9 +2,9 @@ | |||
| { | |||
| public class SpotifyDbConfig | |||
| { | |||
| public string ConnectionString { get; set; } = null!; | |||
| public string DatabaseName { get; set; } = null!; | |||
| public string UserCollection { get; set; } = null!; | |||
| public string TracksCollection { get; set; } = null!; | |||
| public string ConnectionString { get; set; } = "mongodb://127.0.0.1:27017"; | |||
| public string DatabaseName { get; set; } = "spotifyDb"; | |||
| public string UserCollection { get; set; } = "Users"; | |||
| public string TracksCollection { get; set; } = "Tracks"; | |||
| } | |||
| } | |||
| @@ -36,20 +36,17 @@ builder.Services.AddBlazoredLocalStorage(); | |||
| //call spotify api | |||
| builder.Services.AddHttpClient(); | |||
| builder.Services.Configure<CodeRequest>( | |||
| builder.Configuration.GetSection("AuthParams")); | |||
| builder.Services.AddSingleton<SpotifyDbConfig>(); | |||
| builder.Services.Configure<SpotifyDbConfig>( | |||
| builder.Configuration.GetSection("SpotifyDb")); | |||
| builder.Services.AddHttpClient("HttpClient", c => | |||
| { | |||
| c.BaseAddress = new Uri(builder.Configuration.GetSection("SpotifyConfig:SpotifyURL").Value.ToString()); | |||
| c.DefaultRequestHeaders.Add("Accept", builder.Configuration.GetSection("SpotifyConfig:MediaType").Value.ToString()); | |||
| //builder.Services.AddHttpClient("HttpClient", c => | |||
| //{ | |||
| // c.BaseAddress = new Uri(builder.Configuration.GetSection("SpotifyConfig:SpotifyURL").Value.ToString()); | |||
| // c.DefaultRequestHeaders.Add("Accept", builder.Configuration.GetSection("SpotifyConfig:MediaType").Value.ToString()); | |||
| }); | |||
| //}); | |||
| var app = builder.Build(); | |||
| @@ -38,7 +38,8 @@ namespace IdentityProvider.Services | |||
| return new UserResponse | |||
| { | |||
| Id = user.Id, | |||
| Token = user.Token | |||
| Token = user.Token, | |||
| RefreshToken = user.RefreshToken | |||
| }; | |||
| else return new UserResponse(); | |||
| } | |||
| @@ -8,23 +8,17 @@ | |||
| "AllowedHosts": "*", | |||
| "Kestrel": { | |||
| "EndpointDefaults": { | |||
| "Protocols": "Http2" | |||
| "Protocols": "Http1AndHttp2" | |||
| } | |||
| }, | |||
| "AuthParams": { | |||
| "ClientId": "83e1d09876b049c4bb1953185a4b3bfb", | |||
| "RedirectURI": "https://localhost:44342/callback", | |||
| "Scope": "user-read-currently-playing user-read-email user-library-modify user-top-read user-read-private", | |||
| "ClientSecret": "ea752433d0774fad87fab5c1ee788c8d" | |||
| }, | |||
| "SpotifyDb": { | |||
| "ConnectionString": "mongodb://127.0.0.1:27017", | |||
| "DatabaseName": "spotifyDb", | |||
| "UserCollection": "Users", | |||
| "TracksCollection": "Tracks" | |||
| }, | |||
| "SpotifyConfig": { | |||
| "SpotifyURL": "https://api.spotify.com/v1/", | |||
| "MediaType": "application/json" | |||
| } | |||
| //"SpotifyConfig": { | |||
| // "SpotifyURL": "https://api.spotify.com/v1/", | |||
| // "MediaType": "application/json" | |||
| //} | |||
| } | |||
| @@ -37,7 +37,15 @@ namespace SpotifyService.HttpUtils | |||
| if (req.StatusCode == System.Net.HttpStatusCode.Unauthorized) | |||
| { | |||
| await SpotifyHelper.TryRefreshToken(authService, userResponse, identityService); | |||
| string newToken = await SpotifyHelper.TryRefreshToken(authService, userResponse, identityService); | |||
| if (newToken != null) | |||
| { | |||
| client.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Bearer " + newToken); | |||
| req = await client.GetAsync(url); | |||
| response = JsonConvert.DeserializeObject<T>(await req.Content.ReadAsStringAsync())!; | |||
| } | |||
| //ako to ne radi to znaci da je refresh token isteko, treba da se refreshuje | |||
| } | |||
| return response; | |||
| } | |||
| @@ -53,14 +61,37 @@ namespace SpotifyService.HttpUtils | |||
| } | |||
| public static async Task PutData(HttpClient client, string url, string userId, IIdentityService identityService) | |||
| public static async Task PutData(HttpClient client, string url, string userId, IIdentityService identityService, IAuthService authService) | |||
| { | |||
| var tokenMessage = await identityService.GetTokenByIdAsync(new GrpcShared.DTO.Db.DbRequestMessage { Id = userId }); | |||
| //add header | |||
| client.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Bearer " + tokenMessage.Token); | |||
| try | |||
| { | |||
| var tokenMessage = await identityService.GetTokenByIdAsync(new GrpcShared.DTO.Db.DbRequestMessage { Id = userId }); | |||
| //add header | |||
| client.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Bearer " + tokenMessage.Token); | |||
| //get request | |||
| var responseMessage = await client.PutAsync(url, null); | |||
| if(responseMessage.StatusCode == System.Net.HttpStatusCode.Unauthorized) | |||
| { | |||
| string newToken = await SpotifyHelper.TryRefreshToken(authService, tokenMessage, identityService); | |||
| if (newToken != null) | |||
| { | |||
| responseMessage = await client.PutAsync(url, null); | |||
| } | |||
| } | |||
| } | |||
| catch (RpcException e) | |||
| { | |||
| if (e.StatusCode == StatusCode.Cancelled) | |||
| { | |||
| //vrati message sa status kodom? | |||
| return; | |||
| } | |||
| throw; | |||
| } | |||
| //get request | |||
| await client.PutAsync(url, null); | |||
| } | |||
| @@ -89,6 +89,9 @@ builder.Services.AddCodeFirstGrpc(); | |||
| builder.Services.AddCodeFirstGrpcReflection(); | |||
| builder.Services.Configure<CodeRequest>(builder.Configuration.GetSection("AuthParams")); | |||
| builder.Services.AddScoped<IIdentityService, IdentityService>(); | |||
| builder.Services.AddScoped<IAuthService, AuthService>(); | |||
| var app = builder.Build(); | |||
| @@ -21,6 +21,7 @@ namespace SpotifyService.Services | |||
| _authService = authService; | |||
| } | |||
| public async Task<CurrentTrackResponse> GetCurrentlyPlayingTrack(SessionMessage message) | |||
| { | |||
| string url = "me/player/currently-playing"; | |||
| @@ -68,7 +68,7 @@ namespace SpotifyService.Services | |||
| string url = $"me/tracks/{query}"; | |||
| //the response type has nothing to do with the method, it's there so that the method can be called | |||
| await HttpUtils.HttpUtils<StatusCodeMessage>.PutData(client, url, request.UserId!, _identityService); | |||
| await HttpUtils.HttpUtils<StatusCodeMessage>.PutData(client, url, request.UserId!, _identityService, _authService); | |||
| } | |||
| public static string UriUtil(Dictionary<string, List<string>> param) | |||