package main import ( "github.com/gin-gonic/gin" "net/http" "log/slog" "encoding/json" "fmt" "io" ) type ArtistInfo struct { ID string Name string Popularity int Genres []string } // Define our route functions here 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 request and save the response into DB 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 artistInfo ArtistInfo err = json.Unmarshal(respData, &artistInfo) 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 } // Send back our response c.JSON(http.StatusOK, artistInfo) // 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"}) //} }