diff options
author | Vidhu Kant Sharma <vidhukant@protonmail.ch> | 2022-02-14 22:07:08 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@protonmail.ch> | 2022-02-14 22:07:08 +0530 |
commit | a507674426a6bc149cdbc741cdb25f4ee66a56dd (patch) | |
tree | c15a06524964f1864188bbd549e3095e4275a672 | |
parent | 419e08bc3a369a0c1138871184e1a30320032afd (diff) |
improved the UpdateAnime code
-rw-r--r-- | user/anime/animelist.go | 13 | ||||
-rw-r--r-- | user/anime/request_handler.go | 16 |
2 files changed, 22 insertions, 7 deletions
diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 4f5eac5..8a673c1 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -20,6 +20,7 @@ import ( "encoding/json" "strconv" "fmt" + "log" "errors" a "github.com/MikunoNaka/mal2go/anime" e "github.com/MikunoNaka/mal2go/errhandlers" @@ -37,17 +38,21 @@ func (c AnimeListClient)DeleteAnime(id int) string { } // Update/Add an anime to user's anime list -func (c AnimeListClient)UpdateAnime(id int, data UpdateAnimeData) string { +func (c AnimeListClient)UpdateAnime(id int, data UpdateAnimeData) serverResponse { + /* NOTE: UpdateAnime only adds anime to the list + * It doesn't add new anime to the list. + * This might be a problem with MAL, + * I haven't researched this much */ endpoint := fmt.Sprintf("%s/anime/%d/my_list_status", BASE_URL, id) // turn data struct into json - pepe, err := json.Marshal(data) + jsonData, err := json.Marshal(data) if err != nil { - fmt.Println(err) + log.Println(err) } // finally make API request - res := c.putRequestHandler(endpoint, pepe) + res := c.putRequestHandler(endpoint, jsonData) return res } diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go index 22c6e0b..3de8e4a 100644 --- a/user/anime/request_handler.go +++ b/user/anime/request_handler.go @@ -17,13 +17,19 @@ package anime import ( + "bytes" + "encoding/json" "io/ioutil" "log" "net/http" "strconv" - "bytes" ) +type serverResponse struct { + Message string + Error string +} + // Handles HTTP request with your OAuth token as a Header func (c AnimeListClient) requestHandler(endpoint, method string) string { // generate request @@ -55,7 +61,7 @@ func (c AnimeListClient) requestHandler(endpoint, method string) string { } // for PUT requests (used by UpdateAnime) -func (c AnimeListClient) putRequestHandler(endpoint string, data []uint8) string { +func (c AnimeListClient) putRequestHandler(endpoint string, data []uint8) serverResponse { // generate request req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(data)) if err != nil { @@ -76,5 +82,9 @@ func (c AnimeListClient) putRequestHandler(endpoint string, data []uint8) string log.Fatal(err) } - return string(body) + // server response, ie message / error + var resp serverResponse + json.Unmarshal(body, &resp) + + return resp } |