aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@protonmail.ch>2022-02-14 23:02:38 +0530
committerVidhu Kant Sharma <vidhukant@protonmail.ch>2022-02-14 23:02:38 +0530
commit74a5f1ce1594d01ef7caeb3c5fcac047a7d8f9b3 (patch)
tree24123f6e0dc0d7663ea8e4ddf9de366cd1874f79
parenta5a96fe4673048d49f8f14665e4065e199b1179b (diff)
fixed UpdateAnime not working, though incomplete and kinda dangerous right now
-rw-r--r--user/anime/animelist.go15
-rw-r--r--user/anime/request_handler.go15
2 files changed, 14 insertions, 16 deletions
diff --git a/user/anime/animelist.go b/user/anime/animelist.go
index 8a673c1..78242ac 100644
--- a/user/anime/animelist.go
+++ b/user/anime/animelist.go
@@ -20,7 +20,6 @@ import (
"encoding/json"
"strconv"
"fmt"
- "log"
"errors"
a "github.com/MikunoNaka/mal2go/anime"
e "github.com/MikunoNaka/mal2go/errhandlers"
@@ -39,20 +38,10 @@ func (c AnimeListClient)DeleteAnime(id int) string {
// Update/Add an anime to user's anime list
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
- jsonData, err := json.Marshal(data)
- if err != nil {
- log.Println(err)
- }
-
- // finally make API request
- res := c.putRequestHandler(endpoint, jsonData)
+ // make API request
+ res := c.putRequestHandler(endpoint, data)
return res
}
diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go
index 3de8e4a..6f29d88 100644
--- a/user/anime/request_handler.go
+++ b/user/anime/request_handler.go
@@ -17,8 +17,9 @@
package anime
import (
- "bytes"
+ "strings"
"encoding/json"
+ "net/url"
"io/ioutil"
"log"
"net/http"
@@ -61,13 +62,21 @@ func (c AnimeListClient) requestHandler(endpoint, method string) string {
}
// for PUT requests (used by UpdateAnime)
-func (c AnimeListClient) putRequestHandler(endpoint string, data []uint8) serverResponse {
+func (c AnimeListClient) putRequestHandler(endpoint string, updateData UpdateAnimeData) serverResponse {
+ // TODO: make this do other stuff
+ p := url.Values{}
+ p.Set("score", strconv.Itoa(updateData.Score))
+ p.Set("num_watched_episodes", strconv.Itoa(updateData.EpWatched))
+
// generate request
- req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(data))
+ req, err := http.NewRequest(http.MethodPut, endpoint, strings.NewReader(p.Encode()))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Authorization", c.AuthToken)
+ // this makes the sending-data-to-server magic work
+ req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
+ req.Header.Add("Content-Length", strconv.Itoa(len(p.Encode())))
// do request
res, err := c.HttpClient.Do(req)