blob: 5637ea283688cf79fea14673e853e5b963b06b60 [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 II1206f852013-07-15 17:56:51 -070023}