Skip to content

Commit

Permalink
refactor: protect spoty pkg deps
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed Apr 11, 2022
1 parent aa155e4 commit db149b1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
17 changes: 3 additions & 14 deletions server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Error struct {
// @Description checks if server is running
// @Tags core
// @Produce json
// @Success 200 {object} Success
// @Success 200 {object} server.Success
// @Router /api [get]
func (s *Server) handleHealthCheck() gin.HandlerFunc {
return func(c *gin.Context) {
Expand Down Expand Up @@ -92,7 +92,7 @@ func (s *Server) handleCurrentTrackImages(c *gin.Context) {
// @Failure 403 {object} server.Error "already authenticated"
// @Router /api/authenticate [get]
func (s *Server) handleAuthenticate(c *gin.Context) {
c.Redirect(http.StatusFound, s.spoty.Auth.AuthURL(s.spoty.State))
c.Redirect(http.StatusFound, s.spoty.AuthURL())
}

// handleCallback godoc
Expand All @@ -108,21 +108,10 @@ func (s *Server) handleAuthenticate(c *gin.Context) {
// @Failure 404 {object} server.Error "could not retrieve current user"
// @Router /api/callback [get]
func (s *Server) handleCallback(c *gin.Context) {
tok, err := s.spoty.Auth.Token(s.spoty.State, c.Request)
if err != nil {
if err := s.spoty.SetupNewClient(c.Request); err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "could not retrieve token"})
return
}

client := s.spoty.Auth.NewClient(tok)
if _, err := client.CurrentUser(); err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": "could not retrieve current user"})
return
}

client.AutoRetry = true

s.spoty.Client = &client

c.JSON(http.StatusOK, gin.H{"success": "welcome, you are now authenticated!"})
}
33 changes: 25 additions & 8 deletions spoty/spoty.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ type Image struct {
}

type Spoty struct {
Client *spotify.Client
client *spotify.Client

Auth spotify.Authenticator
State string
auth spotify.Authenticator
state string

cache *cache.Cache
}
Expand Down Expand Up @@ -59,25 +59,42 @@ func New(clientID, clientSecret, host string, port int, cache *cache.Cache) (*Sp
}

return &Spoty{
Auth: auth,
State: state.String(),
auth: auth,
state: state.String(),
cache: cache,
}, nil
}

func (s *Spoty) IsAuth() bool {
return s.Client != nil
return s.client != nil
}

func (s *Spoty) IsPlaying() bool {
state, err := s.Client.PlayerState()
state, err := s.client.PlayerState()
if err != nil {
return false
}

return state.Playing
}

func (s *Spoty) AuthURL() string {
return s.auth.AuthURL(s.state)
}

func (s *Spoty) SetupNewClient(r *http.Request) error {
tok, err := s.auth.Token(s.state, r)
if err != nil {
return err
}

client := s.auth.NewClient(tok)
client.AutoRetry = true
s.client = &client

return nil
}

func (s *Spoty) TrackCurrentlyPlaying() (*spotify.FullTrack, error) {
const cacheCurrentTrackKey = "current_track"

Expand All @@ -90,7 +107,7 @@ func (s *Spoty) TrackCurrentlyPlaying() (*spotify.FullTrack, error) {
return nil, errors.New("no track currently playing")
}

playing, err := s.Client.PlayerCurrentlyPlaying()
playing, err := s.client.PlayerCurrentlyPlaying()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit db149b1

Please sign in to comment.