@@ -114,6 +114,37 @@ func DoWithStringResponse(params Params) (result string, returnErr error) {
114
114
return string (bodyBytes ), nil
115
115
}
116
116
117
+ // DoWithCustomClient is the same as Do but will make the request using the
118
+ // supplied http.Client instead of the cachedClient.
119
+ func DoWithCustomClient (params Params , responseBody interface {}, client * http.Client ) (returnErr error ) {
120
+ req , err := createRequest (params )
121
+ if err != nil {
122
+ return fmt .Errorf ("failed to create request: %w" , err )
123
+ }
124
+
125
+ res , err := client .Do (req )
126
+ if err != nil {
127
+ return fmt .Errorf ("failed to send request: %w" , err )
128
+ }
129
+
130
+ defer func () {
131
+ if cErr := res .Body .Close (); cErr != nil && returnErr == nil {
132
+ returnErr = cErr
133
+ }
134
+ }()
135
+
136
+ err = checkResponseCode (res , params .ExpectedResponseCode )
137
+ if err != nil {
138
+ return err
139
+ }
140
+
141
+ if responseBody == nil {
142
+ return nil
143
+ }
144
+
145
+ return json .NewDecoder (res .Body ).Decode (responseBody )
146
+ }
147
+
117
148
func createRequest (params Params ) (* http.Request , error ) {
118
149
reader , err := convertToReader (params .Body )
119
150
if err != nil {
@@ -141,12 +172,12 @@ func createRequest(params Params) (*http.Request, error) {
141
172
return req , nil
142
173
}
143
174
144
- // Get is a convience wrapper for "Do" to execute GET requests
175
+ // Get is a convenience wrapper for "Do" to execute GET requests
145
176
func Get (url string , responseBody interface {}) error {
146
177
return Do (Params {Method : http .MethodGet , URL : url }, responseBody )
147
178
}
148
179
149
- // Post is a convience wrapper for "Do" to execute POST requests
180
+ // Post is a convenience wrapper for "Do" to execute POST requests
150
181
func Post (url string , requestBody interface {}, responseBody interface {}) error {
151
182
return Do (Params {Method : http .MethodPost , URL : url , Body : requestBody }, responseBody )
152
183
}
0 commit comments