Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame^] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 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. |
| 13 | func (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 | } |