Skip to content

Commit

Permalink
Consolidate API version policy options (#19489)
Browse files Browse the repository at this point in the history
  • Loading branch information
chlowell authored Nov 4, 2022
1 parent d056df3 commit 96096fe
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion sdk/azcore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release History

## 1.2.0 (2022-11-03)
## 1.2.0 (2022-11-04)

### Features Added
* Added `ClientOptions.APIVersion` field, which overrides the default version a client
Expand Down
4 changes: 2 additions & 2 deletions sdk/azcore/arm/runtime/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func NewPipeline(module, version string, cred azcore.TokenCredential, plOpts azr
copy(perCall, plOpts.PerCall)
plOpts.PerCall = append(perCall, regPolicy)
}
if plOpts.APIVersionName == "" {
plOpts.APIVersionName = "api-version"
if plOpts.APIVersion.Name == "" {
plOpts.APIVersion.Name = "api-version"
}
return azruntime.NewPipeline(module, version, plOpts, &options.ClientOptions), nil
}
Expand Down
5 changes: 2 additions & 3 deletions sdk/azcore/runtime/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
// PipelineOptions contains Pipeline options for SDK developers
type PipelineOptions struct {
AllowedHeaders, AllowedQueryParameters []string
APIVersionLocation APIVersionLocation
APIVersionName string
APIVersion APIVersionOptions
PerCall, PerRetry []policy.Policy
}

Expand Down Expand Up @@ -49,7 +48,7 @@ func NewPipeline(module, version string, plOpts PipelineOptions, options *policy
// is populated with the final response (some policies might mutate the response)
policies := []policy.Policy{policyFunc(includeResponsePolicy)}
if cp.APIVersion != "" {
policies = append(policies, newAPIVersionPolicy(plOpts.APIVersionLocation, plOpts.APIVersionName, cp.APIVersion))
policies = append(policies, newAPIVersionPolicy(cp.APIVersion, &plOpts.APIVersion))
}
if !cp.Telemetry.Disabled {
policies = append(policies, NewTelemetryPolicy(module, version, &cp.Telemetry))
Expand Down
22 changes: 16 additions & 6 deletions sdk/azcore/runtime/policy_api_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
)

// APIVersionOptions contains options for API versions
type APIVersionOptions struct {
// Location indicates where to set the version on a request, for example in a header or query param
Location APIVersionLocation
// Name is the name of the header or query parameter, for example "api-version"
Name string
}

// APIVersionLocation indicates which part of a request identifies the service version
type APIVersionLocation int

Expand All @@ -24,11 +32,13 @@ const (
APIVersionLocationHeader = 1
)

// newAPIVersionPolicy constructs an APIVersionPolicy. name is the name of the query parameter or header and
// version is its value. If version is "", Do will be a no-op. If version isn't empty and name is empty,
// Do will return an error.
func newAPIVersionPolicy(location APIVersionLocation, name, version string) *apiVersionPolicy {
return &apiVersionPolicy{location: location, name: name, version: version}
// newAPIVersionPolicy constructs an APIVersionPolicy. If version is "", Do will be a no-op. If version
// isn't empty and opts.Name is empty, Do will return an error.
func newAPIVersionPolicy(version string, opts *APIVersionOptions) *apiVersionPolicy {
if opts == nil {
opts = &APIVersionOptions{}
}
return &apiVersionPolicy{location: opts.Location, name: opts.Name, version: version}
}

// apiVersionPolicy enables users to set the API version of every request a client sends.
Expand All @@ -47,7 +57,7 @@ type apiVersionPolicy struct {
func (a *apiVersionPolicy) Do(req *policy.Request) (*http.Response, error) {
if a.version != "" {
if a.name == "" {
// user set ClientOptions.APIVersion but the client ctor didn't set PipelineOptions.APIVersionLocation
// user set ClientOptions.APIVersion but the client ctor didn't set PipelineOptions.APIVersionOptions
return nil, errors.New("this client doesn't support overriding its API version")
}
switch a.location {
Expand Down
4 changes: 2 additions & 2 deletions sdk/azcore/runtime/policy_api_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestAPIVersionPolicy(t *testing.T) {
if header {
location = APIVersionLocationHeader
}
p := newAPIVersionPolicy(location, name, version)
p := newAPIVersionPolicy(version, &APIVersionOptions{Location: location, Name: name})
pl := newTestPipeline(&policy.ClientOptions{Transport: srv, PerCallPolicies: []policy.Policy{p}})

// when the value isn't set, the policy should set it
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestAPIVersionPolicy(t *testing.T) {
{location: 2, version: version, err: true},
} {
t.Run("no-op", func(t *testing.T) {
p := newAPIVersionPolicy(test.location, test.name, test.version)
p := newAPIVersionPolicy(test.version, &APIVersionOptions{Location: test.location, Name: test.name})
pl := newTestPipeline(&policy.ClientOptions{Transport: srv, PerCallPolicies: []policy.Policy{p}})
req, err := NewRequest(context.Background(), http.MethodGet, srv.URL())
require.NoError(t, err)
Expand Down

0 comments on commit 96096fe

Please sign in to comment.