-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.go
63 lines (53 loc) · 1.61 KB
/
spotify.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
package api
import (
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/tjhorner/dash/util"
"github.com/zmb3/spotify"
"golang.org/x/oauth2"
)
// SpotifyAPI is the API that handles the Spotify panel
type SpotifyAPI struct {
ClientID string
ClientSecret string
RedirectURL string
AuthToken string
RefreshToken string
spotifyClient *spotify.Client
}
// Configure implements API.Configure
func (api *SpotifyAPI) Configure() {
api.ClientID = util.GetEnv("DASH_SPOTIFY_CLIENT_ID", "")
api.ClientSecret = util.GetEnv("DASH_SPOTIFY_CLIENT_SECRET", "")
api.RedirectURL = util.GetEnv("DASH_SPOTIFY_REDIRECT_URL", "")
api.AuthToken = util.GetEnv("DASH_SPOTIFY_AUTH_TOKEN", "")
api.RefreshToken = util.GetEnv("DASH_SPOTIFY_REFRESH_TOKEN", "")
token := &oauth2.Token{
Expiry: time.Now(), // so we always refresh the token
TokenType: "Bearer",
AccessToken: api.AuthToken,
RefreshToken: api.RefreshToken,
}
auth := spotify.NewAuthenticator(api.RedirectURL, spotify.ScopeUserReadPlaybackState)
auth.SetAuthInfo(api.ClientID, api.ClientSecret)
client := auth.NewClient(token)
api.spotifyClient = &client
}
// Prefix implements API.Prefix
func (api *SpotifyAPI) Prefix() string {
return "/api/spotify"
}
// Route implements API.Route
func (api *SpotifyAPI) Route(router *mux.Router) {
router.HandleFunc("/playing", api.getPlaying).Methods("GET")
}
// GET /playing
func (api *SpotifyAPI) getPlaying(w http.ResponseWriter, r *http.Request) {
playing, err := api.spotifyClient.PlayerState()
if err != nil {
respondError(err, "ERR_SPOTIFY_RESPONSE", w, r)
return
}
respondJSON(*playing, w, r)
}