Skip to content

Commit

Permalink
Merge pull request #272 from flimzy/cleanups
Browse files Browse the repository at this point in the history
Misc minor cleanups
  • Loading branch information
strideynet authored Nov 14, 2024
2 parents 1e343d2 + 2955fbc commit f62d349
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 99 deletions.
4 changes: 0 additions & 4 deletions category.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ func (c *Client) GetCategory(ctx context.Context, id string, opts ...RequestOpti
}

err := c.get(ctx, spotifyURL, &cat)
if err != nil {
return cat, err
}

return cat, err
}

Expand Down
6 changes: 1 addition & 5 deletions library.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,5 @@ func (c *Client) modifyLibrary(ctx context.Context, typ string, add bool, ids ..
if err != nil {
return err
}
err = c.execute(req, nil)
if err != nil {
return err
}
return nil
return c.execute(req, nil)
}
37 changes: 6 additions & 31 deletions player.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,10 @@ func (c *Client) TransferPlayback(ctx context.Context, deviceID ID, play bool) e
if err != nil {
return err
}
err = c.execute(req, nil,
return c.execute(req, nil,
http.StatusAccepted,
http.StatusNoContent,
)
if err != nil {
return err
}

return nil
}

// Play Start a new context or resume current playback on the user's active
Expand Down Expand Up @@ -303,14 +298,10 @@ func (c *Client) PlayOpt(ctx context.Context, opt *PlayOptions) error {
if err != nil {
return err
}
err = c.execute(req, nil,
return c.execute(req, nil,
http.StatusAccepted,
http.StatusNoContent,
)
if err != nil {
return err
}
return nil
}

// Pause Playback on the user's currently active device.
Expand Down Expand Up @@ -339,14 +330,10 @@ func (c *Client) PauseOpt(ctx context.Context, opt *PlayOptions) error {
if err != nil {
return err
}
err = c.execute(req, nil,
return c.execute(req, nil,
http.StatusAccepted,
http.StatusNoContent,
)
if err != nil {
return err
}
return nil
}

// GetQueue gets the user's queue on the user's currently
Expand Down Expand Up @@ -432,14 +419,10 @@ func (c *Client) NextOpt(ctx context.Context, opt *PlayOptions) error {
if err != nil {
return err
}
err = c.execute(req, nil,
return c.execute(req, nil,
http.StatusAccepted,
http.StatusNoContent,
)
if err != nil {
return err
}
return nil
}

// Previous skips to the previous track in the user's queue on the user's
Expand Down Expand Up @@ -468,14 +451,10 @@ func (c *Client) PreviousOpt(ctx context.Context, opt *PlayOptions) error {
if err != nil {
return err
}
err = c.execute(req, nil,
return c.execute(req, nil,
http.StatusAccepted,
http.StatusNoContent,
)
if err != nil {
return err
}
return nil
}

// Seek to the given position in the user’s currently playing track.
Expand Down Expand Up @@ -587,12 +566,8 @@ func (c *Client) playerFuncWithOpt(ctx context.Context, urlSuffix string, values
if err != nil {
return err
}
err = c.execute(req, nil,
return c.execute(req, nil,
http.StatusAccepted,
http.StatusNoContent,
)
if err != nil {
return err
}
return nil
}
47 changes: 11 additions & 36 deletions playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ func (c *Client) FollowPlaylist(ctx context.Context, playlist ID, public bool) e
return err
}
req.Header.Set("Content-Type", "application/json")
err = c.execute(req, nil)
if err != nil {
return err
}
return nil
return c.execute(req, nil)
}

// UnfollowPlaylist [removes the current user as a follower of a playlist].
Expand All @@ -116,11 +112,7 @@ func (c *Client) UnfollowPlaylist(ctx context.Context, playlist ID) error {
if err != nil {
return err
}
err = c.execute(req, nil)
if err != nil {
return err
}
return nil
return c.execute(req, nil)
}

func buildFollowURI(url string, playlist ID) string {
Expand Down Expand Up @@ -202,7 +194,7 @@ func (c *Client) GetPlaylistTracks(
return nil, err
}

return &result, err
return &result, nil
}

// PlaylistItem contains info about an item in a playlist.
Expand Down Expand Up @@ -249,20 +241,12 @@ func (t *PlaylistItemTrack) UnmarshalJSON(b []byte) error {

switch itemType.Type {
case "episode":
err := json.Unmarshal(b, &t.Episode)
if err != nil {
return err
}
return json.Unmarshal(b, &t.Episode)
case "track":
err := json.Unmarshal(b, &t.Track)
if err != nil {
return err
}
return json.Unmarshal(b, &t.Track)
default:
return fmt.Errorf("unrecognized item type: %s", itemType.Type)
}

return nil
}

// PlaylistItemPage contains information about items in a playlist.
Expand Down Expand Up @@ -295,7 +279,7 @@ func (c *Client) GetPlaylistItems(ctx context.Context, playlistID ID, opts ...Re
return nil, err
}

return &result, err
return &result, nil
}

// CreatePlaylistForUser [creates a playlist] for a Spotify user.
Expand Down Expand Up @@ -338,7 +322,7 @@ func (c *Client) CreatePlaylistForUser(ctx context.Context, userID, playlistName
return nil, err
}

return &p, err
return &p, nil
}

// ChangePlaylistName [changes the name of a playlist]. This call requires that the
Expand Down Expand Up @@ -407,11 +391,7 @@ func (c *Client) modifyPlaylist(ctx context.Context, playlistID ID, newName, new
return err
}
req.Header.Set("Content-Type", "application/json")
err = c.execute(req, nil, http.StatusCreated)
if err != nil {
return err
}
return nil
return c.execute(req, nil, http.StatusCreated)
}

// AddTracksToPlaylist [adds one or more tracks to a user's playlist].
Expand Down Expand Up @@ -570,12 +550,7 @@ func (c *Client) ReplacePlaylistTracks(ctx context.Context, playlistID ID, track
if err != nil {
return err
}
err = c.execute(req, nil, http.StatusCreated)
if err != nil {
return err
}

return nil
return c.execute(req, nil, http.StatusCreated)
}

// ReplacePlaylistItems [replaces all the items in a playlist], overwriting its
Expand Down Expand Up @@ -637,7 +612,7 @@ func (c *Client) UserFollowsPlaylist(ctx context.Context, playlistID ID, userIDs
return nil, err
}

return follows, err
return follows, nil
}

// PlaylistReorderOptions is used with ReorderPlaylistTracks to reorder
Expand Down Expand Up @@ -697,7 +672,7 @@ func (c *Client) ReorderPlaylistTracks(ctx context.Context, playlistID ID, opt P
return "", err
}

return result.SnapshotID, err
return result.SnapshotID, nil
}

// SetPlaylistImage replaces the image used to represent a playlist.
Expand Down
14 changes: 6 additions & 8 deletions playlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand All @@ -26,7 +26,7 @@ func TestFeaturedPlaylists(t *testing.T) {
if msg != "New Music Friday!" {
t.Errorf("Want 'Enjoy a mellow afternoon.', got'%s'\n", msg)
}
if p.Playlists == nil || len(p.Playlists) == 0 {
if len(p.Playlists) == 0 {
t.Fatal("Empty playlists result")
}
expected := "New Music Friday Sweden"
Expand All @@ -37,7 +37,6 @@ func TestFeaturedPlaylists(t *testing.T) {
if desc := p.Playlists[0].Description; desc != expected {
t.Errorf("Want '%s', got '%s'\n", expected, desc)
}

}

func TestFeaturedPlaylistsExpiredToken(t *testing.T) {
Expand Down Expand Up @@ -88,7 +87,6 @@ func TestPlaylistsForUser(t *testing.T) {
if p.Description != expected {
t.Errorf("Expected '%s', got '%s'\n", expected, p.Description)
}

}

func TestGetPlaylist(t *testing.T) {
Expand Down Expand Up @@ -478,7 +476,7 @@ func TestAddTracksToPlaylist(t *testing.T) {

func TestRemoveTracksFromPlaylist(t *testing.T) {
client, server := testClientString(http.StatusOK, `{ "snapshot_id" : "JbtmHBDBAYu3/bt8BOXKjzKx3i0b6LCa/wVjyl6qQ2Yf6nFXkbmzuEa+ZI/U1yF+" }`, func(req *http.Request) {
requestBody, err := ioutil.ReadAll(req.Body)
requestBody, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal("Could not read request body:", err)
}
Expand Down Expand Up @@ -518,7 +516,7 @@ func TestRemoveTracksFromPlaylist(t *testing.T) {

func TestRemoveTracksFromPlaylistOpt(t *testing.T) {
client, server := testClientString(http.StatusOK, `{ "snapshot_id" : "JbtmHBDBAYu3/bt8BOXKjzKx3i0b6LCa/wVjyl6qQ2Yf6nFXkbmzuEa+ZI/U1yF+" }`, func(req *http.Request) {
requestBody, err := ioutil.ReadAll(req.Body)
requestBody, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -617,7 +615,7 @@ func TestClient_ReplacePlaylistItems(t *testing.T) {
var gotRequestBody string

c, server := testClientString(tt.clientFields.httpCode, tt.clientFields.body, func(request *http.Request) {
b, err := ioutil.ReadAll(request.Body)
b, err := io.ReadAll(request.Body)
defer request.Body.Close()
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -729,7 +727,7 @@ func TestSetPlaylistImage(t *testing.T) {
if req.Method != "PUT" {
t.Errorf("expected a PUT, got a %s\n", req.Method)
}
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 2 additions & 10 deletions spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -161,7 +160,7 @@ func (e Error) Error() string {

// decodeError decodes an Error from an io.Reader.
func (c *Client) decodeError(resp *http.Response) error {
responseBody, err := ioutil.ReadAll(resp.Body)
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down Expand Up @@ -296,15 +295,8 @@ func (c *Client) get(ctx context.Context, url string, result interface{}) error
return c.decodeError(resp)
}

err = json.NewDecoder(resp.Body).Decode(result)
if err != nil {
return err
}

break
return json.NewDecoder(resp.Body).Decode(result)
}

return nil
}

// NewReleases gets a list of new album releases featured in Spotify.
Expand Down
6 changes: 1 addition & 5 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,7 @@ func (c *Client) modifyFollowers(ctx context.Context, usertype string, follow bo
if err != nil {
return err
}
err = c.execute(req, nil, http.StatusNoContent)
if err != nil {
return err
}
return nil
return c.execute(req, nil, http.StatusNoContent)
}

// CurrentUsersFollowedArtists gets the [current user's followed artists].
Expand Down

0 comments on commit f62d349

Please sign in to comment.