aboutsummaryrefslogtreecommitdiff
path: root/user/anime/request_handler.go
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/request_handler.go
parent71210ebc8e04d49a6afeeecee842b2e8b53f3c4f (diff)
added api error handling to user/anime and user/manga packages
Diffstat (limited to 'user/anime/request_handler.go')
-rw-r--r--user/anime/request_handler.go60
1 files changed, 42 insertions, 18 deletions
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
}