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"`
|
ClientSecret string `json:"client_secret"`
|
||||||
|
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
ExpirationDate time.Time `json:"access"`
|
ExpirationAt time.Time `json:"expiration_at"`
|
||||||
RefreshToken string `json:"refresh_token"`
|
RefreshToken string `json:"refresh_token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,6 +265,10 @@ func (api *Client) ExchangeAuth() (*TokenPair, error) {
|
||||||
|
|
||||||
api.options.AccessToken = result.AccessToken
|
api.options.AccessToken = result.AccessToken
|
||||||
api.options.RefreshToken = result.RefreshToken
|
api.options.RefreshToken = result.RefreshToken
|
||||||
|
now := time.Now()
|
||||||
|
api.options.ExpirationAt = now.Add(
|
||||||
|
time.Second*time.Duration(result.ExpiresIn),
|
||||||
|
)
|
||||||
err = api.writeSecret()
|
err = api.writeSecret()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -272,6 +276,17 @@ func (api *Client) ExchangeAuth() (*TokenPair, error) {
|
||||||
return ret, nil
|
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) {
|
func (api *Client) RefreshToken() (*OauthTokenResponse, error) {
|
||||||
result := new(OauthTokenResponse)
|
result := new(OauthTokenResponse)
|
||||||
request := map[string]string{
|
request := map[string]string{
|
||||||
|
@ -293,6 +308,10 @@ func (api *Client) RefreshToken() (*OauthTokenResponse, error) {
|
||||||
|
|
||||||
api.options.AccessToken = result.AccessToken
|
api.options.AccessToken = result.AccessToken
|
||||||
api.options.RefreshToken = result.RefreshToken
|
api.options.RefreshToken = result.RefreshToken
|
||||||
|
now := time.Now()
|
||||||
|
api.options.ExpirationAt = now.Add(
|
||||||
|
time.Second*time.Duration(result.ExpiresIn),
|
||||||
|
)
|
||||||
err = api.writeSecret()
|
err = api.writeSecret()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -306,6 +325,10 @@ func (api *Client) Get(resource string, result interface{}, abs ...bool) error {
|
||||||
if len(abs) > 0 {
|
if len(abs) > 0 {
|
||||||
a = abs[0]
|
a = abs[0]
|
||||||
}
|
}
|
||||||
|
err := api.RefreshTokenIfExpired()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return api.doRequest(resource, requestOptions{
|
return api.doRequest(resource, requestOptions{
|
||||||
HttpMethod: http.MethodGet,
|
HttpMethod: http.MethodGet,
|
||||||
Body: nil,
|
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 {
|
func (api *Client) Post(resource string, request interface{}, result interface{}) error {
|
||||||
|
err := api.RefreshTokenIfExpired()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return api.doRequest(resource, requestOptions{
|
return api.doRequest(resource, requestOptions{
|
||||||
HttpMethod: http.MethodPost,
|
HttpMethod: http.MethodPost,
|
||||||
Body: request,
|
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 {
|
func (api *Client) Patch(resource string, request interface{}, result interface{}) error {
|
||||||
|
err := api.RefreshTokenIfExpired()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return api.doRequest(resource, requestOptions{
|
return api.doRequest(resource, requestOptions{
|
||||||
HttpMethod: http.MethodPatch,
|
HttpMethod: http.MethodPatch,
|
||||||
Body: request,
|
Body: request,
|
||||||
|
|
Loading…
Reference in a new issue