-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
180 lines (144 loc) · 4.85 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Package sdk provides a Go SDK with which to interact with the Turnkey API service.
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"
"github.com/tkhq/go-sdk/pkg/api/client"
"github.com/tkhq/go-sdk/pkg/apikey"
"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(options ...OptionFunc) (*Client, error) {
c := &config{
clientVersion: DefaultClientVersion,
transportConfig: client.DefaultTransportConfig(),
}
for _, o := range options {
err := o(c)
if err != nil {
return nil, err
}
}
// 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)
return &Client{
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)
}
// Client provides a handle by which to interact with the Turnkey API.
type Client struct {
// Client is the base HTTP API Client.
Client *client.TurnkeyAPI
// Authenticator provides a client option authentication provider which should be attached to every API request as a clientOption.
Authenticator *Authenticator
// APIKey is the API key to be used for API request signing.
APIKey *apikey.Key
}
// DefaultOrganization returns the first organization found in the APIKey's set of organizations.
func (c *Client) DefaultOrganization() *string {
for _, o := range c.APIKey.Organizations {
return &o
}
return nil
}
// V0 returns the raw initial Turnkey API client.
// WARNING: this is a temporary API which requires a bit more work to use than the one which will be eventually offered.
func (c *Client) V0() *client.TurnkeyAPI {
return c.Client
}
// Authenticator provides a runtime.ClientAuthInfoWriter for use with the swagger API client.
type Authenticator struct {
// Key optionally overrides the globally-parsed APIKeypair with a custom key.
Key *apikey.Key
}
// AuthenticateRequest implements runtime.ClientAuthInfoWriter.
// It adds the X-Stamp header to the request based by generating the Stamp with the request body and API key.
func (auth *Authenticator) AuthenticateRequest(req runtime.ClientRequest, reg strfmt.Registry) (err error) { //nolint: revive
stamp, err := apikey.Stamp(req.GetBody(), auth.Key)
if err != nil {
return errors.Wrap(err, "failed to generate API stamp")
}
return req.SetHeaderParam("X-Stamp", stamp)
}