forked from astarte-platform/astarte-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
245 lines (217 loc) · 7.16 KB
/
client.go
File metadata and controls
245 lines (217 loc) · 7.16 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright © 2023 SECO Mind srl
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package newclient
import (
"net/http"
"net/url"
"time"
"github.com/astarte-platform/astarte-go/misc"
)
type Client struct {
baseURL *url.URL
appEngineURL *url.URL
housekeepingURL *url.URL
pairingURL *url.URL
realmManagementURL *url.URL
userAgent string
httpClient *http.Client
token string
}
type clientOption = func(c *Client) error
// Finally, generics (actually, type constraints)
type privateKeyProvider interface {
string | []byte
}
// The New function creates a new Astarte API client.
// If no options are specified, the following is assumed:
// - standard Astarte URL hierarchy
// - standard HTTP client
// - no JWT token (no call will be authorized)
// - "astarte-go" as user agent
// A production-ready client may be created using e.g.:
// `client.New(client.WithBaseUrl("api.your-astarte.org"), client.WithToken("YOUR_JWT_TOKEN"))``
func New(options ...clientOption) (*Client, error) {
// We start with a client with bare zero-valued fields
c := &Client{}
// Then we modify it according to user-provided options...
for _, f := range options {
err := f(c)
if err != nil {
return c, err
}
}
// ... and check if the result is valid
if err := validate(c); err != nil {
return c, err
}
// Finally, we add just a sprinkle of defaults and a new Client is born!
return setDefaults(c), nil
}
// The WithAppengineURL function allows to specify an
// AppEngine URL different from the standard one (e.g. http://localhost:4000).
// This is not recommendend in production.
func WithAppengineURL(appEngineURL string) clientOption {
return func(c *Client) error {
appengine, err := url.Parse(appEngineURL)
if err != nil {
return err
}
c.appEngineURL = appengine
return nil
}
}
// The WithHousekeepingURL function allows to specify an
// Housekeeping URL different from the standard one (e.g. http://localhost:4001).
// This is not recommendend in production.
func WithHousekeepingURL(housekeepingURL string) clientOption {
return func(c *Client) error {
housekeeping, err := url.Parse(housekeepingURL)
if err != nil {
return err
}
c.housekeepingURL = housekeeping
return nil
}
}
// The WithPairingURL function allows to specify an
// Pairing URL different from the standard one (e.g. http://localhost:4002).
// This is not recommendend in production.
func WithPairingURL(pairingURL string) clientOption {
return func(c *Client) error {
// check that it's a valid URL
pairing, err := url.Parse(pairingURL)
if err != nil {
return err
}
c.pairingURL = pairing
return nil
}
}
// The WithRealmManagementURL function allows to specify an
// RealmManagement URL different from the standard one (e.g. http://localhost:4003).
// This is not recommendend in production.
func WithRealmManagementURL(realmManagementURL string) clientOption {
return func(c *Client) error {
realmManagement, err := url.Parse(realmManagementURL)
if err != nil {
return err
}
c.realmManagementURL = realmManagement
return nil
}
}
// The WithBaseURL function allows to specify the Astarte
// base URL (e.g. api.your-astarte.org)
func WithBaseURL(baseURL string) clientOption {
return func(c *Client) error {
base, err := url.Parse(baseURL)
if err != nil {
return err
}
c.baseURL = base
return nil
}
}
// The WithHTTPClient function allows to specify an httpClient
// with custom options, e.g. different timeout, or skipTLSVerify
func WithHTTPClient(httpClient *http.Client) clientOption {
return func(c *Client) error {
c.httpClient = httpClient
return nil
}
}
// The WithToken function allows to specify a JWT
// token that the client will use to interact with Astarte.
func WithToken(token string) clientOption {
return func(c *Client) error {
c.token = token
return nil
}
}
// The WithUserAgent function allows to specify the User Agent
// that the client will use when making http requests.
func WithUserAgent(userAgent string) clientOption {
return func(c *Client) error {
c.userAgent = userAgent
return nil
}
}
// The WithPrivateKey function allows to specify a realm private key,
// used internally to generate a valid JWT token to all Astarte APIs with no expiry.
// The client will use that token to interact with Astarte.
func WithPrivateKey[T privateKeyProvider](privateKey T) clientOption {
return WithPrivateKeyWithTTL(privateKey, 0)
}
// The WithPrivateKey function allows to specify a realm private key,
// used internally to generate a valid JWT token to all Astarte APIs with a specified expiry (in seconds).
// The client will use that token to interact with Astarte.
func WithPrivateKeyWithTTL[T privateKeyProvider](privateKey T, ttlSeconds int64) clientOption {
// Add all types
servicesAndClaims := map[misc.AstarteService][]string{
misc.AppEngine: {},
misc.Channels: {},
misc.Flow: {},
misc.Housekeeping: {},
misc.Pairing: {},
misc.RealmManagement: {},
}
return WithPrivateKeyWithClaimsWithTTL(privateKey, servicesAndClaims, 0)
}
// The WithPrivateKey function allows to specify a realm private key,
// used internally to generate a valid JWT token with a given set of Astarte claims and
// a specified expiry (in seconds).
// The client will use that token to interact with Astarte.
func WithPrivateKeyWithClaimsWithTTL[T privateKeyProvider](privateKey T, claims map[misc.AstarteService][]string, ttlSeconds int64) clientOption {
return func(c *Client) error {
// Golang I hate you so much
switch k := any(privateKey).(type) {
case string:
var err error
c.token, err = misc.GenerateAstarteJWTFromKeyFile(k, claims, ttlSeconds)
return err
case []byte:
var err error
c.token, err = misc.GenerateAstarteJWTFromPEMKey(k, claims, ttlSeconds)
return err
default:
return ErrNoPrivateKeyProvided
}
}
}
func validate(c *Client) error {
if c.baseURL != nil && (c.appEngineURL != nil || c.realmManagementURL != nil || c.housekeepingURL != nil || c.pairingURL != nil) {
return ErrConflictingUrls
}
if c.baseURL == nil && c.appEngineURL == nil && c.realmManagementURL == nil && c.housekeepingURL == nil && c.pairingURL == nil {
return ErrNoUrlsProvided
}
return nil
}
func setDefaults(c *Client) *Client {
if c.httpClient == nil {
c.httpClient = &http.Client{
Timeout: time.Second * 30,
}
}
if c.userAgent == "" {
c.userAgent = "astarte-go"
}
if c.baseURL != nil {
c.appEngineURL, _ = url.Parse(c.baseURL.String()+"/appengine")
c.housekeepingURL, _ = url.Parse(c.baseURL.String()+"/housekeeping")
c.pairingURL, _ = url.Parse(c.baseURL.String()+"/pairing")
c.realmManagementURL, _ = url.Parse(c.baseURL.String()+"/realmmanagement")
}
return c
}