From 1d3f72c1b48998b86fd1740e893559b6dcaf7663 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 13:42:06 +0530 Subject: modularised the code for easy access of various functions in packages --- anime/anime.go | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index 90231e3..323105b 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -21,6 +21,8 @@ import ( "errors" "fmt" "strconv" + e "github.com/MikunoNaka/mal2go/errhandlers" + u "github.com/MikunoNaka/mal2go/util" ) const BASE_URL string = "https://api.myanimelist.net/v2/anime" @@ -30,19 +32,19 @@ func (c AnimeClient) SearchAnime(searchString string, limit, offset int, fields var searchResults AnimeSearch // error handling for limit and offset - limitsErr := limitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset) if limitsErr != nil { return searchResults, limitsErr } // handle all the errors for the fields - fields, err := fieldsErrHandler(fields) + fields, err := e.FieldsErrHandler(fields) if err != nil { return searchResults, err } // generate endpoint url with custom params - endpoint, _ := urlGenerator( + endpoint, _ := u.UrlGenerator( BASE_URL, []string{"q", "limit", "offset", "fields"}, [][]string{{searchString}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, @@ -74,12 +76,12 @@ func (c AnimeClient) GetAnimeById(animeId int, fields []string) (Anime, error) { var anime Anime // handle all the errors for the fields - fields, err := fieldsErrHandler(fields) + fields, err := e.FieldsErrHandler(fields) if err != nil { return anime, err } - endpoint, _ := urlGenerator( + endpoint, _ := u.UrlGenerator( BASE_URL + "/" + strconv.Itoa(animeId), []string{"fields"}, /* it seems to still return all fields from the API. @@ -100,23 +102,23 @@ func (c AnimeClient) GetAnimeRanking(rankingType string, limit, offset int, fiel var animeRanking AnimeRanking // error handling for limit and offset - limitsErr := limitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset) if limitsErr != nil { return animeRanking, limitsErr } // handle all the errors for the fields - fields, err := fieldsErrHandler(fields) + fields, err := e.FieldsErrHandler(fields) if err != nil { return animeRanking, err } // if ranking type is invalid - if !isValidRankingType(rankingType) { + if !e.IsValidRankingType(rankingType) { return animeRanking, errors.New(fmt.Sprintf("GetAnimeRanking: Invalid ranking type specified: \"%s\"", rankingType)) } - endpoint, _ := urlGenerator( + endpoint, _ := u.UrlGenerator( BASE_URL + "/ranking", []string{"ranking_type", "limit", "offset", "fields"}, [][]string{{rankingType}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, @@ -157,28 +159,28 @@ func (c AnimeClient) GetSeasonalAnime(year, season, sort string, limit, offset i var seasonalAnime SeasonalAnime // error handling for limit and offset - limitsErr := limitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset) if limitsErr != nil { return seasonalAnime, limitsErr } // handle all the errors for the fields - fields, err := fieldsErrHandler(fields) + fields, err := e.FieldsErrHandler(fields) if err != nil { return seasonalAnime, err } // checks if valid season is specified - if !isValidSeason(season) { + if !e.IsValidSeason(season) { return seasonalAnime, errors.New(fmt.Sprintf("GetSeasonalAnime: Invalid season specified: \"%s\"", season)) } // checks if valid sort is specified - if !isValidSort(sort) { + if !e.IsValidSort(sort) { return seasonalAnime, errors.New(fmt.Sprintf("GetSeasonalAnime: Invalid sort specified: \"%s\"", sort)) } - endpoint, _ := urlGenerator( + endpoint, _ := u.UrlGenerator( BASE_URL + fmt.Sprintf("/season/%s/%s", year, season), []string{"sort", "limit", "offset", "fields"}, [][]string{{sort}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, @@ -211,18 +213,18 @@ func (c AnimeClient) GetSuggestedAnime(limit, offset int, fields []string) (Sugg var suggestedAnime SuggestedAnime // error handling for limit and offset - limitsErr := limitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset) if limitsErr != nil { return suggestedAnime, limitsErr } // handle all the errors for the fields - fields, err := fieldsErrHandler(fields) + fields, err := e.FieldsErrHandler(fields) if err != nil { return suggestedAnime, err } - endpoint, _ := urlGenerator( + endpoint, _ := u.UrlGenerator( BASE_URL + "/suggestions", []string{"limit", "offset", "fields"}, [][]string{{strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, -- cgit v1.2.3 From 937f3b8ada85274dfe3842f3dde8aef45c4f3ae7 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 14:12:45 +0530 Subject: completed (?) the GetAnimeList function --- anime/anime.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'anime/anime.go') diff --git a/anime/anime.go b/anime/anime.go index 323105b..969b04c 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -27,12 +27,15 @@ import ( const BASE_URL string = "https://api.myanimelist.net/v2/anime" +// MAL Might change this +const maxAnimeLimit int = 500 + // in MAL documentation this is named Get Anime List func (c AnimeClient) SearchAnime(searchString string, limit, offset int, fields []string) (AnimeSearch, error) { var searchResults AnimeSearch // error handling for limit and offset - limitsErr := e.LimitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset, maxAnimeLimit) if limitsErr != nil { return searchResults, limitsErr } @@ -102,7 +105,7 @@ func (c AnimeClient) GetAnimeRanking(rankingType string, limit, offset int, fiel var animeRanking AnimeRanking // error handling for limit and offset - limitsErr := e.LimitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset, maxAnimeLimit) if limitsErr != nil { return animeRanking, limitsErr } @@ -159,7 +162,7 @@ func (c AnimeClient) GetSeasonalAnime(year, season, sort string, limit, offset i var seasonalAnime SeasonalAnime // error handling for limit and offset - limitsErr := e.LimitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset, maxAnimeLimit) if limitsErr != nil { return seasonalAnime, limitsErr } @@ -176,7 +179,7 @@ func (c AnimeClient) GetSeasonalAnime(year, season, sort string, limit, offset i } // checks if valid sort is specified - if !e.IsValidSort(sort) { + if !e.IsValidSeasonalSort(sort) { return seasonalAnime, errors.New(fmt.Sprintf("GetSeasonalAnime: Invalid sort specified: \"%s\"", sort)) } @@ -213,7 +216,7 @@ func (c AnimeClient) GetSuggestedAnime(limit, offset int, fields []string) (Sugg var suggestedAnime SuggestedAnime // error handling for limit and offset - limitsErr := e.LimitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset, maxAnimeLimit) if limitsErr != nil { return suggestedAnime, limitsErr } -- cgit v1.2.3