aboutsummaryrefslogtreecommitdiff
path: root/user/anime
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-06-16 22:20:17 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-06-16 22:20:17 +0530
commit05fe09f3ec167417ea94fc04148ac110f18c4ad6 (patch)
tree19d7c2713c8e24772ccbacc1572c354d48707dc9 /user/anime
parent71210ebc8e04d49a6afeeecee842b2e8b53f3c4f (diff)
added api error handling to user/anime and user/manga packages
Diffstat (limited to 'user/anime')
-rw-r--r--user/anime/animelist.go42
-rw-r--r--user/anime/animelist.structs.go10
-rw-r--r--user/anime/request_handler.go60
-rw-r--r--user/anime/update_animelist.go60
4 files changed, 98 insertions, 74 deletions
diff --git a/user/anime/animelist.go b/user/anime/animelist.go
index 2840df3..321e6d0 100644
--- a/user/anime/animelist.go
+++ b/user/anime/animelist.go
@@ -17,7 +17,7 @@
package anime
import (
- "encoding/json"
+ "encoding/json"
"strconv"
"fmt"
a "github.com/MikunoNaka/MAL2Go/anime"
@@ -29,7 +29,7 @@ const BASE_URL string = "https://api.myanimelist.net/v2"
const maxListLimit int = 1000
// Delete an anime from user's anime list
-func (c Client)DeleteAnime(id int) string {
+func (c Client)DeleteAnime(id int) (string, error) {
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 */
@@ -37,18 +37,19 @@ func (c Client)DeleteAnime(id int) string {
}
// Get authenticated user's anime list
-func (c Client) GetAnimeList(user, status, sort string, limit, offset int, fields []string) (a.AnimeList, error){
- var userAnimeList a.AnimeList
+// returns true as second value if there are more animes present
+func (c Client) GetAnimeList(user, status, sort string, limit, offset int, fields []string) ([]a.Anime, bool, error){
+ var userAnimeList []a.Anime
// error handling for limit
limitErr := e.LimitErrHandler(limit, maxListLimit)
if limitErr != nil {
- return userAnimeList, limitErr
+ return userAnimeList, false, limitErr
}
// handle all the errors for the fields
fields, err := e.FieldsErrHandler(fields)
if err != nil {
- return userAnimeList, err
+ return userAnimeList, false, err
}
// append "list_status" field only used by this func.
@@ -56,12 +57,12 @@ func (c Client) GetAnimeList(user, status, sort string, limit, offset int, field
// checks if valid sort is specified
if !e.IsValidListSort(sort) {
- return userAnimeList, e.InvalidSortError
+ return userAnimeList, false, e.InvalidSortError
}
// checks if valid status is specified
if status != "" && !e.IsValidListStatus(status) {
- return userAnimeList, e.InvalidStatusError
+ return userAnimeList, false, e.InvalidStatusError
}
// get own list if user not specified
@@ -88,27 +89,24 @@ func (c Client) GetAnimeList(user, status, sort string, limit, offset int, field
)
}
-
// get data from API
- var animeListData AnimeListRaw
- data := c.requestHandler(endpoint, "GET")
+ var animeListData animeListRaw
+ data, err := c.requestHandler(endpoint, "GET")
+ if err != nil {
+ return userAnimeList, false, err
+ }
json.Unmarshal([]byte(data), &animeListData)
+ nextPageExists := animeListData.Paging.NextPage != ""
+
// set ListStatus for each element and add it to array
- var animes []a.Anime
for _, element := range animeListData.Data {
- anime := element.Anime
- anime.ListStatus = element.ListStatus
-
- animes = append(animes, anime)
- }
+ a := element.Anime
+ a.ListStatus = element.ListStatus
- // finally create AnimeList
- userAnimeList = a.AnimeList {
- Animes: animes,
- Paging: animeListData.Paging,
+ userAnimeList = append(userAnimeList, a)
}
- return userAnimeList, nil
+ return userAnimeList, nextPageExists, nil
}
diff --git a/user/anime/animelist.structs.go b/user/anime/animelist.structs.go
index 80eb01a..c727d97 100644
--- a/user/anime/animelist.structs.go
+++ b/user/anime/animelist.structs.go
@@ -20,12 +20,15 @@ import (
"github.com/MikunoNaka/MAL2Go/anime"
)
-type AnimeListRaw struct {
+type animeListRaw struct {
Data []struct {
Anime anime.Anime `json:"node"`
ListStatus anime.ListStatus `json:"list_status"`
- } `json:"data"`
- Paging anime.ListPaging `json:"paging"`
+ } `json:"data"`
+ Paging struct {
+ NextPage string `json:"next"`
+ PrevPage string `json:"previous"`
+ } `json:"paging"`
}
type UpdateAnimeData struct {
@@ -35,7 +38,6 @@ type UpdateAnimeData struct {
EpWatched int
Priority int
TimesRewatched int
- // NOTE: idk what RewatchValue is
RewatchValue int
Tags string
Comments string
diff --git a/user/anime/request_handler.go b/user/anime/request_handler.go
index abc0afc..45bfe83 100644
--- a/user/anime/request_handler.go
+++ b/user/anime/request_handler.go
@@ -16,22 +16,34 @@
package anime
import (
- "encoding/json"
- "io/ioutil"
- "log"
- "net/http"
- "net/url"
- "strconv"
- "strings"
+ "encoding/json"
+ "io/ioutil"
+ "log"
+ "net/http"
+ "net/url"
+ "strconv"
+ "strings"
+ "errors"
+ "github.com/MikunoNaka/MAL2Go/util"
)
-type serverResponse struct {
- Message string
- Error string
+type UpdateResponse struct {
+ Status string `json:"status"`
+ Score int `json:"score"`
+ EpWatched int `json:"num_episodes_watched"`
+ IsRewatching bool `json:"is_rewatching"`
+ StartDate string `json:"start_date"`
+ FinishDate string `json:"finish_date"`
+ Priority string `json:"priority"`
+ TimesRewatched string `json:"num_times_rewatched"`
+ RewatchValue string `json:"rewatch_value"`
+ Tags string `json:"tags"`
+ Comments string `json:"string"`
+ UpdatedAt string `json:"updated_at"`
}
// Handles HTTP request with your OAuth token as a Header
-func (c Client) requestHandler(endpoint, method string) string {
+func (c Client) requestHandler(endpoint, method string) (string, error) {
// generate request
req, err := http.NewRequest(method, endpoint, nil)
if err != nil {
@@ -52,16 +64,23 @@ func (c Client) requestHandler(endpoint, method string) string {
log.Fatal(err)
}
+ // error handling (if API returns error)
+ var errMsg util.APIError
+ json.Unmarshal(body, &errMsg)
+ if errMsg.Err != "" {
+ return string(body), errors.New(errMsg.Err + " " + errMsg.Msg)
+ }
+
// for DeleteAnime, its endpoint returns null data
if method == "DELETE" {
- return strconv.Itoa(res.StatusCode)
+ return strconv.Itoa(res.StatusCode), nil
}
- return string(body)
+ return string(body), nil
}
// for PUT requests (used for updating anime)
-func (c Client) putRequestHandler(endpoint string, params url.Values) serverResponse {
+func (c Client) putRequestHandler(endpoint string, params url.Values) (UpdateResponse, error) {
paramsEncoded := params.Encode()
// generate request
@@ -87,10 +106,15 @@ func (c Client) putRequestHandler(endpoint string, params url.Values) serverResp
log.Fatal(err)
}
- // TODO: there are other serverResponses. Add them
- // server response, ie message / error
- var resp serverResponse
+ // error handling (if API returns error)
+ var errMsg util.APIError
+ json.Unmarshal(body, &errMsg)
+ if errMsg.Err != "" {
+ return UpdateResponse{}, errors.New(errMsg.Err + " " + errMsg.Msg)
+ }
+
+ var resp UpdateResponse
json.Unmarshal(body, &resp)
- return resp
+ return resp, nil
}
diff --git a/user/anime/update_animelist.go b/user/anime/update_animelist.go
index 780f079..ec0d60d 100644
--- a/user/anime/update_animelist.go
+++ b/user/anime/update_animelist.go
@@ -18,8 +18,8 @@ package anime
import (
e "github.com/MikunoNaka/MAL2Go/errhandlers"
"fmt"
- "net/url"
- "strconv"
+ "net/url"
+ "strconv"
)
// generate the endpoint url with the anime id
@@ -28,12 +28,12 @@ func endpointGenerator(id int) string {
}
// update just an anime's status
-func (c Client)SetStatus(id int, status string) (serverResponse, error) {
+func (c Client)SetStatus(id int, status string) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified list status is valid
if !e.IsValidListStatus(status) {
- return serverResponse{}, e.InvalidStatusError
+ return UpdateResponse{}, e.InvalidStatusError
}
// data to be sent to the server
@@ -41,11 +41,11 @@ func (c Client)SetStatus(id int, status string) (serverResponse, error) {
params.Set("status", status)
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just an anime's num of episodes watched
-func (c Client)SetWatchedEpisodes(id int, episodesWatched int) (serverResponse, error) {
+func (c Client)SetWatchedEpisodes(id int, episodesWatched int) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
@@ -53,11 +53,11 @@ func (c Client)SetWatchedEpisodes(id int, episodesWatched int) (serverResponse,
params.Set("num_watched_episodes", strconv.Itoa(episodesWatched))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just an anime's rewatching status
-func (c Client)SetIsRewatching(id int, isRewatching bool) (serverResponse, error) {
+func (c Client)SetIsRewatching(id int, isRewatching bool) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
@@ -65,16 +65,16 @@ func (c Client)SetIsRewatching(id int, isRewatching bool) (serverResponse, error
params.Set("is_rewatching", strconv.FormatBool(isRewatching))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just the anime's score
-func (c Client)SetScore(id int, score int) (serverResponse, error) {
+func (c Client)SetScore(id int, score int) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified score is valid
if !e.IsValidScore(score) {
- return serverResponse{}, e.InvalidScoreError
+ return UpdateResponse{}, e.InvalidScoreError
}
// data to be sent to the server
@@ -82,16 +82,16 @@ func (c Client)SetScore(id int, score int) (serverResponse, error) {
params.Set("score", strconv.Itoa(score))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just an anime's priority
-func (c Client)SetPriority(id int, priority int) (serverResponse, error) {
+func (c Client)SetPriority(id int, priority int) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified priority is valid
if !e.IsValidPriority(priority) {
- return serverResponse{}, e.InvalidPriorityError
+ return UpdateResponse{}, e.InvalidPriorityError
}
// data to be sent to the server
@@ -99,16 +99,16 @@ func (c Client)SetPriority(id int, priority int) (serverResponse, error) {
params.Set("priority", strconv.Itoa(priority))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just an anime's rewatch value
-func (c Client)SetRewatchValue(id int, rewatchValue int) (serverResponse, error) {
+func (c Client)SetRewatchValue(id int, rewatchValue int) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified rewatch value is valid
if !e.IsValidRewatchValue(rewatchValue) {
- return serverResponse{}, e.InvalidRewatchValueError
+ return UpdateResponse{}, e.InvalidRewatchValueError
}
// data to be sent to the server
@@ -116,11 +116,11 @@ func (c Client)SetRewatchValue(id int, rewatchValue int) (serverResponse, error)
params.Set("rewatch_value", strconv.Itoa(rewatchValue))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just an anime's rewatch count
-func (c Client)SetRewatchCount(id int, rewatchCount int) (serverResponse, error) {
+func (c Client)SetRewatchCount(id int, rewatchCount int) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
@@ -128,11 +128,11 @@ func (c Client)SetRewatchCount(id int, rewatchCount int) (serverResponse, error)
params.Set("num_times_rewatched", strconv.Itoa(rewatchCount))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just an anime's tags
-func (c Client)UpdateTags(id int, tags string) (serverResponse, error) {
+func (c Client)UpdateTags(id int, tags string) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
@@ -140,11 +140,11 @@ func (c Client)UpdateTags(id int, tags string) (serverResponse, error) {
params.Set("tags", tags)
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just an anime's comments
-func (c Client)UpdateComments(id int, comments string) (serverResponse, error) {
+func (c Client)UpdateComments(id int, comments string) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
@@ -152,34 +152,34 @@ func (c Client)UpdateComments(id int, comments string) (serverResponse, error) {
params.Set("comments", comments)
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
/* This will overwrite everything
* i won't use it.. but it's pretty flexible
* so this will stay here */
// Update/Add an anime to user's anime list
-func (c Client)UpdateAnime(id int, data UpdateAnimeData) (serverResponse, error) {
+func (c Client)UpdateAnime(id int, data UpdateAnimeData) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified list status is valid
if !e.IsValidListStatus(data.Status) {
- return serverResponse{}, e.InvalidStatusError
+ return UpdateResponse{}, e.InvalidStatusError
}
// checks if specified score is valid
if !e.IsValidScore(data.Score) {
- return serverResponse{}, e.InvalidScoreError
+ return UpdateResponse{}, e.InvalidScoreError
}
// checks if specified priority is valid
if !e.IsValidPriority(data.Priority) {
- return serverResponse{}, e.InvalidPriorityError
+ return UpdateResponse{}, e.InvalidPriorityError
}
// checks if specified rewatch value is valid
if !e.IsValidRewatchValue(data.RewatchValue) {
- return serverResponse{}, e.InvalidRereadValueError
+ return UpdateResponse{}, e.InvalidRereadValueError
}
params := url.Values{}
@@ -197,6 +197,6 @@ func (c Client)UpdateAnime(id int, data UpdateAnimeData) (serverResponse, error)
params.Set("comments", data.Comments)
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}