Skip to content

Commit

Permalink
Refactor client parameters. Enable setting header
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-kearney committed Oct 17, 2024
1 parent e151f76 commit 436dc3c
Showing 1 changed file with 100 additions and 7 deletions.
107 changes: 100 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
}
Expand Down

0 comments on commit 436dc3c

Please sign in to comment.