From 97a02c2d9b309ae7e6ed2c0b807ed02913ded98c Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 31 Jan 2022 12:21:19 +0530 Subject: Added error handling for if limit is exceeded --- anime/anime.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/anime/anime.go b/anime/anime.go index 1439221..5d207ea 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "log" + "math" "strconv" ) @@ -75,16 +76,25 @@ func GetAnimeById(token string, animeId int, fields []string) (Anime, error) { } // Ranking is a list of anime sorted by their rank -func GetAnimeRanking(token string, rankingType string, limit int) (AnimeRanking, error) { +func GetAnimeRanking(token string, rankingType string, limit int, offset int) (AnimeRanking, error) { var animeRanking AnimeRanking + + // if limit exceeds what MAL supports + if limit > 500 { + return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Limit too high(%d). Max limit is 500", limit)) + } else if offset > 499 { + return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Offset too high(%d). Max offset for mal2go is 499", offset)) + } + + // if ranking type is invalid if !isValidRankingType(rankingType) { return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Invalid ranking type specified: \"%s\"", rankingType)) } endpoint, _ := urlGenerator( BASE_URL + "/ranking", - []string{"ranking_type", "limit"}, - [][]string{{rankingType}, {strconv.Itoa(limit)}}, + []string{"ranking_type", "limit", "offset"}, + [][]string{{rankingType}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}}, true, ) -- cgit v1.2.3