retry
This commit is contained in:
parent
ce6558fbb5
commit
47a918a8ef
1 changed files with 16 additions and 3 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue