blob: 342aca46108e65b9c379f7d162d0505b5d91bb4b [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.
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070013func (c *Context) WithReauth(ap AccessProvider, f func() error) error {
14 err := f()
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070015 cause, ok := err.(*perigee.UnexpectedResponseCodeError)
16 if ok && cause.Actual == 401 {
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070017 err = c.reauthHandler(ap)
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070018 if err == nil {
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070019 err = f()
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070020 }
21 }
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070022 return err
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -070023}
Mark Peek441991d2013-08-24 19:00:24 -070024
25// This is like WithReauth above but returns a perigee Response object
26func (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}