From 757d55e125471cbc729b8b1e514764eaedc5c478 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Aug 2021 23:46:45 +0200 Subject: [PATCH] update(spotify/json): check if json is ok with status and not json structure --- server.go | 153 +++--------------------------------------------------- struct.go | 7 --- 2 files changed, 7 insertions(+), 153 deletions(-) diff --git a/server.go b/server.go index 8a26cba..5c85a06 100644 --- a/server.go +++ b/server.go @@ -4,130 +4,13 @@ import ( // "bytes" "fmt" "log" - "time" +// "time" "net/http" "encoding/json" ) var MyClient = &http.Client{} - -type YoutubeAPI struct { - Href string `json:"href"` - Items []struct { - AddedAt time.Time `json:"added_at"` - AddedBy struct { - ExternalUrls struct { - Spotify string `json:"spotify"` - - } `json:"external_urls"` - Href string `json:"href"` - ID string `json:"id"` - Type string `json:"type"` - URI string `json:"uri"` - - } `json:"added_by"` - IsLocal bool `json:"is_local"` - PrimaryColor interface{} `json:"primary_color"` - Track struct { - Album struct { - AlbumType string `json:"album_type"` - Artists []struct { - ExternalUrls struct { - Spotify string `json:"spotify"` - - } `json:"external_urls"` - Href string `json:"href"` - ID string `json:"id"` - Name string `json:"name"` - Type string `json:"type"` - URI string `json:"uri"` - - } `json:"artists"` - AvailableMarkets []string `json:"available_markets"` - ExternalUrls struct { - Spotify string `json:"spotify"` - - } `json:"external_urls"` - Href string `json:"href"` - ID string `json:"id"` - Images []struct { - Height int `json:"height"` - URL string `json:"url"` - Width int `json:"width"` - - } `json:"images"` - Name string `json:"name"` - ReleaseDate string `json:"release_date"` - ReleaseDatePrecision string `json:"release_date_precision"` - TotalTracks int `json:"total_tracks"` - Type string `json:"type"` - URI string `json:"uri"` - - } `json:"album"` - Artists []struct { - ExternalUrls struct { - Spotify string `json:"spotify"` - - } `json:"external_urls"` - Href string `json:"href"` - ID string `json:"id"` - Name string `json:"name"` - Type string `json:"type"` - URI string `json:"uri"` - - } `json:"artists"` - AvailableMarkets []string `json:"available_markets"` - DiscNumber int `json:"disc_number"` - DurationMs int `json:"duration_ms"` - Episode bool `json:"episode"` - Explicit bool `json:"explicit"` - ExternalIds struct { - Isrc string `json:"isrc"` - - } `json:"external_ids"` - ExternalUrls struct { - Spotify string `json:"spotify"` - - } `json:"external_urls"` - Href string `json:"href"` - ID string `json:"id"` - IsLocal bool `json:"is_local"` - Name string `json:"name"` - Popularity int `json:"popularity"` - PreviewURL string `json:"preview_url"` - Track bool `json:"track"` - TrackNumber int `json:"track_number"` - Type string `json:"type"` - URI string `json:"uri"` - - } `json:"track"` - VideoThumbnail struct { - URL interface{} `json:"url"` - - } `json:"video_thumbnail"` - - } `json:"items"` - Limit int `json:"limit"` - Next string `json:"next"` - Offset int `json:"offset"` - Previous interface{} `json:"previous"` - Total int `json:"total"` - -} - -type Test struct { - Tee string -} - -type ErrorSpotify struct { - href string -} - -type Error struct { - status, message string -} - /* id de la playlist */ @@ -142,43 +25,21 @@ func getListPlaylist(id string) { fmt.Printf("error:", err) return } - fmt.Printf("hm", res.Status) - /* - buf := new(bytes.Buffer) - buf.ReadFrom(res.Body) - fmt.Printf("coucou %+v", buf.String()) - */ + if res.StatusCode > 300 { + fmt.Printf("Erreur !!\n") + return + } - //ree := &ErrorSpotify{} - var ree YoutubeAPI - //defer res.Body.Close() - //json.Unmarshal(res.Body, ree) + ree := &SpotifyPlaylist{} + defer res.Body.Close() err = json.NewDecoder(res.Body).Decode(&ree) if err != nil { fmt.Printf("error:", err) return } - //json.NewDecoder(res.Body).Decode(interface{}(ree)) - //decoder := json.NewDecoder(res.Body).Decode(interface{}(ree)) - /* - for token, _ := decoder.Token(); token != nil; token, _ = decoder.Token() { - fmt.Printf("value:", token) - } - */ - //decoder.Decode(interface{}(ree)) fmt.Printf(ree.Href) - fmt.Printf("coucou\n") - return - - token := map[string]string{} - fmt.Printf("coucou\n") - - for _, v := range token { - fmt.Printf("coucou") - fmt.Printf("\n", v) - } } func formHandler(w http.ResponseWriter, r *http.Request) { diff --git a/struct.go b/struct.go index a1d6988..9fab46d 100644 --- a/struct.go +++ b/struct.go @@ -2,13 +2,6 @@ package main import "time" -type SpotyfyError struct { - Error struct { - Status int `json:"status"` - Message string `json:"message"` - } `json:"error"` -} - type SpotifyPlaylist struct { Href string `json:"href"` Items []struct {