From a507674426a6bc149cdbc741cdb25f4ee66a56dd Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 14 Feb 2022 22:07:08 +0530 Subject: improved the UpdateAnime code --- user/anime/request_handler.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'user/anime/request_handler.go') 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 } -- cgit v1.2.3 From 74a5f1ce1594d01ef7caeb3c5fcac047a7d8f9b3 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 14 Feb 2022 23:02:38 +0530 Subject: fixed UpdateAnime not working, though incomplete and kinda dangerous right now --- user/anime/request_handler.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'user/anime/request_handler.go') 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) -- cgit v1.2.3 From 831a57e8d065a01cefe40dd8545770064759eb13 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 15 Feb 2022 21:55:56 +0530 Subject: somehow made UpdateAnime work decently --- user/anime/request_handler.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'user/anime/request_handler.go') diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go index 6f29d88..e44dab1 100644 --- a/user/anime/request_handler.go +++ b/user/anime/request_handler.go @@ -2,8 +2,7 @@ * Copyright (C) 2022 Vidhu Kant Sharma * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or + * it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, @@ -64,19 +63,31 @@ func (c AnimeListClient) requestHandler(endpoint, method string) string { // for PUT requests (used by UpdateAnime) 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)) + params := url.Values{} + + /* NOTE: THIS WILL OVERWRITE ANY DATA THAT + * IS NOT SPECIFIED AND SET IT TO NULL */ + params.Set("status", updateData.Status) + params.Set("is_rewatching", strconv.FormatBool(updateData.IsRewatching)) + params.Set("score", strconv.Itoa(updateData.Score)) + params.Set("num_watched_episodes", strconv.Itoa(updateData.EpWatched)) + params.Set("priority", strconv.Itoa(updateData.Priority)) + params.Set("num_times_rewatched", strconv.Itoa(updateData.TimesRewatched)) + params.Set("rewatch_value", strconv.Itoa(updateData.RewatchValue)) + params.Set("tags", updateData.Tags) + params.Set("comments", updateData.Comments) + + paramsEncoded := params.Encode() // generate request - req, err := http.NewRequest(http.MethodPut, endpoint, strings.NewReader(p.Encode())) + req, err := http.NewRequest(http.MethodPut, endpoint, strings.NewReader(paramsEncoded)) 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()))) + req.Header.Add("Content-Length", strconv.Itoa(len(paramsEncoded))) // do request res, err := c.HttpClient.Do(req) -- cgit v1.2.3 From 5c94f2f8e7026b9a4dff874edbf3b4fa3b266516 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Wed, 16 Feb 2022 22:43:23 +0530 Subject: Defined different functions to update different anime fields --- user/anime/request_handler.go | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) (limited to 'user/anime/request_handler.go') diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go index e44dab1..abc0afc 100644 --- a/user/anime/request_handler.go +++ b/user/anime/request_handler.go @@ -1,4 +1,4 @@ -/* mal2go - MyAnimeList V2 API wrapper for Go +/* MAL2Go - MyAnimeList V2 API wrapper for Go * Copyright (C) 2022 Vidhu Kant Sharma * This program is free software: you can redistribute it and/or modify @@ -16,13 +16,13 @@ package anime import ( - "strings" "encoding/json" - "net/url" "io/ioutil" "log" "net/http" + "net/url" "strconv" + "strings" ) type serverResponse struct { @@ -31,7 +31,7 @@ type serverResponse struct { } // Handles HTTP request with your OAuth token as a Header -func (c AnimeListClient) requestHandler(endpoint, method string) string { +func (c Client) requestHandler(endpoint, method string) string { // generate request req, err := http.NewRequest(method, endpoint, nil) if err != nil { @@ -60,23 +60,8 @@ func (c AnimeListClient) requestHandler(endpoint, method string) string { return string(body) } -// for PUT requests (used by UpdateAnime) -func (c AnimeListClient) putRequestHandler(endpoint string, updateData UpdateAnimeData) serverResponse { - // TODO: make this do other stuff - params := url.Values{} - - /* NOTE: THIS WILL OVERWRITE ANY DATA THAT - * IS NOT SPECIFIED AND SET IT TO NULL */ - params.Set("status", updateData.Status) - params.Set("is_rewatching", strconv.FormatBool(updateData.IsRewatching)) - params.Set("score", strconv.Itoa(updateData.Score)) - params.Set("num_watched_episodes", strconv.Itoa(updateData.EpWatched)) - params.Set("priority", strconv.Itoa(updateData.Priority)) - params.Set("num_times_rewatched", strconv.Itoa(updateData.TimesRewatched)) - params.Set("rewatch_value", strconv.Itoa(updateData.RewatchValue)) - params.Set("tags", updateData.Tags) - params.Set("comments", updateData.Comments) - +// for PUT requests (used for updating anime) +func (c Client) putRequestHandler(endpoint string, params url.Values) serverResponse { paramsEncoded := params.Encode() // generate request @@ -102,6 +87,7 @@ func (c AnimeListClient) putRequestHandler(endpoint string, updateData UpdateAni log.Fatal(err) } + // TODO: there are other serverResponses. Add them // server response, ie message / error var resp serverResponse json.Unmarshal(body, &resp) -- cgit v1.2.3