MaxHasADHD / TraktKit

Swift wrapper for Trakt.tv API.
MIT License
112 stars 37 forks source link

getWatchedShows(extended: [.noSeasons]) gives error #37

Closed ghost closed 4 years ago

ghost commented 5 years ago

Hi, I'm trying to use the method getWatchedShows with the argument ExtendedType.noSeasons like this:

TraktManager.sharedManager.getWatchedShows(extended: [.noSeasons]) { result in
    switch result {
        case .success (let data):
            print(data)
        case .error (let error):
            print(error.debugDescription)
    }
}

And I'm getting the following error:

Optional(Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "seasons", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"seasons\", intValue: nil) (\"seasons\").", underlyingError: nil)))
MaxHasADHD commented 4 years ago

I'll try looking into this soon.

cyberclectic commented 4 years ago

The key is in the api document...

If type is set to shows and you add ?extended=noseasons to the URL, it won't return season or episode info.

Thus, on the model TraktWatchedShows if we allow seasons to be an optional array, this will then decode properly...

public let seasons: [TraktWatchedSeason]?

Since a season element is not returned...

Sample of json returned using .noSeasons is below

[
    {
        "last_watched_at": "2020-05-05T23:54:22.000Z",
        "reset_at": null,
        "last_updated_at": "2020-05-05T23:57:07.000Z",
        "plays": 6,
        "show": {
            "title": "Driven",
            "year": 2020,
            "ids": {
                "tmdb": null,
                "slug": "driven-2020",
                "tvdb": 378677,
                "tvrage": null,
                "trakt": 159421,
                "imdb": null
            }
        }
    },
    {
        "last_watched_at": "2020-05-05T23:54:10.000Z",
        "reset_at": null,
        "last_updated_at": "2020-05-05T23:59:06.000Z",
        "plays": 37,
        "show": {
            "title": "Street Outlaws: Memphis",
            "year": 2018,
            "ids": {
                "tmdb": 76107,
                "slug": "street-outlaws-memphis",
                "tvdb": 340321,
                "tvrage": null,
                "trakt": 126601,
                "imdb": "tt9177338"
            }
        }
    }
]

If you would like a PR, I can submit one including a new test for this response.