From 6bb3577bf00dce0ad284f5ea515dccbed52f62e5 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Thu, 22 May 2014 14:33:36 +0200 Subject: [PATCH] Fixing styling issues for caching. --- cache.go | 15 +++++++++------ cache_test.go | 2 +- transport.go | 15 ++++++++------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/cache.go b/cache.go index f4e6ad1..5920645 100644 --- a/cache.go +++ b/cache.go @@ -24,8 +24,11 @@ func NewFileCache(filename string) *FileCache { // FileCache represents a file based token cacher. type FileCache struct { - filename string - ErrorHandlerFunc func(error) + // Handler to be invoked if an error occurs + // during read or write operations. + ErrorHandler func(error) + + filename string } // Read reads a cache token from the specified file. @@ -34,8 +37,8 @@ func (f *FileCache) Read() (token *Token) { if err == nil { err = json.Unmarshal(data, token) } - if f.ErrorHandlerFunc != nil { - f.ErrorHandlerFunc(err) + if f.ErrorHandler != nil { + f.ErrorHandler(err) } return } @@ -46,7 +49,7 @@ func (f *FileCache) Write(token *Token) { if err == nil { err = ioutil.WriteFile(f.filename, data, 0644) } - if f.ErrorHandlerFunc != nil { - f.ErrorHandlerFunc(err) + if f.ErrorHandler != nil { + f.ErrorHandler(err) } } diff --git a/cache_test.go b/cache_test.go index a7bfef1..0c0e351 100644 --- a/cache_test.go +++ b/cache_test.go @@ -12,7 +12,7 @@ import ( func TestFileCacheErrorHandling(t *testing.T) { var lastErr error fileCache := NewFileCache("/path/that/doesnt/exist") - fileCache.ErrorHandlerFunc = func(err error) { + fileCache.ErrorHandler = func(err error) { lastErr = err } fileCache.Read() diff --git a/transport.go b/transport.go index dac1707..4346770 100644 --- a/transport.go +++ b/transport.go @@ -94,12 +94,14 @@ 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() + // Try to initialize the token from the cache. + if token == nil { + if c := t.fetcher.Cache(); c != nil { + token = c.Read() + } } + if token == nil || token.Expired() { // Check if the token is refreshable. // If token is refreshable, don't return an error, @@ -165,9 +167,8 @@ func (t *authorizedTransport) RefreshToken() error { } t.token = token - cache := t.fetcher.Cache() - if cache != nil { - cache.Write(token) + if c := t.fetcher.Cache(); c != nil { + c.Write(token) } return nil