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

Refactor azcosmos client constructor #15705

Merged
merged 2 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 3 additions & 7 deletions sdk/data/azcosmos/cosmos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package azcosmos
import (
"context"
"errors"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
)

// A CosmosClient is used to interact with the Azure Cosmos DB database service.
Expand All @@ -19,20 +17,18 @@ type CosmosClient struct {
options *CosmosClientOptions
}

// NewCosmosClient creates a new instance of CosmosClient with the specified values. It uses the default pipeline configuration.
// NewClientWithSharedKey creates a new instance of CosmosClient with the specified values. It uses the default pipeline configuration.
// endpoint - The cosmos service endpoint to use.
// cred - The credential used to authenticate with the cosmos service.
// options - Optional CosmosClient options. Pass nil to accept default values.
func NewCosmosClient(endpoint string, cred azcore.Credential, options *CosmosClientOptions) (*CosmosClient, error) {
func NewClientWithSharedKey(endpoint string, cred *SharedKeyCredential, options *CosmosClientOptions) (*CosmosClient, error) {
if options == nil {
options = &CosmosClientOptions{}
}

connection := newCosmosClientConnection(endpoint, cred, options)

c, _ := cred.(*SharedKeyCredential)

return &CosmosClient{Endpoint: endpoint, connection: connection, cred: c, options: options}, nil
return &CosmosClient{Endpoint: endpoint, connection: connection, cred: cred, options: options}, nil
}

// GetCosmosDatabase returns a CosmosDatabase object.
Expand Down
5 changes: 2 additions & 3 deletions sdk/data/azcosmos/cosmos_client_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
azruntime "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
)
Expand All @@ -22,15 +21,15 @@ type cosmosClientConnection struct {

// newConnection creates an instance of the connection type with the specified endpoint.
// Pass nil to accept the default options; this is the same as passing a zero-value options.
func newCosmosClientConnection(endpoint string, cred azcore.Credential, options *CosmosClientOptions) *cosmosClientConnection {
func newCosmosClientConnection(endpoint string, cred *SharedKeyCredential, options *CosmosClientOptions) *cosmosClientConnection {
policies := []policy.Policy{
azruntime.NewTelemetryPolicy("azcosmos", serviceLibVersion, &options.Telemetry),
}
policies = append(policies, options.PerCallPolicies...)
policies = append(policies, azruntime.NewRetryPolicy(&options.Retry))
policies = append(policies, options.PerRetryPolicies...)
policies = append(policies, options.getSDKInternalPolicies()...)
policies = append(policies, cred.NewAuthenticationPolicy(azruntime.AuthenticationOptions{}))
policies = append(policies, newSharedKeyCredPolicy(cred))
policies = append(policies, azruntime.NewLogPolicy(&options.Logging))
return &cosmosClientConnection{endpoint: endpoint, Pipeline: azruntime.NewPipeline(options.HTTPClient, policies...)}
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/data/azcosmos/emulator_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newEmulatorTests(t *testing.T) *emulatorTests {

func (e *emulatorTests) getClient(t *testing.T) *CosmosClient {
cred, _ := NewSharedKeyCredential(e.key)
client, err := NewCosmosClient(e.host, cred, nil)
client, err := NewClientWithSharedKey(e.host, cred, nil)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
Expand Down
7 changes: 1 addition & 6 deletions sdk/data/azcosmos/shared_key_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/internal/log"
)

Expand Down Expand Up @@ -92,18 +91,14 @@ type sharedKeyCredPolicy struct {
cred *SharedKeyCredential
}

func newSharedKeyCredPolicy(cred *SharedKeyCredential, opts runtime.AuthenticationOptions) *sharedKeyCredPolicy {
func newSharedKeyCredPolicy(cred *SharedKeyCredential) *sharedKeyCredPolicy {
s := &sharedKeyCredPolicy{
cred: cred,
}

return s
}

func (c *SharedKeyCredential) NewAuthenticationPolicy(options runtime.AuthenticationOptions) policy.Policy {
return newSharedKeyCredPolicy(c, options)
}

func (s *sharedKeyCredPolicy) Do(req *policy.Request) (*http.Response, error) {
// Add a x-ms-date header if it doesn't already exist
if d := req.Raw().Header.Get(headerXmsDate); d == "" {
Expand Down