This commit is contained in:
Michael Zhang 2021-07-21 14:54:24 -05:00
parent ce6558fbb5
commit 47a918a8ef
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B

View file

@ -47,10 +47,16 @@ func New(config *config.Config) *Osuapi {
} }
func (api *Osuapi) Token() (token string, err error) { func (api *Osuapi) Token() (token string, err error) {
return api.token(false)
}
func (api *Osuapi) token(force bool) (token string, err error) {
if !force {
if time.Now().Before(api.expires) { if time.Now().Before(api.expires) {
token = api.token token = api.token
return return
} }
}
if api.isFetchingToken { if api.isFetchingToken {
api.tokenLock.RLock() api.tokenLock.RLock()
@ -123,6 +129,13 @@ func (api *Osuapi) Request0(action string, url string) (resp *http.Response, err
} }
if resp.StatusCode != 200 { 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 var respBody []byte
respBody, err = ioutil.ReadAll(resp.Body) respBody, err = ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {