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

Enable optional headers to be passed into the authentication parameters #68

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Changes from 2 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
108 changes: 101 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
package sdk

import (
"net/http"

"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/pkg/errors"

Expand All @@ -11,22 +14,113 @@ import (
"github.com/tkhq/go-sdk/pkg/store/local"
)

const DefaultClientVersion = "go-sdk"

type config struct {
apiKey *apikey.Key
clientVersion string
registry strfmt.Registry
transportConfig *client.TransportConfig
}

// OptionFunc defines a function which sets configuration options for a Client.
type OptionFunc func(c *config) error

// WithClientVersion overrides the client version used for this API client.
func WithClientVersion(clientVersion string) OptionFunc {
return func(c *config) error {
c.clientVersion = clientVersion
return nil
}
}

// WithRegistry sets the registry formats used for this API client.
func WithRegistry(registry strfmt.Registry) OptionFunc {
return func(c *config) error {
c.registry = registry
return nil
}
}

// WithTransportConfig sets the TransportConfig used for this API client.
func WithTransportConfig(transportConfig client.TransportConfig) OptionFunc {
return func(c *config) error {
c.transportConfig = &transportConfig
return nil
}
}

// WithAPIKey sets the API key used for this API client.
// Users would normally use WithAPIKeyName. This offers a lower-level custom API
// key.
func WithAPIKey(apiKey *apikey.Key) OptionFunc {
return func(c *config) error {
c.apiKey = apiKey
return nil
}
}

// WithAPIKeyName sets the API key to the key loaded from the local keystore
// with the provided name.
func WithAPIKeyName(keyname string) OptionFunc {
return func(c *config) error {
apiKey, err := local.New[*apikey.Key]().Load(keyname)
if err != nil {
return errors.Wrap(err, "failed to load API key")
}
c.apiKey = apiKey
return nil
}
}

// New returns a new API Client with the given API key name from the default keystore.
func New(keyname string) (*Client, error) {
apiKey, err := local.New[*apikey.Key, apikey.Metadata]().Load(keyname)
if err != nil {
return nil, errors.Wrap(err, "failed to load API key")
func New(options ...OptionFunc) (*Client, error) {
c := &config{
clientVersion: DefaultClientVersion,
transportConfig: client.DefaultTransportConfig(),
}

for _, o := range options {
o(c)
}

// Create transport and client
transport := httptransport.New(
c.transportConfig.Host,
c.transportConfig.BasePath,
c.transportConfig.Schemes,
)

// Add client version header
transport.Transport = SetClientVersion(transport.Transport, c.clientVersion)
Ulexus marked this conversation as resolved.
Show resolved Hide resolved

return &Client{
Client: client.NewHTTPClient(nil),
Authenticator: &Authenticator{Key: apiKey},
APIKey: apiKey,
Client: client.New(transport, c.registry),
Authenticator: &Authenticator{Key: c.apiKey},
APIKey: c.apiKey,
}, nil
}

func SetClientVersion(inner http.RoundTripper, clientVersion string) http.RoundTripper {
return &addClientVersion{
inner: inner,
Version: clientVersion,
}
}

type addClientVersion struct {
inner http.RoundTripper
Version string
}

func (acv *addClientVersion) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("X-Client-Version", acv.Version)
return acv.inner.RoundTrip(r)
}

// NewHTTPClient returns a new base HTTP API client.
// Most users will call New() instead.
// Deprecated: Use New(WithRegistry(formats)) instead
func NewHTTPClient(formats strfmt.Registry) *client.TurnkeyAPI {
return client.NewHTTPClient(formats)
}
Expand Down
Loading