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

Don't implement stateless polices as first-class funcs #13509

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion sdk/azcore/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewPipeline(transport Transport, policies ...Policy) Pipeline {
transport = defaultHTTPClient
}
// transport policy must always be the last in the slice
policies = append(policies, newHTTPHeaderPolicy(), newBodyDownloadPolicy(), transportPolicy{trans: transport})
policies = append(policies, PolicyFunc(httpHeaderPolicy), PolicyFunc(bodyDownloadPolicy), transportPolicy{trans: transport})
return Pipeline{
policies: policies,
}
Expand Down
14 changes: 9 additions & 5 deletions sdk/azcore/policy_anonymous_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

package azcore

func anonCredAuthPolicyFunc(AuthenticationPolicyOptions) Policy {
return PolicyFunc(anonCredPolicyFunc)
}

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

// AnonymousCredential is for use with HTTP(S) requests that read public resource
// or for use with Shared Access Signatures (SAS).
func AnonymousCredential() Credential {
return credentialFunc(func(AuthenticationPolicyOptions) Policy {
return PolicyFunc(func(req *Request) (*Response, error) {
return req.Next()
})
})
return credentialFunc(anonCredAuthPolicyFunc)
}
40 changes: 19 additions & 21 deletions sdk/azcore/policy_body_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,26 @@ import (
"strings"
)

// newBodyDownloadPolicy creates a policy object that downloads the response's body to a []byte.
func newBodyDownloadPolicy() Policy {
return PolicyFunc(func(req *Request) (*Response, error) {
resp, err := req.Next()
if err != nil {
return resp, err
}
var opValues bodyDownloadPolicyOpValues
// don't skip downloading error response bodies
if req.OperationValue(&opValues); opValues.skip && resp.StatusCode < 400 {
return resp, err
}
// Either bodyDownloadPolicyOpValues was not specified (so skip is false)
// or it was specified and skip is false: don't skip downloading the body
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return resp, newBodyDownloadError(err, req)
}
resp.Body = &nopClosingBytesReader{s: b}
// bodyDownloadPolicy creates a policy object that downloads the response's body to a []byte.
func bodyDownloadPolicy(req *Request) (*Response, error) {
resp, err := req.Next()
if err != nil {
return resp, err
})
}
var opValues bodyDownloadPolicyOpValues
// don't skip downloading error response bodies
if req.OperationValue(&opValues); opValues.skip && resp.StatusCode < 400 {
return resp, err
}
// Either bodyDownloadPolicyOpValues was not specified (so skip is false)
// or it was specified and skip is false: don't skip downloading the body
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return resp, newBodyDownloadError(err, req)
}
resp.Body = &nopClosingBytesReader{s: b}
return resp, err
}

type bodyDownloadError struct {
Expand Down
26 changes: 12 additions & 14 deletions sdk/azcore/policy_http_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ import (
type ctxWithHTTPHeader struct{}

// newHTTPHeaderPolicy creates a policy object that adds custom HTTP headers to a request
func newHTTPHeaderPolicy() Policy {
return PolicyFunc(func(req *Request) (*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) {
// use Set to replace any existing value
// it also canonicalizes the header key
req.Header.Set(k, v[0])
// add any remaining values
for i := 1; i < len(v); i++ {
req.Header.Add(k, v[i])
}
func httpHeaderPolicy(req *Request) (*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) {
// use Set to replace any existing value
// it also canonicalizes the header key
req.Header.Set(k, v[0])
// add any remaining values
for i := 1; i < len(v); i++ {
req.Header.Add(k, v[i])
}
}
return req.Next()
})
}
return req.Next()
}

// WithHTTPHeader adds the specified http.Header to the parent context.
Expand Down