aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@protonmail.ch>2022-02-13 16:56:09 +0530
committerVidhu Kant Sharma <vidhukant@protonmail.ch>2022-02-13 16:56:09 +0530
commit419e08bc3a369a0c1138871184e1a30320032afd (patch)
treec32a78c0c6f16416f42975cac86fbfc23d1b1735
parent208cb52b47278315b5227251d5b65c07908cde23 (diff)
added functionality to update anime list of authenticated user
-rw-r--r--user/anime/animelist.go14
-rw-r--r--user/anime/animelist.structs.go13
-rw-r--r--user/anime/request_handler.go26
3 files changed, 51 insertions, 2 deletions
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)
+}