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

Base client interoperability #876

Merged
merged 2 commits into from
Feb 8, 2024
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
43 changes: 43 additions & 0 deletions sdk/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,49 @@ func NewClient(baseUri string, serviceName, apiVersion string) *Client {
}
}

// SetAuthorizer configures the request authorizer for the client
func (c *Client) SetAuthorizer(authorizer auth.Authorizer) {
c.Authorizer = authorizer
}

// SetUserAgent configures the user agent to be included in requests
func (c *Client) SetUserAgent(userAgent string) {
c.UserAgent = userAgent
}

// GetUserAgent retrieves the configured user agent for the client
func (c *Client) GetUserAgent() string {
return c.UserAgent
}

// AppendRequestMiddleware appends a request middleware function for the client
func (c *Client) AppendRequestMiddleware(f RequestMiddleware) {
if c.RequestMiddlewares == nil {
m := make([]RequestMiddleware, 0)
c.RequestMiddlewares = &m
}
*c.RequestMiddlewares = append(*c.RequestMiddlewares, f)
}

// ClearRequestMiddlewares removes all request middleware functions for the client
func (c *Client) ClearRequestMiddlewares() {
c.RequestMiddlewares = nil
}

// AppendResponseMiddleware appends a response middleware function for the client
func (c *Client) AppendResponseMiddleware(f ResponseMiddleware) {
if c.ResponseMiddlewares == nil {
m := make([]ResponseMiddleware, 0)
c.ResponseMiddlewares = &m
}
*c.ResponseMiddlewares = append(*c.ResponseMiddlewares, f)
}

// ClearResponseMiddlewares removes all response middleware functions for the client
func (c *Client) ClearResponseMiddlewares() {
c.ResponseMiddlewares = nil
}

// NewRequest configures a new *Request
func (c *Client) NewRequest(ctx context.Context, input RequestOptions) (*Request, error) {
req := (&http.Request{}).WithContext(ctx)
Expand Down
6 changes: 3 additions & 3 deletions sdk/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestAccClient(t *testing.T) {
c := &testClient{
Client: NewClient(*endpoint, "example", "2020-01-01"),
}
c.Authorizer = conn.Authorizer
c.SetAuthorizer(conn.Authorizer)

path := fmt.Sprintf("/v1.0/servicePrincipals/%s", conn.Claims.ObjectId)
reqOpts := RequestOptions{
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestAccClient_Paged(t *testing.T) {
c := &testClient{
Client: NewClient(*endpoint, "example", "2020-01-01"),
}
c.Authorizer = conn.Authorizer
c.SetAuthorizer(conn.Authorizer)

path := "/v1.0/applications"
reqOpts := RequestOptions{
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestAccClient_CustomPaged(t *testing.T) {
c := &testClient{
Client: NewClient(*endpoint, "example", "2020-01-01"),
}
c.Authorizer = conn.Authorizer
c.SetAuthorizer(conn.Authorizer)

path := "/v1.0/applications"
reqOpts := RequestOptions{
Expand Down
4 changes: 3 additions & 1 deletion sdk/client/dataplane/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"github.com/hashicorp/go-azure-sdk/sdk/client"
)

var _ client.BaseClient = &Client{}

type Client struct {
Client *client.Client
*client.Client

// ApiVersion specifies the version of the API being used, which (by design) will be consistent across a client
// as we intentionally split out multiple API Versions into different clients, rather than using composite API
Expand Down
9 changes: 3 additions & 6 deletions sdk/client/dataplane/storage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (
"fmt"
"net/url"

"github.com/hashicorp/go-azure-sdk/sdk/auth"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/client/dataplane"
)

var _ client.BaseClient = &BaseClient{}

var storageDefaultRetryFunctions = []client.RequestRetryFunc{
// TODO: stuff n tings
}

type BaseClient struct {
Client *dataplane.Client
*dataplane.Client
}

func NewBaseClient(baseUri string, componentName, apiVersion string) (*BaseClient, error) {
Expand Down Expand Up @@ -78,7 +79,3 @@ func (c *BaseClient) Execute(ctx context.Context, req *client.Request) (*client.
func (c *BaseClient) ExecutePaged(ctx context.Context, req *client.Request) (*client.Response, error) {
return c.Client.ExecutePaged(ctx, req)
}

func (c *BaseClient) WithAuthorizer(auth auth.Authorizer) {
c.Client.Client.Authorizer = auth
}
22 changes: 22 additions & 0 deletions sdk/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"net/http"

"github.com/hashicorp/go-azure-sdk/sdk/auth"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
)

Expand All @@ -19,6 +20,27 @@ type BaseClient interface {

// NewRequest constructs a *Request that can be passed to Execute or ExecutePaged
NewRequest(ctx context.Context, input RequestOptions) (*Request, error)

// SetAuthorizer configures the request authorizer for the client
SetAuthorizer(auth.Authorizer)

// SetUserAgent configures the user agent to be included in requests
SetUserAgent(string)

// GetUserAgent retrieves the configured user agent for the client
GetUserAgent() string

// AppendRequestMiddleware appends a request middleware function for the client
AppendRequestMiddleware(RequestMiddleware)

// ClearRequestMiddlewares removes all request middleware functions for the client
ClearRequestMiddlewares()

// AppendResponseMiddleware appends a response middleware function for the client
AppendResponseMiddleware(ResponseMiddleware)

// ClearResponseMiddlewares removes all response middleware functions for the client
ClearResponseMiddlewares()
}

// RequestRetryFunc is a function that determines whether an HTTP request has failed due to eventual consistency and should be retried
Expand Down
Loading