diff options
author | Vidhu Kant Sharma <bokuwakanojogahoshii@yahoo.com> | 2022-02-28 05:35:39 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-28 05:35:39 +0000 |
commit | 06425ac756f916bca10d0f8797cf575a77dcf69d (patch) | |
tree | 7b4df51906c714f83a2bdce710e218a5cbb6672e /user/anime | |
parent | f44303ee9de8da4e330116d2c6e6048d59b52526 (diff) | |
parent | 38f7861cc533081aba7458ef52a25acdeddd1f16 (diff) |
Merge pull request #7 from MikunoNaka/user-mangalist
Added functionality to update manga lists
Diffstat (limited to 'user/anime')
-rw-r--r-- | user/anime/animelist.go | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 6516184..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,26 +70,38 @@ 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") json.Unmarshal([]byte(data), &animeListData) - // set MyListStatus for each element and add it to array + // set ListStatus for each element and add it to array var animes []a.Anime for _, element := range animeListData.Data { - a := element.Anime - a.MyListStatus = element.ListStatus + anime := element.Anime + anime.ListStatus = element.ListStatus - animes = append(animes, a) + animes = append(animes, anime) } // finally create AnimeList |