From 7816a8487dd5d79111ca84b0da227faf5f8d7bad Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 6 Feb 2022 19:27:02 +0530 Subject: implemented basic functionality to get user's animelist in string form --- user/anime/animelist.go | 32 +++++++++++++++++++ user/anime/client.go | 27 ++++++++++++++++ user/anime/util.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 user/anime/animelist.go create mode 100644 user/anime/client.go create mode 100644 user/anime/util.go (limited to 'user') diff --git a/user/anime/animelist.go b/user/anime/animelist.go new file mode 100644 index 0000000..c48980d --- /dev/null +++ b/user/anime/animelist.go @@ -0,0 +1,32 @@ +/* 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 + * 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, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +package anime + +import ( + "fmt" +) + +const BASE_URL string = "https://api.myanimelist.net/v2" + +// Get authenticated user's anime list +func (c AnimeListClient) GetAnimeList() { + endpoint := BASE_URL + "/users/@me/animelist?fields=list_status&limit=4" + + data := c.requestHandler(endpoint, "GET") + fmt.Println(data) +} + diff --git a/user/anime/client.go b/user/anime/client.go new file mode 100644 index 0000000..e9a99d3 --- /dev/null +++ b/user/anime/client.go @@ -0,0 +1,27 @@ +/* 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 + * 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, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +package anime + +import ( + "net/http" +) + +// MyAnimeList Client for mal2go/anime package +type AnimeListClient struct { + AuthToken, RefreshToken string + HttpClient http.Client +} diff --git a/user/anime/util.go b/user/anime/util.go new file mode 100644 index 0000000..d9a4a4c --- /dev/null +++ b/user/anime/util.go @@ -0,0 +1,85 @@ +/* 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 + * 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, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +package anime + +import ( + "io/ioutil" + "log" + "net/http" +) + +// Handles HTTP request with your OAuth token as a Header +func (c AnimeListClient) requestHandler(endpoint, method string) string { + // generate request + req, err := http.NewRequest(method, endpoint, nil) + 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) +} + +// func urlGenerator(baseUrl string, names []string, values [][]string, isPrimary bool) (string, error) { +// // length of names and values should be same +// if cap(names) != cap(values) { +// return "", errors.New("urlGenerator: Error: Length of names and values don't match.") +// } +// +// var fields string +// +// for index, name := range(names) { +// var data string +// /* if the data is the first field in URL, +// * it goes like ?key=value +// * else it is &nextkey=value */ +// if isPrimary { +// data = "?" + name + "=" +// } else { +// data = "&" + name + "=" +// } +// +// // add values to data variable +// for i, j := range values[index] { +// if i > 0 { +// data = data + "," + j +// } else { +// data = data + j +// } +// } +// +// fields = fields + data +// +// // from now on all other fields will be secondary +// isPrimary = false +// } +// +// return baseUrl + fields, nil +// } -- cgit v1.2.3 From 4bd702d111c6e4d5455865a7e1fbe5de11899b15 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 8 Feb 2022 21:47:58 +0530 Subject: NOT WORKING: pushing current progress, adding support for different options with GetAnimeList --- user/anime/animelist.go | 35 +++++++++++++++++++++++++++++++---- user/anime/animelist.structs.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 user/anime/animelist.structs.go (limited to 'user') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index c48980d..b36b50d 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -17,16 +17,43 @@ package anime import ( - "fmt" + "encoding/json" + "fmt" + "github.com/MikunoNaka/mal2go/anime" ) const BASE_URL string = "https://api.myanimelist.net/v2" // Get authenticated user's anime list -func (c AnimeListClient) GetAnimeList() { - endpoint := BASE_URL + "/users/@me/animelist?fields=list_status&limit=4" +func (c AnimeListClient) GetAnimeList(user, status, sort string/*, limit, offset int*/) { + // get own list if user not specified + if user == "" { + user = "@me" + } + var userAnimeList anime.AnimeList + endpoint := BASE_URL + "/users/0ZeroTsu/animelist?fields=list_status&limit=4" + + // get data from API + var animeListData AnimeListRaw data := c.requestHandler(endpoint, "GET") - fmt.Println(data) + json.Unmarshal([]byte(data), &animeListData) + + // set MyListStatus for each element and add it to array + var animes []anime.Anime + for _, element := range animeListData.Data { + a := element.Anime + a.MyListStatus = element.ListStatus + + animes = append(animes, a) + } + + // finally create AnimeList + userAnimeList = anime.AnimeList { + Animes: animes, + Paging: animeListData.Paging, + } + + fmt.Println(userAnimeList) } diff --git a/user/anime/animelist.structs.go b/user/anime/animelist.structs.go new file mode 100644 index 0000000..d43b47b --- /dev/null +++ b/user/anime/animelist.structs.go @@ -0,0 +1,29 @@ +/* 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 + * 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, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +package anime + +import ( + "github.com/MikunoNaka/mal2go/anime" +) + +type AnimeListRaw struct { + Data []struct { + Anime anime.Anime `json:"node"` + ListStatus anime.ListStatus `json:"list_status"` + } `json:"data"` + Paging anime.ListPaging `json:"paging"` +} -- cgit v1.2.3 From 1d3f72c1b48998b86fd1740e893559b6dcaf7663 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 13:42:06 +0530 Subject: modularised the code for easy access of various functions in packages --- user/anime/animelist.go | 19 ++++++++-- user/anime/request_handler.go | 48 ++++++++++++++++++++++++ user/anime/util.go | 85 ------------------------------------------- 3 files changed, 64 insertions(+), 88 deletions(-) create mode 100644 user/anime/request_handler.go delete mode 100644 user/anime/util.go (limited to 'user') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index b36b50d..e4cddc3 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -20,18 +20,31 @@ import ( "encoding/json" "fmt" "github.com/MikunoNaka/mal2go/anime" + e "github.com/MikunoNaka/mal2go/errhandlers" ) const BASE_URL string = "https://api.myanimelist.net/v2" // Get authenticated user's anime list -func (c AnimeListClient) GetAnimeList(user, status, sort string/*, limit, offset int*/) { +func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int, fields []string) (anime.AnimeList, error){ + var userAnimeList anime.AnimeList + // error handling for limit and offset + limitsErr := e.LimitsErrHandler(limit, offset) + if limitsErr != nil { + return userAnimeList, limitsErr + } + + // handle all the errors for the fields + fields, err := e.FieldsErrHandler(fields) + if err != nil { + return userAnimeList, err + } + // get own list if user not specified if user == "" { user = "@me" } - var userAnimeList anime.AnimeList endpoint := BASE_URL + "/users/0ZeroTsu/animelist?fields=list_status&limit=4" // get data from API @@ -54,6 +67,6 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string/*, limit, offset Paging: animeListData.Paging, } - fmt.Println(userAnimeList) + return userAnimeList, nil } diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go new file mode 100644 index 0000000..f424c29 --- /dev/null +++ b/user/anime/request_handler.go @@ -0,0 +1,48 @@ +/* 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 + * 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, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +package anime + +import ( + "io/ioutil" + "log" + "net/http" +) + +// Handles HTTP request with your OAuth token as a Header +func (c AnimeListClient) requestHandler(endpoint, method string) string { + // generate request + req, err := http.NewRequest(method, endpoint, nil) + 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) +} diff --git a/user/anime/util.go b/user/anime/util.go deleted file mode 100644 index d9a4a4c..0000000 --- a/user/anime/util.go +++ /dev/null @@ -1,85 +0,0 @@ -/* 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 - * 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, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . */ - -package anime - -import ( - "io/ioutil" - "log" - "net/http" -) - -// Handles HTTP request with your OAuth token as a Header -func (c AnimeListClient) requestHandler(endpoint, method string) string { - // generate request - req, err := http.NewRequest(method, endpoint, nil) - 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) -} - -// func urlGenerator(baseUrl string, names []string, values [][]string, isPrimary bool) (string, error) { -// // length of names and values should be same -// if cap(names) != cap(values) { -// return "", errors.New("urlGenerator: Error: Length of names and values don't match.") -// } -// -// var fields string -// -// for index, name := range(names) { -// var data string -// /* if the data is the first field in URL, -// * it goes like ?key=value -// * else it is &nextkey=value */ -// if isPrimary { -// data = "?" + name + "=" -// } else { -// data = "&" + name + "=" -// } -// -// // add values to data variable -// for i, j := range values[index] { -// if i > 0 { -// data = data + "," + j -// } else { -// data = data + j -// } -// } -// -// fields = fields + data -// -// // from now on all other fields will be secondary -// isPrimary = false -// } -// -// return baseUrl + fields, nil -// } -- cgit v1.2.3 From 937f3b8ada85274dfe3842f3dde8aef45c4f3ae7 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 14:12:45 +0530 Subject: completed (?) the GetAnimeList function --- user/anime/animelist.go | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'user') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index e4cddc3..f25a2a2 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -18,18 +18,22 @@ package anime import ( "encoding/json" - "fmt" - "github.com/MikunoNaka/mal2go/anime" + "strconv" + "fmt" + "errors" + a "github.com/MikunoNaka/mal2go/anime" e "github.com/MikunoNaka/mal2go/errhandlers" + u "github.com/MikunoNaka/mal2go/util" ) const BASE_URL string = "https://api.myanimelist.net/v2" +const maxListLimit int = 1000 // Get authenticated user's anime list -func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int, fields []string) (anime.AnimeList, error){ - var userAnimeList anime.AnimeList +func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int, fields []string) (a.AnimeList, error){ + var userAnimeList a.AnimeList // error handling for limit and offset - limitsErr := e.LimitsErrHandler(limit, offset) + limitsErr := e.LimitsErrHandler(limit, offset, maxListLimit) if limitsErr != nil { return userAnimeList, limitsErr } @@ -40,12 +44,27 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i return userAnimeList, err } + // checks if valid sort is specified + if !e.IsValidListSort(sort) { + return userAnimeList, errors.New(fmt.Sprintf("GetAnimeList: Invalid sort specified: \"%s\"", sort)) + } + + // checks if valid status is specified + if !e.IsValidListStatus(status) { + return userAnimeList, errors.New(fmt.Sprintf("GetAnimeList: Invalid status specified: \"%s\"", status)) + } + // get own list if user not specified if user == "" { user = "@me" } - endpoint := BASE_URL + "/users/0ZeroTsu/animelist?fields=list_status&limit=4" + endpoint, _ := u.UrlGenerator( + BASE_URL + "/users/" + user + "/animelist", + []string{"status", "sort", "limit", "offset", "fields"}, + [][]string{{status}, {sort}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, + true, + ) // get data from API var animeListData AnimeListRaw @@ -53,7 +72,7 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i json.Unmarshal([]byte(data), &animeListData) // set MyListStatus for each element and add it to array - var animes []anime.Anime + var animes []a.Anime for _, element := range animeListData.Data { a := element.Anime a.MyListStatus = element.ListStatus @@ -62,7 +81,7 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i } // finally create AnimeList - userAnimeList = anime.AnimeList { + userAnimeList = a.AnimeList { Animes: animes, Paging: animeListData.Paging, } -- cgit v1.2.3 From 7dcda8a7344ec5be2b93672ace638515708554de Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 15:30:32 +0530 Subject: removed fields from GetAnimeList because endpoint does not support that --- user/anime/animelist.go | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'user') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index f25a2a2..2099204 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -23,14 +23,13 @@ import ( "errors" a "github.com/MikunoNaka/mal2go/anime" e "github.com/MikunoNaka/mal2go/errhandlers" - u "github.com/MikunoNaka/mal2go/util" ) const BASE_URL string = "https://api.myanimelist.net/v2" const maxListLimit int = 1000 // Get authenticated user's anime list -func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int, fields []string) (a.AnimeList, error){ +func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ var userAnimeList a.AnimeList // error handling for limit and offset limitsErr := e.LimitsErrHandler(limit, offset, maxListLimit) @@ -38,12 +37,6 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i return userAnimeList, limitsErr } - // handle all the errors for the fields - fields, err := e.FieldsErrHandler(fields) - if err != nil { - return userAnimeList, err - } - // checks if valid sort is specified if !e.IsValidListSort(sort) { return userAnimeList, errors.New(fmt.Sprintf("GetAnimeList: Invalid sort specified: \"%s\"", sort)) @@ -59,12 +52,7 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i user = "@me" } - endpoint, _ := u.UrlGenerator( - BASE_URL + "/users/" + user + "/animelist", - []string{"status", "sort", "limit", "offset", "fields"}, - [][]string{{status}, {sort}, {strconv.Itoa(limit)}, {strconv.Itoa(offset)}, fields}, - true, - ) + endpoint := BASE_URL + "/users/" + user + "/animelist?status=" + status + "&sort=" + sort + "&limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset) // get data from API var animeListData AnimeListRaw -- cgit v1.2.3 From 032469fc1c43fb76fdf8882371391672dcfb87f0 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 16:05:15 +0530 Subject: added function to delete anime from MAL List --- user/anime/animelist.go | 8 ++++++++ user/anime/request_handler.go | 6 ++++++ 2 files changed, 14 insertions(+) (limited to 'user') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 2099204..7fe81b8 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -28,6 +28,14 @@ import ( const BASE_URL string = "https://api.myanimelist.net/v2" const maxListLimit int = 1000 +// Delete an anime from user's anime list +func (c AnimeListClient)DeleteAnime(id int) string { + endpoint := fmt.Sprintf("%s/anime/%d/my_list_status", BASE_URL, id) + /* Returns 200 if anime successfully deleted + * Alternatively returns 404 if anime not in user's anime list */ + return c.requestHandler(endpoint, "DELETE") +} + // Get authenticated user's anime list func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ var userAnimeList a.AnimeList diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go index f424c29..2007e56 100644 --- a/user/anime/request_handler.go +++ b/user/anime/request_handler.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "log" "net/http" + "strconv" ) // Handles HTTP request with your OAuth token as a Header @@ -44,5 +45,10 @@ func (c AnimeListClient) requestHandler(endpoint, method string) string { log.Fatal(err) } + // for DeleteAnime, its endpoint returns null data + if method == "DELETE" { + return strconv.Itoa(res.StatusCode) + } + return string(body) } -- cgit v1.2.3 From 208cb52b47278315b5227251d5b65c07908cde23 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 13 Feb 2022 16:15:47 +0530 Subject: added support to get all anime from user's anime list --- user/anime/animelist.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'user') diff --git a/user/anime/animelist.go b/user/anime/animelist.go index 7fe81b8..120bdea 100644 --- a/user/anime/animelist.go +++ b/user/anime/animelist.go @@ -36,6 +36,11 @@ func (c AnimeListClient)DeleteAnime(id int) string { return c.requestHandler(endpoint, "DELETE") } +// Update/Add an anime to user's anime list +// func UpdateAnime(id int) { + +// } + // Get authenticated user's anime list func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset int) (a.AnimeList, error){ var userAnimeList a.AnimeList @@ -51,7 +56,7 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i } // checks if valid status is specified - if !e.IsValidListStatus(status) { + if status != "" && !e.IsValidListStatus(status) { return userAnimeList, errors.New(fmt.Sprintf("GetAnimeList: Invalid status specified: \"%s\"", status)) } @@ -60,7 +65,13 @@ func (c AnimeListClient) GetAnimeList(user, status, sort string, limit, offset i user = "@me" } - endpoint := BASE_URL + "/users/" + user + "/animelist?status=" + status + "&sort=" + sort + "&limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset) + // if status is "" it returns all anime + var endpoint string + if status == "" { + endpoint = BASE_URL + "/users/" + user + "/animelist?sort=" + sort + "&limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset) + } else { + endpoint = BASE_URL + "/users/" + user + "/animelist?status=" + status + "&sort=" + sort + "&limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset) + } // get data from API var animeListData AnimeListRaw -- cgit v1.2.3 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') 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