aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2023-08-15 15:42:00 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2023-08-15 15:42:00 +0530
commit33283bff8b1ae239036aeb1526ec3b7483767ed7 (patch)
tree3219f6a539acca526d2ea6c2fa2d6c79c0b8e958
parentaf9d2adedcb37b2c6c316cf52950a19d2a245803 (diff)
added option to use client authv0.0.5
-rw-r--r--anime.go6
-rw-r--r--client.go7
-rw-r--r--errors.go29
-rw-r--r--request_handlers.go16
-rw-r--r--user.go5
5 files changed, 42 insertions, 21 deletions
diff --git a/anime.go b/anime.go
index d73920c..22935d3 100644
--- a/anime.go
+++ b/anime.go
@@ -116,8 +116,12 @@ func (c Client) GetSeasonalAnime(animes *[]Anime, params *SeasonalParams) error
return nil
}
-// TODO: only allow MainAuth not ClientAuth
func (c Client) GetSuggestedAnime(animes *[]Anime, params *SuggestedParams) error {
+ if c.ClientAuthOnly {
+ // this endpoint requires main auth
+ return ErrClientAuthNotSupported
+ }
+
err := validateSuggestedParams(params)
if err != nil {
return err
diff --git a/client.go b/client.go
index 0a3863c..253d9de 100644
--- a/client.go
+++ b/client.go
@@ -20,7 +20,8 @@ package mg
import "net/http"
type Client struct {
- MainAuth string
- ClientAuth string
- httpClient http.Client
+ MainAuth string
+ ClientAuth string
+ ClientAuthOnly bool
+ httpClient http.Client
}
diff --git a/errors.go b/errors.go
index ad125c2..ad5b58c 100644
--- a/errors.go
+++ b/errors.go
@@ -20,18 +20,19 @@ package mg
import "errors"
var (
- ErrLimitOutOfRange = errors.New("mg: invalid limit (out of range)")
- ErrInvalidField = errors.New("mg: invalid field passed")
- ErrInvalidRankingType = errors.New("mg: invalid ranking type")
- ErrInvalidSeason = errors.New("mg: invalid season")
- ErrInvalidSort = errors.New("mg: invalid sort")
- ErrInvalidStatus = errors.New("mg: invalid status")
- ErrInvalidScore = errors.New("mg: invalid score")
- ErrInvalidPriority = errors.New("mg: invalid priority")
- ErrInvalidRewatchValue = errors.New("mg: invalid rewatch value")
- ErrInvalidRereadValue = errors.New("mg: invalid reread value")
- ErrEmptySearchString = errors.New("mg: invalid search string (empty string)")
- ErrInvalidYear = errors.New("mg: invalid year (not integer)")
- ErrUnknownUpdateParam = errors.New("mg: unknown update param")
- ErrAnimeNotInList = errors.New("mg: anime is already not in list")
+ ErrLimitOutOfRange = errors.New("mg: invalid limit (out of range)")
+ ErrInvalidField = errors.New("mg: invalid field passed")
+ ErrInvalidRankingType = errors.New("mg: invalid ranking type")
+ ErrInvalidSeason = errors.New("mg: invalid season")
+ ErrInvalidSort = errors.New("mg: invalid sort")
+ ErrInvalidStatus = errors.New("mg: invalid status")
+ ErrInvalidScore = errors.New("mg: invalid score")
+ ErrInvalidPriority = errors.New("mg: invalid priority")
+ ErrInvalidRewatchValue = errors.New("mg: invalid rewatch value")
+ ErrInvalidRereadValue = errors.New("mg: invalid reread value")
+ ErrEmptySearchString = errors.New("mg: invalid search string (empty string)")
+ ErrInvalidYear = errors.New("mg: invalid year (not integer)")
+ ErrUnknownUpdateParam = errors.New("mg: unknown update param")
+ ErrAnimeNotInList = errors.New("mg: anime is already not in list")
+ ErrClientAuthNotSupported = errors.New("mg: function does not support client auth")
)
diff --git a/request_handlers.go b/request_handlers.go
index adf5535..187d759 100644
--- a/request_handlers.go
+++ b/request_handlers.go
@@ -32,9 +32,11 @@ func (c Client) get(endpoint string) ([]byte, error) {
req, _ := http.NewRequest(http.MethodGet, endpoint, nil)
// add authorization headers
- // TODO: implement client auth
- //req.Header.Add("X-MAL-CLIENT-ID", c.ClientAuth)
- req.Header.Add("Authorization", c.MainAuth)
+ if c.ClientAuthOnly {
+ req.Header.Add("X-MAL-CLIENT-ID", c.ClientAuth)
+ } else {
+ req.Header.Add("Authorization", c.MainAuth)
+ }
// send request
res, err := c.httpClient.Do(req)
@@ -60,6 +62,10 @@ func (c Client) get(endpoint string) ([]byte, error) {
}
func (c Client) delete(endpoint string) error {
+ if c.ClientAuthOnly {
+ return ErrClientAuthNotSupported
+ }
+
// can safely ignore error with http.NewRequest
req, _ := http.NewRequest(http.MethodDelete, endpoint, nil)
@@ -95,6 +101,10 @@ func (c Client) delete(endpoint string) error {
}
func (c Client) put(endpoint string, params url.Values) ([]byte, error) {
+ if c.ClientAuthOnly {
+ return []byte{}, ErrClientAuthNotSupported
+ }
+
encodedParams := params.Encode()
// can safely ignore error with http.NewRequest
diff --git a/user.go b/user.go
index 16ca2b8..870c137 100644
--- a/user.go
+++ b/user.go
@@ -20,6 +20,11 @@ package mg
import "encoding/json"
func (c Client) GetSelfInfo(user *User, getStatistics bool) error {
+ if c.ClientAuthOnly {
+ // this endpoint requires main auth
+ return ErrClientAuthNotSupported
+ }
+
endpoint := BASE_URL + "/users/@me"
if getStatistics {
endpoint += "?fields=anime_statistics"