aboutsummaryrefslogtreecommitdiff
path: root/user/manga
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/manga
parent71210ebc8e04d49a6afeeecee842b2e8b53f3c4f (diff)
added api error handling to user/anime and user/manga packages
Diffstat (limited to 'user/manga')
-rw-r--r--user/manga/mangalist.go44
-rw-r--r--user/manga/mangalist.structs.go20
-rw-r--r--user/manga/request_handler.go58
-rw-r--r--user/manga/update_mangalist.go64
4 files changed, 101 insertions, 85 deletions
diff --git a/user/manga/mangalist.go b/user/manga/mangalist.go
index af75e9f..b60b1d7 100644
--- a/user/manga/mangalist.go
+++ b/user/manga/mangalist.go
@@ -17,18 +17,19 @@
package manga
import (
- "encoding/json"
+ "encoding/json"
"strconv"
"fmt"
e "github.com/MikunoNaka/MAL2Go/errhandlers"
u "github.com/MikunoNaka/MAL2Go/util"
+ m "github.com/MikunoNaka/MAL2Go/manga"
)
const BASE_URL string = "https://api.myanimelist.net/v2"
const maxListLimit int = 1000
// Delete a manga from user's manga list
-func (c Client)DeleteManga(id int) string {
+func (c Client)DeleteManga(id int) (string, error) {
endpoint := fmt.Sprintf("%s/manga/%d/my_list_status", BASE_URL, id)
/* Returns 200 if manga successfully deleted
* Alternatively returns 404 if manga not in user's manga list */
@@ -36,18 +37,19 @@ func (c Client)DeleteManga(id int) string {
}
// Get authenticated user's manga list
-func (c Client) GetMangaList(user, status, sort string, limit, offset int, fields []string) (MangaList, error){
- var userMangaList MangaList
+// returns true as second value if there are more mangas present
+func (c Client) GetMangaList(user, status, sort string, limit, offset int, fields []string) ([]m.Manga, bool, error){
+ var userMangaList []m.Manga
// error handling for limit
limitErr := e.LimitErrHandler(limit, maxListLimit)
if limitErr != nil {
- return userMangaList, limitErr
+ return userMangaList, false, limitErr
}
// handle all the errors for the fields
fields, err := e.FieldsErrHandler(fields)
if err != nil {
- return userMangaList, err
+ return userMangaList, false, err
}
// append "list_status" field only used by this func.
@@ -55,12 +57,12 @@ func (c Client) GetMangaList(user, status, sort string, limit, offset int, field
// checks if valid sort is specified
if !e.IsValidMangaListSort(sort) {
- return userMangaList, e.InvalidSortError
+ return userMangaList, false, e.InvalidSortError
}
// checks if valid status is specified
if status != "" && !e.IsValidMangaListStatus(status) {
- return userMangaList, e.InvalidStatusError
+ return userMangaList, false, e.InvalidStatusError
}
// get own list if user not specified
@@ -88,25 +90,23 @@ func (c Client) GetMangaList(user, status, sort string, limit, offset int, field
}
// get data from API
- var mangaListData MangaListRaw
- data := c.requestHandler(endpoint, "GET")
+ var mangaListData mangaListRaw
+ data, err := c.requestHandler(endpoint, "GET")
+ if err != nil {
+ return userMangaList, false, err
+ }
json.Unmarshal([]byte(data), &mangaListData)
- // set MyListStatus for each element and add it to array
- var mangas []Manga
- for _, element := range mangaListData.Data {
- a := element.Manga
- a.ListStatus = element.ListStatus
+ nextPageExists := mangaListData.Paging.NextPage != ""
- mangas = append(mangas, a)
- }
+ // set MyListStatus for each element and add it to slice
+ for _, element := range mangaListData.Data {
+ m := element.Manga
+ m.ListStatus = element.ListStatus
- // finally create AnimeList
- userMangaList = MangaList {
- Mangas: mangas,
- Paging: mangaListData.Paging,
+ userMangaList = append(userMangaList, m)
}
- return userMangaList, nil
+ return userMangaList, nextPageExists, nil
}
diff --git a/user/manga/mangalist.structs.go b/user/manga/mangalist.structs.go
index 0ea7d70..d7e5352 100644
--- a/user/manga/mangalist.structs.go
+++ b/user/manga/mangalist.structs.go
@@ -17,23 +17,18 @@
package manga
import (
- "github.com/MikunoNaka/MAL2Go/util"
"github.com/MikunoNaka/MAL2Go/manga"
)
-type Manga manga.Manga
-
-type MangaListRaw struct {
+type mangaListRaw struct {
Data []struct {
- Manga Manga `json:"node"`
+ Manga manga.Manga `json:"node"`
ListStatus manga.ListStatus `json:"list_status"`
- } `json:"data"`
- Paging util.ListPaging `json:"paging"`
-}
-
-type MangaList struct {
- Mangas []Manga
- Paging util.ListPaging
+ } `json:"data"`
+ Paging struct {
+ NextPage string `json:"next"`
+ PrevPage string `json:"previous"`
+ } `json:"paging"`
}
type UpdateMangaData struct {
@@ -44,7 +39,6 @@ type UpdateMangaData struct {
ChaptersRead int
Priority int
TimesReread int
- // NOTE: idk what RereadValue is
RereadValue int
Tags string
Comments string
diff --git a/user/manga/request_handler.go b/user/manga/request_handler.go
index ff2b0df..0258b38 100644
--- a/user/manga/request_handler.go
+++ b/user/manga/request_handler.go
@@ -16,22 +16,32 @@
package manga
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"`
+ VolumesRead int `json:"num_volumes_read"`
+ ChaptersRead int `json:"num_chapters_read"`
+ IsRereading bool `json:"is_rereading"`
+ Priority string `json:"priority"`
+ TimesReread string `json:"num_times_reread"`
+ RereadValue string `json:"reread_value"`
+ Tags string `json:"tags"`
+ Comments string `json:"string"`
}
// 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 +62,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 +104,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/manga/update_mangalist.go b/user/manga/update_mangalist.go
index 2fbbe48..b60bfd0 100644
--- a/user/manga/update_mangalist.go
+++ b/user/manga/update_mangalist.go
@@ -18,8 +18,8 @@ package manga
import (
e "github.com/MikunoNaka/MAL2Go/errhandlers"
"fmt"
- "net/url"
- "strconv"
+ "net/url"
+ "strconv"
)
// generate the endpoint url with the manga id
@@ -28,12 +28,12 @@ func endpointGenerator(id int) string {
}
// update just an manga'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.IsValidMangaListStatus(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 a manga's num of volumes read
-func (c Client)SetVolumesRead(id int, volumes int) (serverResponse, error) {
+func (c Client)SetVolumesRead(id int, volumes int) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
@@ -53,11 +53,11 @@ func (c Client)SetVolumesRead(id int, volumes int) (serverResponse, error) {
params.Set("num_volumes_read", strconv.Itoa(volumes))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just a manga's num of chapters read
-func (c Client)SetChaptersRead(id int, chapters int) (serverResponse, error) {
+func (c Client)SetChaptersRead(id int, chapters int) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
@@ -65,11 +65,11 @@ func (c Client)SetChaptersRead(id int, chapters int) (serverResponse, error) {
params.Set("num_chapters_read", strconv.Itoa(chapters))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just a manga's rereading status
-func (c Client)SetIsRereading(id int, isRereading bool) (serverResponse, error) {
+func (c Client)SetIsRereading(id int, isRereading bool) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
@@ -77,16 +77,16 @@ func (c Client)SetIsRereading(id int, isRereading bool) (serverResponse, error)
params.Set("is_rereading", strconv.FormatBool(isRereading))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just the manga'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
@@ -94,16 +94,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 a manga'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
@@ -111,16 +111,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 a manga's reread value
-func (c Client)SetRereadValue(id int, rereadValue int) (serverResponse, error) {
+func (c Client)SetRereadValue(id int, rereadValue int) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified reread value is valid
if !e.IsValidRewatchValue(rereadValue) {
- return serverResponse{}, e.InvalidRereadValueError
+ return UpdateResponse{}, e.InvalidRereadValueError
}
// data to be sent to the server
@@ -128,11 +128,11 @@ func (c Client)SetRereadValue(id int, rereadValue int) (serverResponse, error) {
params.Set("reread_value", strconv.Itoa(rereadValue))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just a manga's reread count
-func (c Client)SetRereadCount(id int, rereadCount int) (serverResponse, error) {
+func (c Client)SetRereadCount(id int, rereadCount int) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// data to be sent to the server
@@ -140,11 +140,11 @@ func (c Client)SetRereadCount(id int, rereadCount int) (serverResponse, error) {
params.Set("num_times_reread", strconv.Itoa(rereadCount))
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}
// update just a manga'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
@@ -152,11 +152,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 a manga'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
@@ -164,34 +164,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)
}
/* NOTE: This will overwrite everything
* i won't use it.. but it's pretty flexible
* so this will stay here */
// Update/Add a manga to user's manga list
-func (c Client)UpdateManga(id int, data UpdateMangaData) (serverResponse, error) {
+func (c Client)UpdateManga(id int, data UpdateMangaData) (UpdateResponse, error) {
endpoint := endpointGenerator(id)
// checks if specified list status is valid
if !e.IsValidMangaListStatus(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 reread value is valid
if !e.IsValidRewatchValue(data.RereadValue) {
- return serverResponse{}, e.InvalidRereadValueError
+ return UpdateResponse{}, e.InvalidRereadValueError
}
params := url.Values{}
@@ -209,6 +209,6 @@ func (c Client)UpdateManga(id int, data UpdateMangaData) (serverResponse, error)
params.Set("comments", data.Comments)
// make API request
- return c.putRequestHandler(endpoint, params), nil
+ return c.putRequestHandler(endpoint, params)
}