forked from dghubble/sling
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
6 changed files
with
94 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package sling | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type HttpWrapper struct { | ||
http *http.Client | ||
} | ||
|
||
func (h *HttpWrapper) Do(req *http.Request) (*http.Response, []byte, error) { | ||
resp, err := h.http.Do(req) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
// when err is nil, resp contains a non-nil resp.Body which must be closed | ||
defer resp.Body.Close() | ||
|
||
// The default HTTP client's Transport may not | ||
// reuse HTTP/1.x "keep-alive" TCP connections if the Body is | ||
// not read to completion and closed. | ||
// See: https://golang.org/pkg/net/http/#Response | ||
defer io.Copy(io.Discard, resp.Body) | ||
rawData, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return resp, rawData, nil | ||
} | ||
|
||
func NewHttpWrapper(client *http.Client) *HttpWrapper { | ||
return &HttpWrapper{http: client} | ||
} |
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
Oops, something went wrong.