-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotifyHandlers.go
78 lines (57 loc) · 1.83 KB
/
spotifyHandlers.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
package main
import (
"net/http"
"github.com/gorilla/mux"
"encoding/json"
"./config"
)
func (spotify *Spotify) GetPlaylistsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
if vars["userID"] == "" {
http.Error(w, "MISSING_USER_ID", http.StatusInternalServerError)
return
}
playlists, err := spotify.controller.GetPlaylists(vars["userID"])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
js, err := json.Marshal(playlists)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(js)
}
func (spotify *Spotify) GetPlaylistHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
vars := mux.Vars(r)
if vars["userID"] == "" {
http.Error(w, "MISSING_USER_ID", http.StatusInternalServerError)
return
}
if vars["playlistID"] == "" {
http.Error(w, "MISSING_PLAYLIST_ID", http.StatusInternalServerError)
return
}
var playlist, err = spotify.controller.GetPlaylist(vars["userID"], vars["playlistID"])
js, err := json.Marshal(playlist)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(js)
}
func (spotify *Spotify) LoginHandler (w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
url := spotify.controller.Login(config.State)
http.Redirect(w, r, url, http.StatusSeeOther)
}
func (spotify *Spotify) CompleteAuth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
user := spotify.controller.CompleteAuth(w, r)
defer http.Redirect(w, r, config.RedirectLoggedURL + user.ID, http.StatusSeeOther)
}