-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Drew MacInnis
authored and
Drew MacInnis
committed
Nov 18, 2016
1 parent
3d0c34f
commit 8bf28ec
Showing
4 changed files
with
89 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package vault | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// httpClient | ||
type httpClient interface { | ||
GetHttpClient() *http.Client | ||
SetToken(req *http.Request) | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
func requestAndFollow(hc httpClient, method string, u *url.URL, body []byte) (*http.Response, error) { | ||
var res *http.Response | ||
var err error | ||
for attempts := 0; attempts < 2; attempts++ { | ||
reader := bytes.NewReader(body) | ||
req, err := http.NewRequest(method, u.String(), reader) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
hc.SetToken(req) | ||
if method == "POST" { | ||
req.Header.Set("Content-Type", "application/json; charset=utf-8") | ||
} | ||
|
||
res, err = hc.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if res.StatusCode == 307 { | ||
res.Body.Close() | ||
location, errLocation := res.Location() | ||
if errLocation != nil { | ||
return nil, errLocation | ||
} | ||
u.Host = location.Host | ||
} else { | ||
break | ||
} | ||
} | ||
return res, err | ||
} |