feat: autorefreshing after the access token expires.
This commit is contained in:
parent
4b088f5617
commit
4d489f1686
1 changed files with 32 additions and 1 deletions
33
api/api.go
33
api/api.go
|
@ -29,7 +29,7 @@ type ClientOptions struct {
|
|||
ClientSecret string `json:"client_secret"`
|
||||
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpirationDate time.Time `json:"access"`
|
||||
ExpirationAt time.Time `json:"expiration_at"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
|
||||
|
@ -265,6 +265,10 @@ func (api *Client) ExchangeAuth() (*TokenPair, error) {
|
|||
|
||||
api.options.AccessToken = result.AccessToken
|
||||
api.options.RefreshToken = result.RefreshToken
|
||||
now := time.Now()
|
||||
api.options.ExpirationAt = now.Add(
|
||||
time.Second*time.Duration(result.ExpiresIn),
|
||||
)
|
||||
err = api.writeSecret()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -272,6 +276,17 @@ func (api *Client) ExchangeAuth() (*TokenPair, error) {
|
|||
return ret, nil
|
||||
}
|
||||
|
||||
func (api *Client) RefreshTokenIfExpired() error {
|
||||
now := time.Now()
|
||||
if now.After(api.options.ExpirationAt.Add(-time.Second*2)) {
|
||||
_, err := api.RefreshToken()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (api *Client) RefreshToken() (*OauthTokenResponse, error) {
|
||||
result := new(OauthTokenResponse)
|
||||
request := map[string]string{
|
||||
|
@ -293,6 +308,10 @@ func (api *Client) RefreshToken() (*OauthTokenResponse, error) {
|
|||
|
||||
api.options.AccessToken = result.AccessToken
|
||||
api.options.RefreshToken = result.RefreshToken
|
||||
now := time.Now()
|
||||
api.options.ExpirationAt = now.Add(
|
||||
time.Second*time.Duration(result.ExpiresIn),
|
||||
)
|
||||
err = api.writeSecret()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -306,6 +325,10 @@ func (api *Client) Get(resource string, result interface{}, abs ...bool) error {
|
|||
if len(abs) > 0 {
|
||||
a = abs[0]
|
||||
}
|
||||
err := api.RefreshTokenIfExpired()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return api.doRequest(resource, requestOptions{
|
||||
HttpMethod: http.MethodGet,
|
||||
Body: nil,
|
||||
|
@ -315,6 +338,10 @@ func (api *Client) Get(resource string, result interface{}, abs ...bool) error {
|
|||
}
|
||||
|
||||
func (api *Client) Post(resource string, request interface{}, result interface{}) error {
|
||||
err := api.RefreshTokenIfExpired()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return api.doRequest(resource, requestOptions{
|
||||
HttpMethod: http.MethodPost,
|
||||
Body: request,
|
||||
|
@ -323,6 +350,10 @@ func (api *Client) Post(resource string, request interface{}, result interface{}
|
|||
}
|
||||
|
||||
func (api *Client) Patch(resource string, request interface{}, result interface{}) error {
|
||||
err := api.RefreshTokenIfExpired()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return api.doRequest(resource, requestOptions{
|
||||
HttpMethod: http.MethodPatch,
|
||||
Body: request,
|
||||
|
|
Loading…
Reference in a new issue