package main import ( "github.com/gin-gonic/gin" "net/http" "log/slog" "encoding/json" "fmt" "io" ) type SpotifyResponse struct { ID string Name string Popularity int Genres []string } func ping(c *gin.Context) { c.String(http.StatusOK, "pong") } func getArtistByID(c *gin.Context) { artistID := c.Params.ByName("artistID") spotifyAuthToken := c.GetString("spotifyAuthToken") if artistID == "" || spotifyAuthToken == "Bearer " { c.JSON(http.StatusBadRequest, gin.H{"Error": "Could not find required parameters and/or required authentication tokens"}) return } // Make a request to spotify API to grab artist data artistEndpoint := fmt.Sprintf("https://api.spotify.com/v1/artists/%s", artistID) req, err := http.NewRequest("GET", artistEndpoint, nil) if err != nil { slog.Error("[GOMUSIC] Failed to build HTTP request", "Error", err) c.JSON(http.StatusInternalServerError, gin.H{"Error": "Failed to request latest spotify data"}) return } req.Header.Add("Authorization", spotifyAuthToken) // Send off request resp, err := http.DefaultClient.Do(req) if err != nil { slog.Error("[GOMUSIC] Failed to get artist data from spotify API", "Error", err) c.JSON(http.StatusInternalServerError, gin.H{"Error": "Failed to request latest spotify data"}) return } else if resp.StatusCode != 200 { slog.Error("[GOMUSIC] Failed to get artist data from spotify API", "Error", resp.Status) c.JSON(http.StatusInternalServerError, gin.H{"Error": "Failed to request latest spotify data"}) return } respData, err := io.ReadAll(resp.Body) if err != nil { slog.Error("[GOMUSIC] Failed to read response data from spotify API", "Error", resp.Status) c.JSON(http.StatusInternalServerError, gin.H{"Error": "Failed to request latest spotify data"}) return } // Close this immediately since it's unused now resp.Body.Close() var spotifyResponse SpotifyResponse err = json.Unmarshal(respData, &spotifyResponse) if err != nil { slog.Error("[GOMUSIC] Failed to read response body data from spotify", "Error", err) c.JSON(http.StatusInternalServerError, gin.H{"Error": "Failed to request latest spotify data"}) return } // Update DB here //value, ok := db[artistID] //if ok { // c.JSON(http.StatusOK, gin.H{"artistID": artistID, "value": value}) //} else { // c.JSON(http.StatusOK, gin.H{"artistID": artistID, "status": "no value"}) //} // Send back our response data c.JSON(http.StatusOK, spotifyResponse) }