blob: 98fba557c8bd528e60dd6e0e78128cf9e12c5bf7 [file] [log] [blame]
Samuel A. Falvo II1206f852013-07-15 17:56:51 -07001package gophercloud
2
3import (
4 "github.com/racker/perigee"
5)
6
7// WithReauth wraps a Perigee request fragment with logic to perform re-authentication
8// if it's deemed necessary.
9//
10// Do not confuse this function with WithReauth()! Although they work together to support reauthentication,
11// WithReauth() actually contains the decision-making logic to determine when to perform a reauth,
12// while WithReauthHandler() is used to configure what a reauth actually entails.
13func (c *Context) WithReauth(f func() (interface{}, error)) (interface{}, error) {
14 result, err := f()
15 cause, ok := err.(*perigee.UnexpectedResponseCodeError)
16 if ok && cause.Actual == 401 {
17 err = c.reauthHandler()
18 if err == nil {
19 result, err = f()
20 }
21 }
22 return result, err
23}