diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-08-15 15:42:00 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-08-15 15:42:00 +0530 |
commit | 33283bff8b1ae239036aeb1526ec3b7483767ed7 (patch) | |
tree | 3219f6a539acca526d2ea6c2fa2d6c79c0b8e958 | |
parent | af9d2adedcb37b2c6c316cf52950a19d2a245803 (diff) |
added option to use client authv0.0.5
-rw-r--r-- | anime.go | 6 | ||||
-rw-r--r-- | client.go | 7 | ||||
-rw-r--r-- | errors.go | 29 | ||||
-rw-r--r-- | request_handlers.go | 16 | ||||
-rw-r--r-- | user.go | 5 |
5 files changed, 42 insertions, 21 deletions
@@ -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 @@ -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 } @@ -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 @@ -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" |