3 // Copyright 2017 Microsoft Corporation
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 contentType = "Content-Type"
23 mimeTypeFormPost = "application/x-www-form-urlencoded"
26 // Sender is the interface that wraps the Do method to send HTTP requests.
28 // The standard http.Client conforms to this interface.
29 type Sender interface {
30 Do(*http.Request) (*http.Response, error)
33 // SenderFunc is a method that implements the Sender interface.
34 type SenderFunc func(*http.Request) (*http.Response, error)
36 // Do implements the Sender interface on SenderFunc.
37 func (sf SenderFunc) Do(r *http.Request) (*http.Response, error) {
41 // SendDecorator takes and possibly decorates, by wrapping, a Sender. Decorators may affect the
42 // http.Request and pass it along or, first, pass the http.Request along then react to the
43 // http.Response result.
44 type SendDecorator func(Sender) Sender
46 // CreateSender creates, decorates, and returns, as a Sender, the default http.Client.
47 func CreateSender(decorators ...SendDecorator) Sender {
48 return DecorateSender(&http.Client{}, decorators...)
51 // DecorateSender accepts a Sender and a, possibly empty, set of SendDecorators, which is applies to
52 // the Sender. Decorators are applied in the order received, but their affect upon the request
53 // depends on whether they are a pre-decorator (change the http.Request and then pass it along) or a
54 // post-decorator (pass the http.Request along and react to the results in http.Response).
55 func DecorateSender(s Sender, decorators ...SendDecorator) Sender {
56 for _, decorate := range decorators {