Skip to content

Commit 380ed39

Browse files
committed
added getTemplate client
1 parent 50f772b commit 380ed39

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed

command/template/apply.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,34 @@
1818
package template
1919

2020
import (
21+
"fmt"
22+
23+
"github.com/arduino/arduino-cli/cli/feedback"
2124
"github.com/arduino/arduino-cloud-cli/config"
25+
storageapi "github.com/arduino/arduino-cloud-cli/internal/storage-api"
26+
"github.com/gofrs/uuid"
27+
"github.com/sirupsen/logrus"
2228
)
2329

2430
func ApplyCustomTemplates(cred *config.Credentials, templateId string) error {
2531

26-
// TODO
32+
apiclient := storageapi.NewClient(cred)
33+
34+
feedback.Printf("Applying template %s", templateId)
35+
36+
templateIdUUID, err := uuid.FromString(templateId)
37+
if err != nil {
38+
return fmt.Errorf("invalid template id: %s", templateId)
39+
}
40+
cstTemplate, err := apiclient.GetCustomTemplate(templateIdUUID)
41+
if err != nil {
42+
return err
43+
}
44+
if len(cstTemplate.ThingTemplates) > 0 {
45+
mainThing := cstTemplate.ThingTemplates[0]
46+
logrus.Debug("Main thing template - id: ", mainThing.Id)
47+
//TODO check thing ID proceed
48+
}
2749

2850
return nil
2951
}

internal/storage-api/client.go

+47-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/arduino/arduino-cloud-cli/config"
3434
"github.com/arduino/arduino-cloud-cli/internal/iot"
35+
"github.com/gofrs/uuid"
3536
"github.com/sirupsen/logrus"
3637
"golang.org/x/oauth2"
3738
)
@@ -125,8 +126,8 @@ func (c *StorageApiClient) performBinaryGetRequest(endpoint, token string) (*htt
125126
return res, nil
126127
}
127128

128-
func (c *StorageApiClient) performBinaryPostRequest(endpoint, token string, body io.Reader) (*http.Response, error) {
129-
req, err := http.NewRequest("POST", endpoint, body)
129+
func (c *StorageApiClient) performRequest(method, endpoint, token string, body io.Reader) (*http.Response, error) {
130+
req, err := http.NewRequest(method, endpoint, body)
130131
if err != nil {
131132
return nil, err
132133
}
@@ -313,7 +314,7 @@ func (c *StorageApiClient) ListCustomTemplates() (*TemplatesListResponse, error)
313314
}
314315

315316
endpoint := c.host + "/storage/template/v1/list"
316-
res, err := c.performBinaryPostRequest(endpoint, userRequestToken.AccessToken, bytes.NewReader(jsonRequest))
317+
res, err := c.performRequest("POST", endpoint, userRequestToken.AccessToken, bytes.NewReader(jsonRequest))
317318
if err != nil {
318319
return nil, err
319320
}
@@ -345,3 +346,46 @@ func (c *StorageApiClient) ListCustomTemplates() (*TemplatesListResponse, error)
345346

346347
return nil, err
347348
}
349+
350+
func (c *StorageApiClient) GetCustomTemplate(templateID uuid.UUID) (*DescribeTemplateResponse, error) {
351+
userRequestToken, err := c.src.Token()
352+
if err != nil {
353+
if strings.Contains(err.Error(), "401") {
354+
return nil, errors.New("wrong credentials")
355+
}
356+
return nil, fmt.Errorf("cannot retrieve a valid token: %w", err)
357+
}
358+
359+
endpoint := c.host + fmt.Sprintf("/storage/template/v1/%s", templateID.String())
360+
res, err := c.performRequest("GET", endpoint, userRequestToken.AccessToken, nil)
361+
if err != nil {
362+
return nil, err
363+
}
364+
defer res.Body.Close()
365+
366+
logrus.Debugf("Get template %s API call status: %d", templateID.String(), res.StatusCode)
367+
368+
if res.StatusCode == 200 || res.StatusCode == 201 {
369+
var getTemplateResponse DescribeTemplateResponse
370+
respBytes, err := io.ReadAll(res.Body)
371+
if err != nil {
372+
return nil, err
373+
}
374+
err = json.Unmarshal(respBytes, &getTemplateResponse)
375+
if err != nil {
376+
return nil, err
377+
}
378+
return &getTemplateResponse, nil
379+
} else if res.StatusCode == 400 {
380+
bodyb, _ := io.ReadAll(res.Body)
381+
return nil, fmt.Errorf("bad request: %s", string(bodyb))
382+
} else if res.StatusCode == 401 {
383+
return nil, errors.New("unauthorized request")
384+
} else if res.StatusCode == 403 {
385+
return nil, errors.New("forbidden request")
386+
} else if res.StatusCode == 500 {
387+
return nil, errors.New("internal server error")
388+
}
389+
390+
return nil, err
391+
}

internal/storage-api/dto.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ func formatHumanReadableTs(ts string) string {
7272
return parsed.Format(time.RFC3339)
7373
}
7474

75-
type TemplateDescribeResponse struct {
75+
type DescribeTemplateResponse struct {
7676
// CompatibleBoards List of devices compatible with the template
77-
CompatibleBoards *[]string `json:"compatible_boards,omitempty"`
77+
CompatibleBoards []string `json:"compatible_boards,omitempty"`
7878

7979
// CreatedAt Template creation date/time
8080
CreatedAt *time.Time `json:"created_at,omitempty"`
8181

8282
// DashboardTemplates List of dashboard templates
83-
DashboardTemplates *[]DashboardTemplate `json:"dashboard_templates,omitempty"`
83+
DashboardTemplates []DashboardTemplate `json:"dashboard_templates,omitempty"`
8484

8585
// DeletedAt Template soft deletion date/time
8686
DeletedAt *time.Time `json:"deleted_at,omitempty"`
@@ -89,7 +89,7 @@ type TemplateDescribeResponse struct {
8989
Description string `json:"description"`
9090

9191
// ImageLinks Optional list of images to be included in the template
92-
ImageLinks *[]string `json:"image_links,omitempty"`
92+
ImageLinks []string `json:"image_links,omitempty"`
9393

9494
// Name Template name
9595
Name string `json:"name" validate:"required,max=128"`
@@ -99,10 +99,10 @@ type TemplateDescribeResponse struct {
9999
TemplateId uuid.UUID `json:"template_id" validate:"required,uuid"`
100100

101101
// ThingTemplates List of thing templates
102-
ThingTemplates *[]ThingTemplate `json:"thing_templates,omitempty"`
102+
ThingTemplates []ThingTemplate `json:"thing_templates,omitempty"`
103103

104104
// TriggerTemplates List of trigger templates
105-
TriggerTemplates *[]TriggerTemplate `json:"trigger_templates,omitempty"`
105+
TriggerTemplates []TriggerTemplate `json:"trigger_templates,omitempty"`
106106

107107
// UpdatedAt Template update date/time
108108
UpdatedAt *time.Time `json:"updated_at,omitempty"`
@@ -119,10 +119,10 @@ type ThingTemplate struct {
119119
Name string `json:"name"`
120120

121121
// Tags Tags
122-
Tags *[]Tag `json:"tags,omitempty"`
122+
Tags []Tag `json:"tags,omitempty"`
123123

124124
// Variables Thing variables
125-
Variables *[]Variable `json:"variables,omitempty"`
125+
Variables []Variable `json:"variables,omitempty"`
126126

127127
// WebhookUri Webhook URI
128128
WebhookUri *string `json:"webhook_uri,omitempty"`
@@ -181,13 +181,13 @@ type Widget struct {
181181
Name string `json:"name"`
182182

183183
// Options Widget options
184-
Options *map[string]interface{} `json:"options,omitempty"`
184+
Options map[string]interface{} `json:"options,omitempty"`
185185

186186
// Type Widget type
187187
Type string `json:"type"`
188188

189189
// Variables Widget variables
190-
Variables *[]WidgetVariable `json:"variables,omitempty"`
190+
Variables []WidgetVariable `json:"variables,omitempty"`
191191

192192
// Width Width
193193
Width *float32 `json:"width,omitempty"`
@@ -227,7 +227,7 @@ type WidgetVariable struct {
227227

228228
type TriggerTemplate struct {
229229
// Actions Actions
230-
Actions *[]TriggerAction `json:"actions,omitempty"`
230+
Actions []TriggerAction `json:"actions,omitempty"`
231231

232232
// Active Active
233233
Active *bool `json:"active,omitempty"`

0 commit comments

Comments
 (0)