From 419e08bc3a369a0c1138871184e1a30320032afd Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 16:56:09 +0530 Subject: added functionality to update anime list of authenticated user --- user/anime/animelist.go | 14 ++++++++++++-- user/anime/animelist.structs.go | 13 +++++++++++++ user/anime/request_handler.go | 26 ++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) (limited to 'user/anime') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 120bdea..4f5eac5 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -37,9 +37,19 @@ func (c AnimeListClient)DeleteAnime(id int) string { } // Update/Add an anime to user's anime list -// func UpdateAnime(id int) { +func (c AnimeListClient)UpdateAnime(id int, data UpdateAnimeData) string { + endpoint := fmt.Sprintf("%s/anime/%d/my_list_status", BASE_URL, id) + + // turn data struct into json + pepe, err := json.Marshal(data) + if err != nil { + fmt.Println(err) + } -// } + // finally make API request + res := c.putRequestHandler(endpoint, pepe) + return res +} // Get authenticated user's anime list func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ diff --git a/user/anime/animelist.structs.go b/user/anime/animelist.structs.go index d43b47b..d874f7b 100644 --- a/user/anime/animelist.structs.go +++ b/user/anime/animelist.structs.go @@ -27,3 +27,16 @@ type AnimeListRaw struct { } `json:"data"` Paging anime.ListPaging `json:"paging"` } + +type UpdateAnimeData struct { + Status string `json:"status"` + IsRewatching bool `json:"is_rewatching"` + Score int `json:"score"` + EpWatched int `json:"num_watched_episodes"` + Priority int `json:"priority"` + TimesRewatched int `json:"num_times_rewatched"` + // NOTE: idk what RewatchValue is + RewatchValue int `json:"rewatch_value"` + Tags string `json:"tags"` + Comments string `json:"comments"` +} diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go index 2007e56..22c6e0b 100644 --- a/user/anime/request_handler.go +++ b/user/anime/request_handler.go @@ -21,6 +21,7 @@ import ( "log" "net/http" "strconv" + "bytes" ) // Handles HTTP request with your OAuth token as a Header @@ -52,3 +53,28 @@ func (c AnimeListClient) requestHandler(endpoint, method string) string { return string(body) } + +// for PUT requests (used by UpdateAnime) +func (c AnimeListClient) putRequestHandler(endpoint string, data []uint8) string { + // generate request + req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(data)) + if err != nil { + log.Fatal(err) + } + req.Header.Add("Authorization", c.AuthToken) + + // do request + res, err := c.HttpClient.Do(req) + if err != nil { + log.Fatal(err) + } + defer res.Body.Close() + + // read body + body, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Fatal(err) + } + + return string(body) +} -- cgit v1.2.3