diff options
author | Vidhu Kant Sharma <vidhukant@protonmail.ch> | 2022-02-06 19:02:33 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@protonmail.ch> | 2022-02-06 19:02:33 +0530 |
commit | 0eb32bb339f66d3416c1088d6372bc7219b6e323 (patch) | |
tree | bc5b2a5f60748f60d49f97d9eb934891369fb60e | |
parent | 0d97d71e135cf4fda87acaea56a376c264f90a59 (diff) |
now a single http.Client is responsible for all the HTTP requests.
-rw-r--r-- | anime/anime.go | 10 | ||||
-rw-r--r-- | anime/client.go | 5 | ||||
-rw-r--r-- | anime/util.go | 8 |
3 files changed, 13 insertions, 10 deletions
diff --git a/anime/anime.go b/anime/anime.go index 617ed22..90231e3 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -51,7 +51,7 @@ func (c AnimeClient) SearchAnime(searchString string, limit, offset int, fields // gets data from API and stores it in a struct var animeSearchData AnimeSearchRaw - data := requestHandler(c.AuthToken, endpoint) + data := c.requestHandler(endpoint) json.Unmarshal([]byte(data), &animeSearchData) // Adding all the animes to another list to get formatted results later @@ -89,7 +89,7 @@ func (c AnimeClient) GetAnimeById(animeId int, fields []string) (Anime, error) { true, ) - data := requestHandler(c.AuthToken, endpoint) + data := c.requestHandler(endpoint) json.Unmarshal([]byte(data), &anime) return anime, nil @@ -125,7 +125,7 @@ func (c AnimeClient) GetAnimeRanking(rankingType string, limit, offset int, fiel // gets data from API and stores it in a struct var rankingData RawRanking - data := requestHandler(c.AuthToken, endpoint) + data := c.requestHandler(endpoint) json.Unmarshal([]byte(data), &rankingData) // Adding all the animes in ranking list to a slice @@ -187,7 +187,7 @@ func (c AnimeClient) GetSeasonalAnime(year, season, sort string, limit, offset i // gets data from API and stores it in a struct var seasonalAnimeData SeasonalAnimeRaw - data := requestHandler(c.AuthToken, endpoint) + data := c.requestHandler(endpoint) json.Unmarshal([]byte(data), &seasonalAnimeData) // Adding all the animes to another list to get formatted results later @@ -231,7 +231,7 @@ func (c AnimeClient) GetSuggestedAnime(limit, offset int, fields []string) (Sugg // gets data from API and stores it in a struct var suggestedAnimeData SuggestedAnimeRaw - data := requestHandler(c.AuthToken, endpoint) + data := c.requestHandler(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 index 464854a..25c71e1 100644 --- a/anime/client.go +++ b/anime/client.go @@ -16,7 +16,12 @@ package anime +import ( + "net/http" +) + // MyAnimeList Client for mal2go/anime package type AnimeClient struct { AuthToken, RefreshToken string + HttpClient http.Client } diff --git a/anime/util.go b/anime/util.go index f1f3b2a..862b54d 100644 --- a/anime/util.go +++ b/anime/util.go @@ -25,18 +25,16 @@ import ( // Handles HTTP request with your OAuth token as a Header // TODO: Verify that this function is safe to use -func requestHandler(token string, endpoint string) string { - client := &http.Client{} - +func (c AnimeClient) requestHandler(endpoint string) string { // generate request req, err := http.NewRequest("GET", endpoint, nil) if err != nil { log.Fatal(err) } - req.Header.Add("Authorization", token) + req.Header.Add("Authorization", c.AuthToken) // do request - res, err := client.Do(req) + res, err := c.HttpClient.Do(req) if err != nil { log.Fatal(err) } |