From 05fe09f3ec167417ea94fc04148ac110f18c4ad6 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Thu, 16 Jun 2022 22:20:17 +0530 Subject: added api error handling to user/anime and user/manga packages --- user/manga/mangalist.go | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'user/manga/mangalist.go') diff --git a/user/manga/mangalist.go b/user/manga/mangalist.go index af75e9f..b60b1d7 100644 --- a/user/manga/mangalist.go +++ b/user/manga/mangalist.go @@ -17,18 +17,19 @@ package manga import ( - "encoding/json" + "encoding/json" "strconv" "fmt" e "github.com/MikunoNaka/MAL2Go/errhandlers" u "github.com/MikunoNaka/MAL2Go/util" + m "github.com/MikunoNaka/MAL2Go/manga" ) const BASE_URL string = "https://api.myanimelist.net/v2" const maxListLimit int = 1000 // Delete a manga from user's manga list -func (c Client)DeleteManga(id int) string { +func (c Client)DeleteManga(id int) (string, error) { endpoint := fmt.Sprintf("%s/manga/%d/my_list_status", BASE_URL, id) /* Returns 200 if manga successfully deleted * Alternatively returns 404 if manga not in user's manga list */ @@ -36,18 +37,19 @@ func (c Client)DeleteManga(id int) string { } // Get authenticated user's manga list -func (c Client) GetMangaList(user, status, sort string, limit, offset int, fields []string) (MangaList, error){ - var userMangaList MangaList +// returns true as second value if there are more mangas present +func (c Client) GetMangaList(user, status, sort string, limit, offset int, fields []string) ([]m.Manga, bool, error){ + var userMangaList []m.Manga // error handling for limit limitErr := e.LimitErrHandler(limit, maxListLimit) if limitErr != nil { - return userMangaList, limitErr + return userMangaList, false, limitErr } // handle all the errors for the fields fields, err := e.FieldsErrHandler(fields) if err != nil { - return userMangaList, err + return userMangaList, false, err } // append "list_status" field only used by this func. @@ -55,12 +57,12 @@ func (c Client) GetMangaList(user, status, sort string, limit, offset int, field // checks if valid sort is specified if !e.IsValidMangaListSort(sort) { - return userMangaList, e.InvalidSortError + return userMangaList, false, e.InvalidSortError } // checks if valid status is specified if status != "" && !e.IsValidMangaListStatus(status) { - return userMangaList, e.InvalidStatusError + return userMangaList, false, e.InvalidStatusError } // get own list if user not specified @@ -88,25 +90,23 @@ func (c Client) GetMangaList(user, status, sort string, limit, offset int, field } // get data from API - var mangaListData MangaListRaw - data := c.requestHandler(endpoint, "GET") + var mangaListData mangaListRaw + data, err := c.requestHandler(endpoint, "GET") + if err != nil { + return userMangaList, false, err + } json.Unmarshal([]byte(data), &mangaListData) - // set MyListStatus for each element and add it to array - var mangas []Manga - for _, element := range mangaListData.Data { - a := element.Manga - a.ListStatus = element.ListStatus + nextPageExists := mangaListData.Paging.NextPage != "" - mangas = append(mangas, a) - } + // set MyListStatus for each element and add it to slice + for _, element := range mangaListData.Data { + m := element.Manga + m.ListStatus = element.ListStatus - // finally create AnimeList - userMangaList = MangaList { - Mangas: mangas, - Paging: mangaListData.Paging, + userMangaList = append(userMangaList, m) } - return userMangaList, nil + return userMangaList, nextPageExists, nil } -- cgit v1.2.3