forked from Mirrors/oauth2
Fixing styling issues for caching.
This commit is contained in:
parent
0476447419
commit
6bb3577bf0
15
cache.go
15
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
15
transport.go
15
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
|
||||
|
|
Loading…
Reference in New Issue