From 2e00ad50b11c6884953da5c8b778745df4f748ab Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Mon, 19 May 2014 00:20:42 +0200 Subject: [PATCH] Authorized transport should read from cache and write to cache. --- transport.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/transport.go b/transport.go index ee104c5..67c67c5 100644 --- a/transport.go +++ b/transport.go @@ -104,6 +104,12 @@ func NewAuthorizedTransport(fetcher TokenFetcher, token *Token) Transport { // If token is expired, tries to refresh/fetch a new token. func (t *authorizedTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) { token := t.Token() + cache := t.fetcher.Cache() + + if token == nil && cache != nil { + // Try to read from cache initially + token, _ := cache.Read() + } if token == nil || token.Expired() { // Check if the token is refreshable. // If token is refreshable, don't return an error, @@ -169,6 +175,11 @@ func (t *authorizedTransport) RefreshToken() error { } t.token = token + cache := t.fetcher.Cache() + if cache != nil { + cache.Write(token) + } + return nil }