Skip to content

Commit 4589570

Browse files
committed
Code refactoring to switch to new API code structure
1 parent a78ee26 commit 4589570

File tree

1 file changed

+72
-51
lines changed

1 file changed

+72
-51
lines changed

internal/iot/client.go

+72-51
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"os"
2424

25-
"github.com/antihax/optional"
2625
"github.com/arduino/arduino-cloud-cli/config"
2726
iotclient "github.com/arduino/iot-client-go"
2827
"golang.org/x/oauth2"
@@ -118,21 +117,23 @@ func (cl *Client) DevicePassSet(ctx context.Context, id string) (*iotclient.Ardu
118117
}
119118

120119
// Fetch suggested password
121-
opts := &iotclient.DevicesV2PassGetOpts{SuggestedPassword: optional.NewBool(true)}
122-
pass, _, err := cl.api.DevicesV2PassApi.DevicesV2PassGet(ctx, id, opts)
120+
req := cl.api.DevicesV2PassApi.DevicesV2PassGet(ctx, id)
121+
req = req.SuggestedPassword(true)
122+
pass, _, err := cl.api.DevicesV2PassApi.DevicesV2PassGetExecute(req)
123123
if err != nil {
124124
err = fmt.Errorf("fetching device suggested password: %w", errorDetail(err))
125125
return nil, err
126126
}
127127

128128
// Set password to the suggested one
129-
p := iotclient.Devicev2Pass{Password: pass.SuggestedPassword}
130-
pass, _, err = cl.api.DevicesV2PassApi.DevicesV2PassSet(ctx, id, p)
129+
reqSet := cl.api.DevicesV2PassApi.DevicesV2PassSet(ctx, id)
130+
reqSet = reqSet.Devicev2Pass(iotclient.Devicev2Pass{Password: pass.SuggestedPassword})
131+
pass, _, err = cl.api.DevicesV2PassApi.DevicesV2PassSetExecute(reqSet)
131132
if err != nil {
132133
err = fmt.Errorf("setting device password: %w", errorDetail(err))
133134
return nil, err
134135
}
135-
return &pass, nil
136+
return pass, nil
136137
}
137138

138139
// DeviceDelete deletes the device corresponding to the passed ID
@@ -160,17 +161,16 @@ func (cl *Client) DeviceList(ctx context.Context, tags map[string]string) ([]iot
160161
return nil, err
161162
}
162163

163-
opts := &iotclient.DevicesV2ListOpts{}
164+
req := cl.api.DevicesV2Api.DevicesV2List(ctx)
164165
if tags != nil {
165166
t := make([]string, 0, len(tags))
166167
for key, val := range tags {
167168
// Use the 'key:value' format required from the backend
168169
t = append(t, key+":"+val)
169170
}
170-
opts.Tags = optional.NewInterface(t)
171+
req = req.Tags(t)
171172
}
172-
173-
devices, _, err := cl.api.DevicesV2Api.DevicesV2List(ctx, opts)
173+
devices, _, err := cl.api.DevicesV2Api.DevicesV2ListExecute(req)
174174
if err != nil {
175175
err = fmt.Errorf("listing devices: %w", errorDetail(err))
176176
return nil, err
@@ -186,12 +186,13 @@ func (cl *Client) DeviceShow(ctx context.Context, id string) (*iotclient.Arduino
186186
return nil, err
187187
}
188188

189-
dev, _, err := cl.api.DevicesV2Api.DevicesV2Show(ctx, id, nil)
189+
req := cl.api.DevicesV2Api.DevicesV2Show(ctx, id)
190+
dev, _, err := cl.api.DevicesV2Api.DevicesV2ShowExecute(req)
190191
if err != nil {
191192
err = fmt.Errorf("retrieving device, %w", errorDetail(err))
192193
return nil, err
193194
}
194-
return &dev, nil
195+
return dev, nil
195196
}
196197

197198
// DeviceOTA performs an OTA upload request to Arduino IoT Cloud, passing
@@ -202,11 +203,10 @@ func (cl *Client) DeviceOTA(ctx context.Context, id string, file *os.File, expir
202203
return err
203204
}
204205

205-
opt := &iotclient.DevicesV2OtaUploadOpts{
206-
ExpireInMins: optional.NewInt32(int32(expireMins)),
207-
Async: optional.NewBool(true),
208-
}
209-
resp, err := cl.api.DevicesV2OtaApi.DevicesV2OtaUpload(ctx, id, file, opt)
206+
req := cl.api.DevicesV2OtaApi.DevicesV2OtaUpload(ctx, id)
207+
req = req.ExpireInMins(int32(expireMins))
208+
req = req.Async(true)
209+
_, resp, err := cl.api.DevicesV2OtaApi.DevicesV2OtaUploadExecute(req)
210210
if err != nil {
211211
// 409 (Conflict) is the status code for an already existing OTA in progress for the same device. Handling it in a different way.
212212
if resp.StatusCode == 409 {
@@ -225,8 +225,9 @@ func (cl *Client) DeviceTagsCreate(ctx context.Context, id string, tags map[stri
225225
}
226226

227227
for key, val := range tags {
228-
t := iotclient.Tag{Key: key, Value: val}
229-
_, err := cl.api.DevicesV2TagsApi.DevicesV2TagsUpsert(ctx, id, t)
228+
req := cl.api.DevicesV2TagsApi.DevicesV2TagsUpsert(ctx, id)
229+
req = req.Tag(iotclient.Tag{Key: key, Value: val})
230+
_, err := cl.api.DevicesV2TagsApi.DevicesV2TagsUpsertExecute(req)
230231
if err != nil {
231232
err = fmt.Errorf("cannot create tag %s: %w", key, errorDetail(err))
232233
return err
@@ -244,7 +245,8 @@ func (cl *Client) DeviceTagsDelete(ctx context.Context, id string, keys []string
244245
}
245246

246247
for _, key := range keys {
247-
_, err := cl.api.DevicesV2TagsApi.DevicesV2TagsDelete(ctx, id, key)
248+
req := cl.api.DevicesV2TagsApi.DevicesV2TagsDelete(ctx, id, key)
249+
_, err := cl.api.DevicesV2TagsApi.DevicesV2TagsDeleteExecute(req)
248250
if err != nil {
249251
err = fmt.Errorf("cannot delete tag %s: %w", key, errorDetail(err))
250252
return err
@@ -261,7 +263,8 @@ func (cl *Client) LoraFrequencyPlansList(ctx context.Context) ([]iotclient.Ardui
261263
return nil, err
262264
}
263265

264-
freqs, _, err := cl.api.LoraFreqPlanV1Api.LoraFreqPlanV1List(ctx)
266+
req := cl.api.LoraFreqPlanV1Api.LoraFreqPlanV1List(ctx)
267+
freqs, _, err := cl.api.LoraFreqPlanV1Api.LoraFreqPlanV1ListExecute(req)
265268
if err != nil {
266269
err = fmt.Errorf("listing lora frequency plans: %w", errorDetail(err))
267270
return nil, err
@@ -278,12 +281,14 @@ func (cl *Client) CertificateCreate(ctx context.Context, id, csr string) (*iotcl
278281
}
279282

280283
cert := iotclient.CreateDevicesV2CertsPayload{
281-
Ca: "Arduino",
284+
Ca: toStringPointer("Arduino"),
282285
Csr: csr,
283286
Enabled: true,
284287
}
285288

286-
newCert, _, err := cl.api.DevicesV2CertsApi.DevicesV2CertsCreate(ctx, id, cert)
289+
req := cl.api.DevicesV2CertsApi.DevicesV2CertsCreate(ctx, id)
290+
req = req.CreateDevicesV2CertsPayload(cert)
291+
newCert, _, err := cl.api.DevicesV2CertsApi.DevicesV2CertsCreateExecute(req)
287292
if err != nil {
288293
err = fmt.Errorf("creating certificate, %w", errorDetail(err))
289294
return nil, err
@@ -299,12 +304,14 @@ func (cl *Client) ThingCreate(ctx context.Context, thing *iotclient.ThingCreate,
299304
return nil, err
300305
}
301306

302-
opt := &iotclient.ThingsV2CreateOpts{Force: optional.NewBool(force)}
303-
newThing, _, err := cl.api.ThingsV2Api.ThingsV2Create(ctx, *thing, opt)
307+
req := cl.api.ThingsV2Api.ThingsV2Create(ctx)
308+
req = req.ThingCreate(*thing)
309+
req = req.Force(force)
310+
newThing, _, err := cl.api.ThingsV2Api.ThingsV2CreateExecute(req)
304311
if err != nil {
305312
return nil, fmt.Errorf("%s: %w", "adding new thing", errorDetail(err))
306313
}
307-
return &newThing, nil
314+
return newThing, nil
308315
}
309316

310317
// ThingUpdate updates a thing on Arduino IoT Cloud.
@@ -314,8 +321,10 @@ func (cl *Client) ThingUpdate(ctx context.Context, id string, thing *iotclient.T
314321
return err
315322
}
316323

317-
opt := &iotclient.ThingsV2UpdateOpts{Force: optional.NewBool(force)}
318-
_, _, err = cl.api.ThingsV2Api.ThingsV2Update(ctx, id, *thing, opt)
324+
req := cl.api.ThingsV2Api.ThingsV2Update(ctx, id)
325+
req = req.Force(force)
326+
req = req.ThingUpdate(*thing)
327+
_, _, err = cl.api.ThingsV2Api.ThingsV2UpdateExecute(req)
319328
if err != nil {
320329
return fmt.Errorf("%s: %v", "updating thing", errorDetail(err))
321330
}
@@ -329,7 +338,8 @@ func (cl *Client) ThingDelete(ctx context.Context, id string) error {
329338
return err
330339
}
331340

332-
_, err = cl.api.ThingsV2Api.ThingsV2Delete(ctx, id, nil)
341+
req := cl.api.ThingsV2Api.ThingsV2Delete(ctx, id)
342+
_, err = cl.api.ThingsV2Api.ThingsV2DeleteExecute(req)
333343
if err != nil {
334344
err = fmt.Errorf("deleting thing: %w", errorDetail(err))
335345
return err
@@ -345,12 +355,12 @@ func (cl *Client) ThingShow(ctx context.Context, id string) (*iotclient.ArduinoT
345355
return nil, err
346356
}
347357

348-
thing, _, err := cl.api.ThingsV2Api.ThingsV2Show(ctx, id, nil)
358+
thing, _, err := cl.api.ThingsV2Api.ThingsV2ShowExecute(cl.api.ThingsV2Api.ThingsV2Show(ctx, id))
349359
if err != nil {
350360
err = fmt.Errorf("retrieving thing, %w", errorDetail(err))
351361
return nil, err
352362
}
353-
return &thing, nil
363+
return thing, nil
354364
}
355365

356366
// ThingList returns a list of things on Arduino IoT Cloud.
@@ -360,15 +370,14 @@ func (cl *Client) ThingList(ctx context.Context, ids []string, device *string, p
360370
return nil, err
361371
}
362372

363-
opts := &iotclient.ThingsV2ListOpts{}
364-
opts.ShowProperties = optional.NewBool(props)
365-
373+
req := cl.api.ThingsV2Api.ThingsV2List(ctx)
374+
req = req.ShowProperties(props)
366375
if ids != nil {
367-
opts.Ids = optional.NewInterface(ids)
376+
req = req.Ids(ids)
368377
}
369378

370379
if device != nil {
371-
opts.DeviceId = optional.NewString(*device)
380+
req = req.DeviceId(*device)
372381
}
373382

374383
if tags != nil {
@@ -377,10 +386,9 @@ func (cl *Client) ThingList(ctx context.Context, ids []string, device *string, p
377386
// Use the 'key:value' format required from the backend
378387
t = append(t, key+":"+val)
379388
}
380-
opts.Tags = optional.NewInterface(t)
389+
req = req.Tags(t)
381390
}
382-
383-
things, _, err := cl.api.ThingsV2Api.ThingsV2List(ctx, opts)
391+
things, _, err := cl.api.ThingsV2Api.ThingsV2ListExecute(req)
384392
if err != nil {
385393
err = fmt.Errorf("retrieving things, %w", errorDetail(err))
386394
return nil, err
@@ -396,8 +404,9 @@ func (cl *Client) ThingTagsCreate(ctx context.Context, id string, tags map[strin
396404
}
397405

398406
for key, val := range tags {
399-
t := iotclient.Tag{Key: key, Value: val}
400-
_, err := cl.api.ThingsV2TagsApi.ThingsV2TagsUpsert(ctx, id, t)
407+
req := cl.api.ThingsV2TagsApi.ThingsV2TagsUpsert(ctx, id)
408+
req = req.Tag(iotclient.Tag{Key: key, Value: val})
409+
_, err := cl.api.ThingsV2TagsApi.ThingsV2TagsUpsertExecute(req)
401410
if err != nil {
402411
err = fmt.Errorf("cannot create tag %s: %w", key, errorDetail(err))
403412
return err
@@ -415,7 +424,8 @@ func (cl *Client) ThingTagsDelete(ctx context.Context, id string, keys []string)
415424
}
416425

417426
for _, key := range keys {
418-
_, err := cl.api.ThingsV2TagsApi.ThingsV2TagsDelete(ctx, id, key)
427+
req := cl.api.ThingsV2TagsApi.ThingsV2TagsDelete(ctx, id, key)
428+
_, err := cl.api.ThingsV2TagsApi.ThingsV2TagsDeleteExecute(req)
419429
if err != nil {
420430
err = fmt.Errorf("cannot delete tag %s: %w", key, errorDetail(err))
421431
return err
@@ -431,11 +441,13 @@ func (cl *Client) DashboardCreate(ctx context.Context, dashboard *iotclient.Dash
431441
return nil, err
432442
}
433443

434-
newDashboard, _, err := cl.api.DashboardsV2Api.DashboardsV2Create(ctx, *dashboard, nil)
444+
req := cl.api.DashboardsV2Api.DashboardsV2Create(ctx)
445+
req = req.Dashboardv2(*dashboard)
446+
newDashboard, _, err := cl.api.DashboardsV2Api.DashboardsV2CreateExecute(req)
435447
if err != nil {
436448
return nil, fmt.Errorf("%s: %w", "adding new dashboard", errorDetail(err))
437449
}
438-
return &newDashboard, nil
450+
return newDashboard, nil
439451
}
440452

441453
// DashboardShow allows to retrieve a specific dashboard, given its id,
@@ -446,12 +458,13 @@ func (cl *Client) DashboardShow(ctx context.Context, id string) (*iotclient.Ardu
446458
return nil, err
447459
}
448460

449-
dashboard, _, err := cl.api.DashboardsV2Api.DashboardsV2Show(ctx, id, nil)
461+
req := cl.api.DashboardsV2Api.DashboardsV2Show(ctx, id)
462+
dashboard, _, err := cl.api.DashboardsV2Api.DashboardsV2ShowExecute(req)
450463
if err != nil {
451464
err = fmt.Errorf("retrieving dashboard, %w", errorDetail(err))
452465
return nil, err
453466
}
454-
return &dashboard, nil
467+
return dashboard, nil
455468
}
456469

457470
// DashboardList returns a list of dashboards on Arduino IoT Cloud.
@@ -460,8 +473,9 @@ func (cl *Client) DashboardList(ctx context.Context) ([]iotclient.ArduinoDashboa
460473
if err != nil {
461474
return nil, err
462475
}
463-
464-
dashboards, _, err := cl.api.DashboardsV2Api.DashboardsV2List(ctx, nil)
476+
477+
req := cl.api.DashboardsV2Api.DashboardsV2List(ctx)
478+
dashboards, _, err := cl.api.DashboardsV2Api.DashboardsV2ListExecute(req)
465479
if err != nil {
466480
err = fmt.Errorf("listing dashboards: %w", errorDetail(err))
467481
return nil, err
@@ -476,7 +490,8 @@ func (cl *Client) DashboardDelete(ctx context.Context, id string) error {
476490
return err
477491
}
478492

479-
_, err = cl.api.DashboardsV2Api.DashboardsV2Delete(ctx, id, nil)
493+
req := cl.api.DashboardsV2Api.DashboardsV2Delete(ctx, id)
494+
_, err = cl.api.DashboardsV2Api.DashboardsV2DeleteExecute(req)
480495
if err != nil {
481496
err = fmt.Errorf("deleting dashboard: %w", errorDetail(err))
482497
return err
@@ -492,9 +507,15 @@ func (cl *Client) setup(client, secret, organization string) error {
492507

493508
config := iotclient.NewConfiguration()
494509
if organization != "" {
495-
config.DefaultHeader = map[string]string{"X-Organization": organization}
510+
config.AddDefaultHeader("X-Organization", organization)
511+
}
512+
config.Host = baseURL + "/iot"
513+
config.Servers = iotclient.ServerConfigurations{
514+
{
515+
URL: config.Host,
516+
Description: "No description provided",
517+
},
496518
}
497-
config.BasePath = baseURL + "/iot"
498519
cl.api = iotclient.NewAPIClient(config)
499520

500521
return nil

0 commit comments

Comments
 (0)