-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
87 lines (68 loc) · 1.8 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
type playlistList struct {
Items []Playlist `json:"items"`
Next string `json:"next"`
}
// Playlist holds the Name and ID of the playlist
type Playlist struct {
Name string `json:"name"`
ID string `json:"id"`
}
// User holds the information to make an http request for the user
type User struct {
token string
client *http.Client
}
// GetUser builds the user client
func GetUser(token string) User {
return User{token, &http.Client{}}
}
// GetPlaylists returns the name and ID for all playlists for the user
func (user *User) GetPlaylists() []Playlist {
playlists := []Playlist{}
url := "https://api.spotify.com/v1/me/playlists"
for len(url) > 0 {
res := user.call("GET", url)
if res.Body != nil {
defer res.Body.Close()
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal("Unable to parse response: ", err)
}
var data playlistList
err = json.Unmarshal(body, &data)
if err != nil {
log.Fatal("Unable to decode json: ", err)
}
url = data.Next
playlists = append(playlists, data.Items...)
}
return playlists
}
// UnfollowPlaylist 'deletes' a playlist for the user
func (user *User) UnfollowPlaylist(id string) {
res := user.call("DELETE", "https://api.spotify.com/v1/playlists/"+id+"/followers")
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
log.Println("Non-OK status received for delete request:", res.StatusCode)
}
}
func (user *User) call(method string, url string) *http.Response {
req, err := http.NewRequest(method, url, nil)
if err != nil {
log.Fatal("Error initating request: ", err)
}
req.Header.Set("Authorization", "Bearer "+user.token)
res, err := user.client.Do(req)
if err != nil {
log.Fatal("Error making request: ", err)
}
return res
}