Daltron / Spartan

An Elegant Spotify Web API Library Written in Swift for iOS and macOS
MIT License
110 stars 16 forks source link

getNext doesn't appear to be working on getMyFollowedArtists #15

Open amiantos opened 6 years ago

amiantos commented 6 years ago

I'm trying to use if object.canMakeNextRequest to get all possible artists returned by getMyFollowedArtists however the second request doesn't appear to be returning an object with any items in it. I've tested my code with the getMyTopArtists method and it works properly. I've also manually changed the limit on getMyFollowedArtists to ensure that the API will return more than the default limit, and it does.

Here's a sample of my code just to see what I am doing.

    var artists: PagingObject<Artist>? {
        didSet {
            print("Added more to artists variable.")
        }
    }

    func grabSpotifyArtists(token: String?) {
        Spartan.authorizationToken = token
        Spartan.loggingEnabled = true

        _ = Spartan.getMyFollowedArtists(limit: 10, after: nil, success: { (object) in
            self.artists = object
            self.printArtists()
            self.fetchAllItems()
        }, failure: { (error) in
            print(error)
        })

    }

    func fetchAllItems() {
        if let artists = self.artists {
            if artists.canMakeNextRequest {
                artists.getNext(success: { (object) in
                    self.artists = object
                    self.printArtists()
                    self.fetchAllItems()
                }) { (error) in
                    print(error)
                }
            }
        }
    }

    func printArtists() {
        if let artists = self.artists?.items {
            for artist in artists {
                print(artist.name)
            }
        }
    }

Sample output:

🔵 [AlamoRecordLogger] GET https://api.spotify.com/v1/me/following?type=artist&limit=10
⚪️ [AlamoRecordLogger] GET https://api.spotify.com/v1/me/following?type=artist&limit=10 (200 OK) 0.15 seconds
Added more to artists variable.
Optional("Handsome Furs")
Optional("Nevermen")
Optional("White Denim")
Optional("Quilt")
Optional("The Poison Control Center")
Optional("The Courtneys")
Optional("Meat Wave")
Optional("The Spook School")
Optional("Savages")
Optional("Twin Peaks")
🔵 [AlamoRecordLogger] GET https://api.spotify.com/v1/me/following?type=artist&after=1xD85sp0kecIVuMwUHShxs&limit=10
⚪️ [AlamoRecordLogger] GET https://api.spotify.com/v1/me/following?type=artist&after=1xD85sp0kecIVuMwUHShxs&limit=10 (200 OK) 0.08 seconds
Added more to artists variable.

What I'd expect to happen is the what happens if I use the getMyTopArtists method like so... changed code following way:

    func grabSpotifyArtists(token: String?) {
        Spartan.authorizationToken = token
        Spartan.loggingEnabled = true

        _ = Spartan.getMyTopArtists(limit: 10, offset: 0, timeRange: .longTerm, success: { (object) in
            self.artists = object
            self.printArtists()
            self.fetchAllItems()
        }, failure: { (error) in
            print(error)
        })

    }

Output:

🔵 [AlamoRecordLogger] GET https://api.spotify.com/v1/me/top/artists?limit=10&offset=0&time_range=long_term
⚪️ [AlamoRecordLogger] GET https://api.spotify.com/v1/me/top/artists?limit=10&offset=0&time_range=long_term (200 OK) 0.67 seconds
Added more to artists variable.
Optional("Guided By Voices")
Optional("Lana Del Rey")
Optional("Meat Wave")
Optional("YACHT")
Optional("White Denim")
Optional("Spoon")
Optional("Electric Six")
Optional("Television")
Optional("Quilt")
Optional("Savages")
🔵 [AlamoRecordLogger] GET https://api.spotify.com/v1/me/top/artists?limit=10&offset=10&time_range=long_term
⚪️ [AlamoRecordLogger] GET https://api.spotify.com/v1/me/top/artists?limit=10&offset=10&time_range=long_term (200 OK) 0.19 seconds
Added more to artists variable.
Optional("Sleater-Kinney")
Optional("Queens of the Stone Age")
Optional("Local H")
Optional("Nick Lowe")
Optional("EMA")
Optional("Kishi Bashi")
Optional("Swearin\'")
Optional("BOYTOY")
Optional("Saint Motel")
Optional("Sleigh Bells")
🔵 [AlamoRecordLogger] GET https://api.spotify.com/v1/me/top/artists?limit=10&offset=20&time_range=long_term
⚪️ [AlamoRecordLogger] GET https://api.spotify.com/v1/me/top/artists?limit=10&offset=20&time_range=long_term (200 OK) 0.19 seconds
Added more to artists variable.
Optional("HAIM")
Optional("Wampire")
Optional("White Fence")
Optional("The Spook School")
Optional("Vampire Weekend")
Optional("Wolf Alice")
Optional("Woods")
Optional("Algebra Suicide")
Optional("Foxygen")
Optional("Twin Peaks")
🔵 [AlamoRecordLogger] GET https://api.spotify.com/v1/me/top/artists?limit=10&offset=30&time_range=long_term
⚪️ [AlamoRecordLogger] GET https://api.spotify.com/v1/me/top/artists?limit=10&offset=30&time_range=long_term (200 OK) 0.18 seconds
Added more to artists variable.
Optional("Joanna Gruesome")
Optional("Chastity Belt")
Optional("The High Strung")
Optional("Metz")
Optional("Archie Bronson Outfit")
Optional("Alex Winston")
Optional("Faith No More")
Optional("Posse")
Optional("Todd Terje")
Optional("Museum Mouth")
🔵 [AlamoRecordLogger] GET https://api.spotify.com/v1/me/top/artists?limit=10&offset=40&time_range=long_term
⚪️ [AlamoRecordLogger] GET https://api.spotify.com/v1/me/top/artists?limit=10&offset=40&time_range=long_term (200 OK) 0.18 seconds
Added more to artists variable.
Optional("Andrew Bird")
Optional("Phoenix")
Optional("Evans The Death")
Optional("California X")
Optional("Nine Inch Nails")
Optional("Richard Hell")
Optional("Harry Nilsson")
Optional("FIDLAR")
Optional("Kitten")
Optional("Rockpile")
tomaculum commented 6 years ago

Hi, I have a similar issue with the canMakeNextRequest but while searching. However the getMyFollowedArtists canMakeNextRequest does work for me.

This is a code snippet I use, just call it the first time with getFollowing(after: nil):

func getFollowing(after: String?) -> Void {

    _ = Spartan.getMyFollowedArtists(limit: 50, after: after, success: { (pagingObject) in

        /* do something with the object e.g. your code:
          self.artists = pagingObject
          self.printArtists()
          self.fetchAllItems()
         */ 

        if pagingObject.canMakeNextRequest {
            // (optional) sleep 100ms
            usleep(100000)
            self.getFollowing(after: pagingObject.cursors?.after)
        }

    }, failure: { (error) in
        print(error)
        // TODO error handling
    })
}
amiantos commented 6 years ago

@tomaculum thank you! This is clearly a workaround for a faulty .getNext() method, but it works and for that I am very grateful. I'm going to keep this issue open. A million thank yous!