diff options
author | Vidhu Kant Sharma <vidhukant@protonmail.ch> | 2022-02-13 13:42:06 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@protonmail.ch> | 2022-02-13 13:42:06 +0530 |
commit | 1d3f72c1b48998b86fd1740e893559b6dcaf7663 (patch) | |
tree | b6692c8aa5dbe237408efed07d9a7ade8450080c /anime | |
parent | 4bd702d111c6e4d5455865a7e1fbe5de11899b15 (diff) |
modularised the code for easy access of various functions in packages
Diffstat (limited to 'anime')
-rw-r--r-- | anime/anime.go | 36 | ||||
-rw-r--r-- | anime/errhandlers.go | 52 | ||||
-rw-r--r-- | anime/general.structs.go | 19 | ||||
-rw-r--r-- | anime/request_handler.go (renamed from anime/util.go) | 38 | ||||
-rw-r--r-- | anime/validators.go | 96 |
5 files changed, 19 insertions, 222 deletions
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}, diff --git a/anime/errhandlers.go b/anime/errhandlers.go deleted file mode 100644 index 3a60f7c..0000000 --- a/anime/errhandlers.go +++ /dev/null @@ -1,52 +0,0 @@ -/* mal2go - MyAnimeList V2 API wrapper for Go - * Copyright (C) 2022 Vidhu Kant Sharma <vidhukant@protonmail.ch> - - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. */ - -package anime - -import ( - "errors" - "fmt" -) - -// if fields aren't specified -func fieldsErrHandler(fields []string) ([]string, error) { - if cap(fields) == 0 { - // uses all the default fields if none specified - return DefaultFields, nil - } - - // checks if each given field is valid - for _, j := range(fields) { - if !isValidField(j) { - return []string{}, errors.New(fmt.Sprintf("InvalidFieldError: Invalid field specified: \"%s\"", j)) - } - } - - // everything's fine! - return fields, nil -} - -// if limit or error specified are above the limit -func limitsErrHandler(limit, offset int) error { - maxOffset := 500 - limit - if limit > 500 { - return errors.New(fmt.Sprintf("InvalidLimitError: Limit specified too high (%d > 500).", limit)) - } else if offset > maxOffset { - return errors.New(fmt.Sprintf("InvalidOffsetError: Offset specified too high (%d > %d).", offset, maxOffset)) - } - // return nil if no error - return nil -} diff --git a/anime/general.structs.go b/anime/general.structs.go index 77bd22a..5df1357 100644 --- a/anime/general.structs.go +++ b/anime/general.structs.go @@ -16,25 +16,6 @@ package anime -/* NOTE: MAL still seems to send some fields - * even if they aren't requested. - * those include Title, Picture, Id, etc */ -// default fields to use when none are specified -var DefaultFields []string = []string{ - "id", "title", "main_picture", - "alternative_titles", "start_date", - "end_date", "synopsis", "mean", "rank", - "popularity", "num_list_users", - "num_scoring_users", "nsfw", "created_at", - "updated_at", "media_type", "status", - "genres", "my_list_status", "num_episodes", - "start_season", "broadcast", "source", - "average_episode_duration", "rating", - "pictures", "background", "related_anime", - "related_manga", "recommendations", - "studios", "statistics", -} - // contains previous/next page for anime list type ListPaging struct { NextPage string `json:"next"` diff --git a/anime/util.go b/anime/request_handler.go index 862b54d..809ca07 100644 --- a/anime/util.go +++ b/anime/request_handler.go @@ -20,7 +20,6 @@ import ( "io/ioutil" "log" "net/http" - "errors" ) // Handles HTTP request with your OAuth token as a Header @@ -48,40 +47,3 @@ func (c AnimeClient) requestHandler(endpoint string) string { return string(body) } - -func urlGenerator(baseUrl string, names []string, values [][]string, isPrimary bool) (string, error) { - // length of names and values should be same - if cap(names) != cap(values) { - return "", errors.New("urlGenerator: Error: Length of names and values don't match.") - } - - var fields string - - for index, name := range(names) { - var data string - /* if the data is the first field in URL, - * it goes like ?key=value - * else it is &nextkey=value */ - if isPrimary { - data = "?" + name + "=" - } else { - data = "&" + name + "=" - } - - // add values to data variable - for i, j := range values[index] { - if i > 0 { - data = data + "," + j - } else { - data = data + j - } - } - - fields = fields + data - - // from now on all other fields will be secondary - isPrimary = false - } - - return baseUrl + fields, nil -} diff --git a/anime/validators.go b/anime/validators.go deleted file mode 100644 index 7f6a7cc..0000000 --- a/anime/validators.go +++ /dev/null @@ -1,96 +0,0 @@ -/* mal2go - MyAnimeList V2 API wrapper for Go - * Copyright (C) 2022 Vidhu Kant Sharma <vidhukant@protonmail.ch> - - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. */ - -package anime - -// Checks if given rankingType is valid -func isValidRankingType(rankingType string) bool { - switch rankingType { - case - "all", - "airing", - "upcoming", - "tv", - "ova", - "movie", - "special", - "bypopularity", - "favorite": return true - } - return false -} - -// Checks if given rankingType is valid -func isValidField(field string) bool { - switch field { - case - "id", - "title", - "main_picture", - "alternative_titles", - "start_date", - "end_date", - "synopsis", - "mean", - "rank", - "popularity", - "num_list_users", - "num_scoring_users", - "nsfw", - "created_at", - "updated_at", - "media_type", - "status", - "genres", - "my_list_status", - "num_episodes", - "start_season", - "broadcast", - "source", - "average_episode_duration", - "rating", - "pictures", - "background", - "related_anime", - "related_manga", - "recommendations", - "studios", - "statistics": return true - } - return false -} - -// Checks if given season is valid -func isValidSeason(season string) bool { - switch season { - case - "winter", - "spring", - "summer", - "fall": return true - } - return false -} - -// Checks if given sort is valid -func isValidSort(sort string) bool { - switch sort { - case - "anime_score", - "anime_num_list_users": return true - } - return false -} |