Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / github.com / Azure / go-autorest / autorest / retriablerequest_1.7.go
1 // +build !go1.8
2
3 // Copyright 2017 Microsoft Corporation
4 //
5 //  Licensed under the Apache License, Version 2.0 (the "License");
6 //  you may not use this file except in compliance with the License.
7 //  You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 //  Unless required by applicable law or agreed to in writing, software
12 //  distributed under the License is distributed on an "AS IS" BASIS,
13 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 //  See the License for the specific language governing permissions and
15 //  limitations under the License.
16
17 package autorest
18
19 import (
20         "bytes"
21         "io/ioutil"
22         "net/http"
23 )
24
25 // RetriableRequest provides facilities for retrying an HTTP request.
26 type RetriableRequest struct {
27         req *http.Request
28         br  *bytes.Reader
29 }
30
31 // Prepare signals that the request is about to be sent.
32 func (rr *RetriableRequest) Prepare() (err error) {
33         // preserve the request body; this is to support retry logic as
34         // the underlying transport will always close the reqeust body
35         if rr.req.Body != nil {
36                 if rr.br != nil {
37                         _, err = rr.br.Seek(0, 0 /*io.SeekStart*/)
38                         rr.req.Body = ioutil.NopCloser(rr.br)
39                 }
40                 if err != nil {
41                         return err
42                 }
43                 if rr.br == nil {
44                         // fall back to making a copy (only do this once)
45                         err = rr.prepareFromByteReader()
46                 }
47         }
48         return err
49 }
50
51 func removeRequestBody(req *http.Request) {
52         req.Body = nil
53         req.ContentLength = 0
54 }