From 10499ad3cf8db85a5c5ebd8112415d17a727c2ec Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Thu, 24 Feb 2022 22:05:32 +0530 Subject: added fields support for GetAnimeList --- user/anime/animelist.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'user/anime') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index f9ddff5..f6d9965 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -23,6 +23,7 @@ import ( "errors" a "github.com/MikunoNaka/MAL2Go/anime" e "github.com/MikunoNaka/MAL2Go/errhandlers" + u "github.com/MikunoNaka/MAL2Go/util" ) const BASE_URL string = "https://api.myanimelist.net/v2" @@ -37,7 +38,7 @@ func (c Client)DeleteAnime(id int) string { } // Get authenticated user's anime list -func (c Client) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ +func (c Client) GetAnimeList(user, status, sort string, limit, offset int, fields []string) (a.AnimeList, error){ var userAnimeList a.AnimeList // error handling for limit limitErr := e.LimitErrHandler(limit, maxListLimit) @@ -45,6 +46,15 @@ func (c Client) GetAnimeList(user, status, sort string, limit, offset int) (a.An return userAnimeList, limitErr } + // handle all the errors for the fields + fields, err := e.FieldsErrHandler(fields) + if err != nil { + return userAnimeList, err + } + + // append "list_status" field only used by this func. + fields = append(fields, "list_status") + // checks if valid sort is specified if !e.IsValidListSort(sort) { return userAnimeList, errors.New(fmt.Sprintf("GetAnimeList: Invalid sort specified: \"%s\"", sort)) @@ -60,14 +70,26 @@ func (c Client) GetAnimeList(user, status, sort string, limit, offset int) (a.An user = "@me" } - // if status is "" it returns all anime var endpoint string + // if status is "" it returns all anime if status == "" { - endpoint = BASE_URL + "/users/" + user + "/animelist?sort=" + sort + "&limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset) + endpoint, _ = u.UrlGenerator( + BASE_URL + "/users/" + user + "/animelist", + []string{"sort", "limit", "offset", "fields"}, + [][]string{{sort}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, + true, + ) } else { - endpoint = BASE_URL + "/users/" + user + "/animelist?status=" + status + "&sort=" + sort + "&limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset) + // status gets included if specified + endpoint, _ = u.UrlGenerator( + BASE_URL + "/users/" + user + "/animelist", + []string{"status", "sort", "limit", "offset", "fields"}, + [][]string{{status}, {sort}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, + true, + ) } + // get data from API var animeListData AnimeListRaw data := c.requestHandler(endpoint, "GET") -- cgit v1.2.3