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. |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 13 | func (c *Context) WithReauth(ap AccessProvider, f func() error) error { |
| 14 | err := f() |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 15 | cause, ok := err.(*perigee.UnexpectedResponseCodeError) |
| 16 | if ok && cause.Actual == 401 { |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 17 | err = c.reauthHandler(ap) |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 18 | if err == nil { |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 19 | err = f() |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 20 | } |
| 21 | } |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 22 | return err |
Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame] | 23 | } |
Mark Peek | 441991d | 2013-08-24 19:00:24 -0700 | [diff] [blame] | 24 | |
| 25 | // This is like WithReauth above but returns a perigee Response object |
| 26 | func (c *Context) ResponseWithReauth(ap AccessProvider, f func() (*perigee.Response, error)) (*perigee.Response, error) { |
| 27 | response, err := f() |
| 28 | cause, ok := err.(*perigee.UnexpectedResponseCodeError) |
| 29 | if ok && cause.Actual == 401 { |
| 30 | err = c.reauthHandler(ap) |
| 31 | if err == nil { |
| 32 | response, err = f() |
| 33 | } |
| 34 | } |
| 35 | return response, err |
| 36 | } |