| 1 |
package autorest |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"crypto/tls" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"io/ioutil" |
| 24 |
"log" |
| 25 |
"net/http" |
| 26 |
"strings" |
| 27 |
"time" |
| 28 |
|
| 29 |
"github.com/Azure/go-autorest/logger" |
| 30 |
) |
| 31 |
|
| 32 |
const ( |
| 33 |
|
| 34 |
DefaultPollingDelay = 30 * time.Second |
| 35 |
|
| 36 |
|
| 37 |
DefaultPollingDuration = 15 * time.Minute |
| 38 |
|
| 39 |
|
| 40 |
DefaultRetryAttempts = 3 |
| 41 |
|
| 42 |
|
| 43 |
DefaultRetryDuration = 30 * time.Second |
| 44 |
) |
| 45 |
|
| 46 |
var ( |
| 47 |
|
| 48 |
StatusCodesForRetry = []int{ |
| 49 |
http.StatusRequestTimeout, |
| 50 |
http.StatusTooManyRequests, |
| 51 |
http.StatusInternalServerError, |
| 52 |
http.StatusBadGateway, |
| 53 |
http.StatusServiceUnavailable, |
| 54 |
http.StatusGatewayTimeout, |
| 55 |
} |
| 56 |
) |
| 57 |
|
| 58 |
const ( |
| 59 |
requestFormat = `HTTP Request Begin =================================================== |
| 60 |
%s |
| 61 |
===================================================== HTTP Request End |
| 62 |
` |
| 63 |
responseFormat = `HTTP Response Begin =================================================== |
| 64 |
%s |
| 65 |
===================================================== HTTP Response End |
| 66 |
` |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
type Response struct { |
| 72 |
*http.Response `json:"-"` |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func (r Response) IsHTTPStatus(statusCode int) bool { |
| 78 |
if r.Response == nil { |
| 79 |
return false |
| 80 |
} |
| 81 |
return r.Response.StatusCode == statusCode |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
func (r Response) HasHTTPStatus(statusCodes ...int) bool { |
| 88 |
return ResponseHasStatusCode(r.Response, statusCodes...) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
type LoggingInspector struct { |
| 94 |
Logger *log.Logger |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
func (li LoggingInspector) WithInspection() PrepareDecorator { |
| 103 |
return func(p Preparer) Preparer { |
| 104 |
return PreparerFunc(func(r *http.Request) (*http.Request, error) { |
| 105 |
var body, b bytes.Buffer |
| 106 |
|
| 107 |
defer r.Body.Close() |
| 108 |
|
| 109 |
r.Body = ioutil.NopCloser(io.TeeReader(r.Body, &body)) |
| 110 |
if err := r.Write(&b); err != nil { |
| 111 |
return nil, fmt.Errorf("Failed to write response: %v", err) |
| 112 |
} |
| 113 |
|
| 114 |
li.Logger.Printf(requestFormat, b.String()) |
| 115 |
|
| 116 |
r.Body = ioutil.NopCloser(&body) |
| 117 |
return p.Prepare(r) |
| 118 |
}) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
func (li LoggingInspector) ByInspecting() RespondDecorator { |
| 128 |
return func(r Responder) Responder { |
| 129 |
return ResponderFunc(func(resp *http.Response) error { |
| 130 |
var body, b bytes.Buffer |
| 131 |
defer resp.Body.Close() |
| 132 |
resp.Body = ioutil.NopCloser(io.TeeReader(resp.Body, &body)) |
| 133 |
if err := resp.Write(&b); err != nil { |
| 134 |
return fmt.Errorf("Failed to write response: %v", err) |
| 135 |
} |
| 136 |
|
| 137 |
li.Logger.Printf(responseFormat, b.String()) |
| 138 |
|
| 139 |
resp.Body = ioutil.NopCloser(&body) |
| 140 |
return r.Respond(resp) |
| 141 |
}) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
type Client struct { |
| 157 |
Authorizer Authorizer |
| 158 |
Sender Sender |
| 159 |
RequestInspector PrepareDecorator |
| 160 |
ResponseInspector RespondDecorator |
| 161 |
|
| 162 |
|
| 163 |
PollingDelay time.Duration |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
PollingDuration time.Duration |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
RetryAttempts int |
| 172 |
|
| 173 |
|
| 174 |
RetryDuration time.Duration |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
UserAgent string |
| 179 |
|
| 180 |
Jar http.CookieJar |
| 181 |
|
| 182 |
|
| 183 |
SkipResourceProviderRegistration bool |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
SendDecorators []SendDecorator |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
func NewClientWithUserAgent(ua string) Client { |
| 194 |
return newClient(ua, tls.RenegotiateNever) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
type ClientOptions struct { |
| 199 |
|
| 200 |
UserAgent string |
| 201 |
|
| 202 |
|
| 203 |
Renegotiation tls.RenegotiationSupport |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
func NewClientWithOptions(options ClientOptions) Client { |
| 208 |
return newClient(options.UserAgent, options.Renegotiation) |
| 209 |
} |
| 210 |
|
| 211 |
func newClient(ua string, renegotiation tls.RenegotiationSupport) Client { |
| 212 |
c := Client{ |
| 213 |
PollingDelay: DefaultPollingDelay, |
| 214 |
PollingDuration: DefaultPollingDuration, |
| 215 |
RetryAttempts: DefaultRetryAttempts, |
| 216 |
RetryDuration: DefaultRetryDuration, |
| 217 |
UserAgent: UserAgent(), |
| 218 |
} |
| 219 |
c.Sender = c.sender(renegotiation) |
| 220 |
c.AddToUserAgent(ua) |
| 221 |
return c |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
func (c *Client) AddToUserAgent(extension string) error { |
| 226 |
if extension != "" { |
| 227 |
c.UserAgent = fmt.Sprintf("%s %s", c.UserAgent, extension) |
| 228 |
return nil |
| 229 |
} |
| 230 |
return fmt.Errorf("Extension was empty, User Agent stayed as %s", c.UserAgent) |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
func (c Client) Do(r *http.Request) (*http.Response, error) { |
| 237 |
if r.UserAgent() == "" { |
| 238 |
r, _ = Prepare(r, |
| 239 |
WithUserAgent(c.UserAgent)) |
| 240 |
} |
| 241 |
|
| 242 |
r, err := Prepare(r, |
| 243 |
c.WithAuthorization(), |
| 244 |
c.WithInspection()) |
| 245 |
if err != nil { |
| 246 |
var resp *http.Response |
| 247 |
if detErr, ok := err.(DetailedError); ok { |
| 248 |
|
| 249 |
|
| 250 |
resp = detErr.Response |
| 251 |
} |
| 252 |
return resp, NewErrorWithError(err, "autorest/Client", "Do", nil, "Preparing request failed") |
| 253 |
} |
| 254 |
logger.Instance.WriteRequest(r, logger.Filter{ |
| 255 |
Header: func(k string, v []string) (bool, []string) { |
| 256 |
|
| 257 |
if strings.EqualFold(k, "Authorization") || strings.EqualFold(k, "Ocp-Apim-Subscription-Key") { |
| 258 |
v = []string{"**REDACTED**"} |
| 259 |
} |
| 260 |
return true, v |
| 261 |
}, |
| 262 |
}) |
| 263 |
resp, err := SendWithSender(c.sender(tls.RenegotiateNever), r) |
| 264 |
if resp == nil && err == nil { |
| 265 |
err = errors.New("autorest: received nil response and error") |
| 266 |
} |
| 267 |
logger.Instance.WriteResponse(resp, logger.Filter{}) |
| 268 |
Respond(resp, c.ByInspecting()) |
| 269 |
return resp, err |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
func (c Client) sender(renengotiation tls.RenegotiationSupport) Sender { |
| 274 |
if c.Sender == nil { |
| 275 |
return sender(renengotiation) |
| 276 |
} |
| 277 |
return c.Sender |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
func (c Client) WithAuthorization() PrepareDecorator { |
| 283 |
return c.authorizer().WithAuthorization() |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
func (c Client) authorizer() Authorizer { |
| 288 |
if c.Authorizer == nil { |
| 289 |
return NullAuthorizer{} |
| 290 |
} |
| 291 |
return c.Authorizer |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
func (c Client) WithInspection() PrepareDecorator { |
| 297 |
if c.RequestInspector == nil { |
| 298 |
return WithNothing() |
| 299 |
} |
| 300 |
return c.RequestInspector |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
func (c Client) ByInspecting() RespondDecorator { |
| 306 |
if c.ResponseInspector == nil { |
| 307 |
return ByIgnoring() |
| 308 |
} |
| 309 |
return c.ResponseInspector |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
func (c Client) Send(req *http.Request, decorators ...SendDecorator) (*http.Response, error) { |
| 320 |
if c.SendDecorators != nil { |
| 321 |
decorators = c.SendDecorators |
| 322 |
} |
| 323 |
inCtx := req.Context().Value(ctxSendDecorators{}) |
| 324 |
if sd, ok := inCtx.([]SendDecorator); ok { |
| 325 |
decorators = sd |
| 326 |
} |
| 327 |
return SendWithSender(c, req, decorators...) |
| 328 |
} |