From 436dc3cfb06fd7456b1df92ebed6ff906a5f868e Mon Sep 17 00:00:00 2001 From: Jack Kearney Date: Thu, 17 Oct 2024 13:49:50 -0400 Subject: [PATCH] Refactor client parameters. Enable setting header --- client.go | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 100 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index 85e4932..947409b 100644 --- a/client.go +++ b/client.go @@ -2,7 +2,11 @@ package sdk import ( + "fmt" + "net/http" + "github.com/go-openapi/runtime" + httptransport "github.com/go-openapi/runtime/client" "github.com/go-openapi/strfmt" "github.com/pkg/errors" @@ -11,22 +15,111 @@ 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 +} + +type Optfn func(c *config) error + +// WithClientVersion overrides the client version used for this API client. +func WithClientVersion(clientVersion string) Optfn { + 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) Optfn { + return func(c *config) error { + c.registry = registry + return nil + } +} + +// WithTransportConfig sets the TransportConfig used for this API client. +func WithTransportConfig(transportConfig client.TransportConfig) Optfn { + 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) Optfn { + 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) Optfn { + 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 ...Optfn) (*Client, error) { + c := &config{ + clientVersion: DefaultClientVersion, + transportConfig: client.DefaultTransportConfig(), + } + + for _, o := range options { + o(c) } + fmt.Println(c.transportConfig.Host) + + // create transport and client + transport := httptransport.New( + c.transportConfig.Host, + c.transportConfig.BasePath, + c.transportConfig.Schemes, + ) + transport.Transport = SetClientVersion(transport.Transport, c.clientVersion) 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) }