public async Task <ActionResult <SpotifyPlayResponse> > Tocar(PlayRequest request) { try { SpotifyPlayResponse response = await ChamarSpotifyApi(request); return(response); } catch (Exception ex) { return(BadRequest( new Models.Response.ErroResponse(500, ex.Message) )); } }
public async Task <SpotifyPlayResponse> ChamarSpotifyApi(PlayRequest request) { // Gera token de acesso string token = await GerarTokenAcesso(); // Cria objeto de conexao com spotify-api HttpClient api = new HttpClient(); api.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); // Cria body-request para enviar para spotify-api SpotifyPlayRequest spotifyRequest = new SpotifyPlayRequest(); spotifyRequest.uris.Add(request.MusicaURI); string json = JsonConvert.SerializeObject(spotifyRequest); StringContent body = new StringContent(json); // Chama spotify-api verbo PUT HttpResponseMessage spotifyResponse = await api.PutAsync(URL_SPOTIFY_PLAY, body); // Se api expirou, reseta token if (spotifyResponse.StatusCode == HttpStatusCode.Unauthorized) { TOKEN = string.Empty; } // Cria objeto response com responsta da spotify-api SpotifyPlayResponse response = new SpotifyPlayResponse(); response.Status = (int)spotifyResponse.StatusCode; response.Mensagem = await spotifyResponse.Content.ReadAsStringAsync(); return(response); }