Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http integration: StdlibTransport wrapper #8

Merged
merged 3 commits into from
Aug 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions stdlibwrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package http

import "net/http"

// StdlibTransport is an adapter for integrating net/http dependend code.
// It looks like an http.RoundTripper but uses this fork internally.
type StdlibTransport struct {
Transport
}

// RoundTrip implements the http.RoundTripper interface.
func (txp *StdlibTransport) RoundTrip(stdReq *http.Request) (*http.Response, error) {
req, err := NewRequest(stdReq.Method, stdReq.URL.String(), stdReq.Body)
if err != nil {
return nil, err
}
req.Header = Header(stdReq.Header)
resp, err := txp.Transport.RoundTrip(req)
if err != nil {
return nil, err
}
stdResp := &http.Response{
Status: resp.Status,
StatusCode: resp.StatusCode,
Proto: resp.Proto,
ProtoMinor: resp.ProtoMinor,
ProtoMajor: resp.ProtoMajor,
Header: http.Header(resp.Header),
Body: resp.Body,
ContentLength: resp.ContentLength,
TransferEncoding: resp.TransferEncoding,
Close: resp.Close,
Uncompressed: resp.Uncompressed,
Trailer: http.Header(resp.Trailer),
Request: stdReq,
TLS: resp.TLS,
}
return stdResp, nil
}