From 47a918a8efb4bca68fe3e98c52e5265393be4bfb Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Wed, 21 Jul 2021 14:54:24 -0500 Subject: [PATCH] retry --- osuapi/osuapi.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/osuapi/osuapi.go b/osuapi/osuapi.go index e65db30..bbf5050 100644 --- a/osuapi/osuapi.go +++ b/osuapi/osuapi.go @@ -47,9 +47,15 @@ func New(config *config.Config) *Osuapi { } func (api *Osuapi) Token() (token string, err error) { - if time.Now().Before(api.expires) { - token = api.token - return + return api.token(false) +} + +func (api *Osuapi) token(force bool) (token string, err error) { + if !force { + if time.Now().Before(api.expires) { + token = api.token + return + } } if api.isFetchingToken { @@ -123,6 +129,13 @@ func (api *Osuapi) Request0(action string, url string) (resp *http.Response, err } if resp.StatusCode != 200 { + if resp.StatusCode == 401 { + // force fetch token again + api.token(true) + log.Println("re-fetched token cus expired") + return api.Request0(action, url) + } + var respBody []byte respBody, err = ioutil.ReadAll(resp.Body) if err != nil {