Skip to content

Commit

Permalink
Remove Response type (#15294)
Browse files Browse the repository at this point in the history
Just return an *http.Response instead.
Changed Response methods to functions.
  • Loading branch information
jhendrixMSFT authored Aug 13, 2021
1 parent 9cfdc25 commit 18d4a3b
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 237 deletions.
12 changes: 6 additions & 6 deletions sdk/azcore/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ type Policy interface {
// Do applies the policy to the specified Request. When implementing a Policy, mutate the
// request before calling req.Next() to move on to the next policy, and respond to the result
// before returning to the caller.
Do(req *Request) (*Response, error)
Do(req *Request) (*http.Response, error)
}

// policyFunc is a type that implements the Policy interface.
// Use this type when implementing a stateless policy as a first-class function.
type policyFunc func(*Request) (*Response, error)
type policyFunc func(*Request) (*http.Response, error)

// Do implements the Policy interface on PolicyFunc.
func (pf policyFunc) Do(req *Request) (*Response, error) {
func (pf policyFunc) Do(req *Request) (*http.Response, error) {
return pf(req)
}

Expand All @@ -50,7 +50,7 @@ type transportPolicy struct {
trans Transporter
}

func (tp transportPolicy) Do(req *Request) (*Response, error) {
func (tp transportPolicy) Do(req *Request) (*http.Response, error) {
resp, err := tp.trans.Do(req.Request)
if err != nil {
return nil, err
Expand All @@ -59,7 +59,7 @@ func (tp transportPolicy) Do(req *Request) (*Response, error) {
// this ensures the retry policy will retry the request
return nil, errors.New("received nil response")
}
return &Response{Response: resp}, nil
return resp, nil
}

// Pipeline represents a primitive for sending HTTP requests and receiving responses.
Expand All @@ -84,7 +84,7 @@ func NewPipeline(transport Transporter, policies ...Policy) Pipeline {
// Do is called for each and every HTTP request. It passes the request through all
// the Policy objects (which can transform the Request's URL/query parameters/headers)
// and ultimately sends the transformed HTTP request over the network.
func (p Pipeline) Do(req *Request) (*Response, error) {
func (p Pipeline) Do(req *Request) (*http.Response, error) {
if err := req.valid(); err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion sdk/azcore/policy_anonymous_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

package azcore

import "net/http"

func anonCredAuthPolicyFunc(AuthenticationOptions) Policy {
return policyFunc(anonCredPolicyFunc)
}

func anonCredPolicyFunc(req *Request) (*Response, error) {
func anonCredPolicyFunc(req *Request) (*http.Response, error) {
return req.Next()
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/azcore/policy_body_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// bodyDownloadPolicy creates a policy object that downloads the response's body to a []byte.
func bodyDownloadPolicy(req *Request) (*Response, error) {
func bodyDownloadPolicy(req *Request) (*http.Response, error) {
resp, err := req.Next()
if err != nil {
return resp, err
Expand Down
20 changes: 10 additions & 10 deletions sdk/azcore/policy_body_download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestDownloadBody(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
payload, err := resp.Payload()
payload, err := Payload(resp)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestSkipBodyDownload(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
payload, err := resp.Payload()
payload, err := Payload(resp)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -80,7 +80,7 @@ func TestDownloadBodyFail(t *testing.T) {
if err == nil {
t.Fatal("unexpected nil error")
}
payload, err := resp.Payload()
payload, err := Payload(resp)
if err == nil {
t.Fatalf("expected an error")
}
Expand All @@ -106,7 +106,7 @@ func TestDownloadBodyWithRetryGet(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
payload, err := resp.Payload()
payload, err := Payload(resp)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestDownloadBodyWithRetryDelete(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
payload, err := resp.Payload()
payload, err := Payload(resp)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestDownloadBodyWithRetryPut(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
payload, err := resp.Payload()
payload, err := Payload(resp)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestDownloadBodyWithRetryPatch(t *testing.T) {
if _, ok := err.(*bodyDownloadError); !ok {
t.Fatal("expected *bodyDownloadError type")
}
payload, err := resp.Payload()
payload, err := Payload(resp)
if err == nil {
t.Fatalf("expected an error")
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestDownloadBodyWithRetryPost(t *testing.T) {
if err == nil {
t.Fatal("unexpected nil error")
}
payload, err := resp.Payload()
payload, err := Payload(resp)
if err == nil {
t.Fatalf("expected an error")
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestSkipBodyDownloadWith400(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
payload, err := resp.Payload()
payload, err := Payload(resp)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestReadBodyAfterSeek(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
payload, err := resp.Payload()
payload, err := Payload(resp)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/azcore/policy_http_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type ctxWithHTTPHeader struct{}

// newHTTPHeaderPolicy creates a policy object that adds custom HTTP headers to a request
func httpHeaderPolicy(req *Request) (*Response, error) {
func httpHeaderPolicy(req *Request) (*http.Response, error) {
// check if any custom HTTP headers have been specified
if header := req.Context().Value(ctxWithHTTPHeader{}); header != nil {
for k, v := range header.(http.Header) {
Expand Down
5 changes: 3 additions & 2 deletions sdk/azcore/policy_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package azcore
import (
"bytes"
"fmt"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -42,7 +43,7 @@ type logPolicyOpValues struct {
start time.Time
}

func (p *logPolicy) Do(req *Request) (*Response, error) {
func (p *logPolicy) Do(req *Request) (*http.Response, error) {
// Get the per-operation values. These are saved in the Message's map so that they persist across each retry calling into this policy object.
var opValues logPolicyOpValues
if req.OperationValue(&opValues); opValues.start.IsZero() {
Expand Down Expand Up @@ -88,7 +89,7 @@ func (p *logPolicy) Do(req *Request) (*Response, error) {
// skip frames runtime.Callers() and runtime.StackTrace()
b.WriteString(diag.StackTrace(2, StackFrameCount))
} else if p.options.IncludeBody {
err = response.writeBody(b)
err = writeBody(response, b)
}
log.Write(log.Response, b.String())
}
Expand Down
8 changes: 4 additions & 4 deletions sdk/azcore/policy_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type retryPolicy struct {
options RetryOptions
}

func (p *retryPolicy) Do(req *Request) (resp *Response, err error) {
func (p *retryPolicy) Do(req *Request) (resp *http.Response, err error) {
options := p.options
// check if the retry options have been overridden for this call
if override := req.Context().Value(ctxWithRetryOptionsKey{}); override != nil {
Expand Down Expand Up @@ -170,7 +170,7 @@ func (p *retryPolicy) Do(req *Request) (resp *Response, err error) {
log.Writef(log.RetryPolicy, "error %v", err)
}

if err == nil && !resp.HasStatusCode(options.StatusCodes...) {
if err == nil && !HasStatusCode(resp, options.StatusCodes...) {
// if there is no error and the response code isn't in the list of retry codes then we're done.
return
} else if ctxErr := req.Context().Err(); ctxErr != nil {
Expand All @@ -195,10 +195,10 @@ func (p *retryPolicy) Do(req *Request) (resp *Response, err error) {
}

// drain before retrying so nothing is leaked
resp.Drain()
Drain(resp)

// use the delay from retry-after if available
delay := resp.retryAfter()
delay := RetryAfter(resp)
if delay <= 0 {
delay = options.calcDelay(try)
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/azcore/policy_retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestRetryPolicyFailOnStatusCodeRespBodyPreserved(t *testing.T) {
srv.SetResponse(mock.WithStatusCode(http.StatusInternalServerError), mock.WithBody([]byte(respBody)))
// add a per-request policy that reads and restores the request body.
// this is to simulate how something like httputil.DumpRequest works.
pl := NewPipeline(srv, policyFunc(func(r *Request) (*Response, error) {
pl := NewPipeline(srv, policyFunc(func(r *Request) (*http.Response, error) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
Expand Down
3 changes: 2 additions & 1 deletion sdk/azcore/policy_telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package azcore
import (
"bytes"
"fmt"
"net/http"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -64,7 +65,7 @@ func NewTelemetryPolicy(o *TelemetryOptions) Policy {
return &tp
}

func (p telemetryPolicy) Do(req *Request) (*Response, error) {
func (p telemetryPolicy) Do(req *Request) (*http.Response, error) {
if p.telemetryValue == "" {
return req.Next()
}
Expand Down
Loading

0 comments on commit 18d4a3b

Please sign in to comment.