2021-08-28 16:08:54 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-28 21:05:38 +02:00
|
|
|
// "bytes"
|
2021-08-28 16:08:54 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-08-28 23:46:45 +02:00
|
|
|
// "time"
|
2021-08-28 16:08:54 +02:00
|
|
|
"net/http"
|
2021-08-28 19:20:52 +02:00
|
|
|
"encoding/json"
|
2021-08-28 16:08:54 +02:00
|
|
|
)
|
|
|
|
|
2021-08-28 19:20:52 +02:00
|
|
|
var MyClient = &http.Client{}
|
|
|
|
|
|
|
|
/*
|
|
|
|
id de la playlist
|
|
|
|
*/
|
|
|
|
func getListPlaylist(id string) {
|
|
|
|
req, _ := http.NewRequest("GET", "https://api.spotify.com/v1/playlists/"+id+"/tracks", nil)
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
req.Header.Add("Authorization", SpotifyAPI)
|
|
|
|
|
|
|
|
res, err := MyClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-28 23:46:45 +02:00
|
|
|
if res.StatusCode > 300 {
|
|
|
|
fmt.Printf("Erreur !!\n")
|
|
|
|
return
|
|
|
|
}
|
2021-08-28 19:20:52 +02:00
|
|
|
|
2021-08-28 23:46:45 +02:00
|
|
|
ree := &SpotifyPlaylist{}
|
|
|
|
defer res.Body.Close()
|
2021-08-28 21:05:38 +02:00
|
|
|
err = json.NewDecoder(res.Body).Decode(&ree)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(ree.Href)
|
2021-08-28 19:20:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-28 16:08:54 +02:00
|
|
|
func formHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
fmt.Fprintf(w, "ParseForm() err: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-28 19:20:52 +02:00
|
|
|
fmt.Fprintf(w, "POST request successful\n")
|
2021-08-28 16:08:54 +02:00
|
|
|
type_id := r.FormValue("type-id")
|
|
|
|
id := r.FormValue("id")
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "Type ID = %s\n", type_id)
|
|
|
|
fmt.Fprintf(w, "ID = %s\n", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func index(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/hello" {
|
|
|
|
http.Error(w, "404", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "Use GET pls", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "Helo!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-08-28 19:20:52 +02:00
|
|
|
getListPlaylist("6OGZZ8tI45MB1d3EUEqNKI")
|
|
|
|
return
|
|
|
|
|
2021-08-28 16:08:54 +02:00
|
|
|
fileServer := http.FileServer(http.Dir("./static"))
|
|
|
|
http.Handle("/", fileServer)
|
|
|
|
http.HandleFunc("/hello", index)
|
|
|
|
http.HandleFunc("/back", formHandler)
|
|
|
|
|
|
|
|
fmt.Printf("Starting the server…\n")
|
|
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|