From 0d97d71e135cf4fda87acaea56a376c264f90a59 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 6 Feb 2022 18:56:45 +0530 Subject: converted all anime functions into methods for client struct --- anime/anime.go | 26 +++++++++++++------------- anime/client.go | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 anime/client.go diff --git a/anime/anime.go b/anime/anime.go index 3344016..617ed22 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -26,7 +26,7 @@ import ( const BASE_URL string = "https://api.myanimelist.net/v2/anime" // in MAL documentation this is named Get Anime List -func SearchAnime(token, searchString string, limit, offset int, fields []string) (AnimeSearch, error) { +func (c AnimeClient) SearchAnime(searchString string, limit, offset int, fields []string) (AnimeSearch, error) { var searchResults AnimeSearch // error handling for limit and offset @@ -51,14 +51,14 @@ func SearchAnime(token, searchString string, limit, offset int, fields []string) // gets data from API and stores it in a struct var animeSearchData AnimeSearchRaw - data := requestHandler(token, endpoint) + data := requestHandler(c.AuthToken, endpoint) json.Unmarshal([]byte(data), &animeSearchData) // Adding all the animes to another list to get formatted results later var animes []Anime - for _, element := range animeSearchData.Data { - animes = append(animes, element.Anime) - } + for _, element := range animeSearchData.Data { + animes = append(animes, element.Anime) + } // finally generate AnimeList searchResults = AnimeSearch { @@ -70,7 +70,7 @@ func SearchAnime(token, searchString string, limit, offset int, fields []string) } // Each anime has its own ID on MAL -func GetAnimeById(token string, animeId int, fields []string) (Anime, error) { +func (c AnimeClient) GetAnimeById(animeId int, fields []string) (Anime, error) { var anime Anime // handle all the errors for the fields @@ -89,14 +89,14 @@ func GetAnimeById(token string, animeId int, fields []string) (Anime, error) { true, ) - data := requestHandler(token, endpoint) + data := requestHandler(c.AuthToken, endpoint) json.Unmarshal([]byte(data), &anime) return anime, nil } // Ranking is a list of anime sorted by their rank -func GetAnimeRanking(token string, rankingType string, limit, offset int, fields []string) (AnimeRanking, error) { +func (c AnimeClient) GetAnimeRanking(rankingType string, limit, offset int, fields []string) (AnimeRanking, error) { var animeRanking AnimeRanking // error handling for limit and offset @@ -125,7 +125,7 @@ func GetAnimeRanking(token string, rankingType string, limit, offset int, fields // gets data from API and stores it in a struct var rankingData RawRanking - data := requestHandler(token, endpoint) + data := requestHandler(c.AuthToken, endpoint) json.Unmarshal([]byte(data), &rankingData) // Adding all the animes in ranking list to a slice @@ -153,7 +153,7 @@ func GetAnimeRanking(token string, rankingType string, limit, offset int, fields } // get list of animes from specified season -func GetSeasonalAnime(token, year, season, sort string, limit, offset int, fields []string) (SeasonalAnime, error) { +func (c AnimeClient) GetSeasonalAnime(year, season, sort string, limit, offset int, fields []string) (SeasonalAnime, error) { var seasonalAnime SeasonalAnime // error handling for limit and offset @@ -187,7 +187,7 @@ func GetSeasonalAnime(token, year, season, sort string, limit, offset int, field // gets data from API and stores it in a struct var seasonalAnimeData SeasonalAnimeRaw - data := requestHandler(token, endpoint) + data := requestHandler(c.AuthToken, endpoint) json.Unmarshal([]byte(data), &seasonalAnimeData) // Adding all the animes to another list to get formatted results later @@ -207,7 +207,7 @@ func GetSeasonalAnime(token, year, season, sort string, limit, offset int, field } // get anime suggestions for the user -func GetSuggestedAnime(token string, limit, offset int, fields []string) (SuggestedAnime, error){ +func (c AnimeClient) GetSuggestedAnime(limit, offset int, fields []string) (SuggestedAnime, error){ var suggestedAnime SuggestedAnime // error handling for limit and offset @@ -231,7 +231,7 @@ func GetSuggestedAnime(token string, limit, offset int, fields []string) (Sugges // gets data from API and stores it in a struct var suggestedAnimeData SuggestedAnimeRaw - data := requestHandler(token, endpoint) + data := requestHandler(c.AuthToken, endpoint) json.Unmarshal([]byte(data), &suggestedAnimeData) // Adding all the animes to another list to get formatted results later diff --git a/anime/client.go b/anime/client.go new file mode 100644 index 0000000..464854a --- /dev/null +++ b/anime/client.go @@ -0,0 +1,22 @@ +/* mal2go - MyAnimeList V2 API wrapper for Go + * Copyright (C) 2022 Vidhu Kant Sharma + + * 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 . */ + +package anime + +// MyAnimeList Client for mal2go/anime package +type AnimeClient struct { + AuthToken, RefreshToken string +} -- cgit v1.2.3