-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
244 lines (209 loc) · 6.34 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
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
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package otaapi
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sort"
"strings"
"time"
"github.com/arduino/arduino-cloud-cli/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
"golang.org/x/oauth2"
)
const (
OrderDesc = "desc"
OrderAsc = "asc"
)
var ErrAlreadyInProgress = fmt.Errorf("already in progress")
var ErrAlreadyCancelled = fmt.Errorf("already cancelled")
type OtaApiClient struct {
client *http.Client
host string
src oauth2.TokenSource
organization string
}
func NewClient(credentials *config.Credentials) *OtaApiClient {
host := iot.GetArduinoAPIBaseURL()
tokenSource := iot.NewUserTokenSource(credentials.Client, credentials.Secret, host, credentials.Organization)
return &OtaApiClient{
client: &http.Client{},
src: tokenSource,
host: host,
organization: credentials.Organization,
}
}
func (c *OtaApiClient) performGetRequest(endpoint, token string) (*http.Response, error) {
return c.performRequest(endpoint, "GET", token)
}
func (c *OtaApiClient) performRequest(endpoint, method, token string) (*http.Response, error) {
req, err := http.NewRequest(method, endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+token)
req.Header.Add("Content-Type", "application/json")
if c.organization != "" {
req.Header.Add("X-Organization", c.organization)
}
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
return res, nil
}
func (c *OtaApiClient) GetOtaStatusByOtaID(otaid string, limit int, order string) (*OtaStatusResponse, error) {
if otaid == "" {
return nil, fmt.Errorf("invalid ota-id: empty")
}
userRequestToken, err := c.src.Token()
if err != nil {
if strings.Contains(err.Error(), "401") {
return nil, errors.New("wrong credentials")
}
return nil, fmt.Errorf("cannot retrieve a valid token: %w", err)
}
endpoint := c.host + "/ota/v1/ota/" + otaid
res, err := c.performGetRequest(endpoint, userRequestToken.AccessToken)
if err != nil {
return nil, err
}
defer res.Body.Close()
bodyb, err := io.ReadAll(res.Body)
if res.StatusCode == 200 {
var otaResponse OtaStatusResponse
if err == nil && bodyb != nil {
err = json.Unmarshal(bodyb, &otaResponse)
if err != nil {
return nil, err
}
}
if len(otaResponse.States) > 0 {
// Sort output by StartedAt
sort.Slice(otaResponse.States, func(i, j int) bool {
t1, err := time.Parse(time.RFC3339, otaResponse.States[i].Timestamp)
if err != nil {
return false
}
t2, err := time.Parse(time.RFC3339, otaResponse.States[j].Timestamp)
if err != nil {
return false
}
if order == "asc" {
return t1.Before(t2)
}
return t1.After(t2)
})
if limit > 0 && len(otaResponse.States) > limit {
otaResponse.States = otaResponse.States[:limit]
}
}
return &otaResponse, nil
} else if res.StatusCode == 404 || res.StatusCode == 400 {
return nil, fmt.Errorf("ota-id %s not found", otaid)
}
return nil, err
}
func (c *OtaApiClient) GetOtaStatusByOtaIDs(otaids string) (*OtaStatusList, error) {
ids := strings.Split(otaids, ",")
if len(ids) == 0 {
return nil, fmt.Errorf("invalid ota-ids: empty")
}
returnStatus := OtaStatusList{}
for _, id := range ids {
if id != "" {
resp, err := c.GetOtaStatusByOtaID(id, 1, OrderDesc)
if err != nil {
return nil, err
}
returnStatus.Ota = append(returnStatus.Ota, resp.Ota)
}
}
return &returnStatus, nil
}
func (c *OtaApiClient) GetOtaLastStatusByDeviceID(deviceID string) (*OtaStatusList, error) {
return c.GetOtaStatusByDeviceID(deviceID, 1, OrderDesc)
}
func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string, limit int, order string) (*OtaStatusList, error) {
if deviceID == "" {
return nil, fmt.Errorf("invalid device-id: empty")
}
userRequestToken, err := c.src.Token()
if err != nil {
if strings.Contains(err.Error(), "401") {
return nil, errors.New("wrong credentials")
}
return nil, fmt.Errorf("cannot retrieve a valid token: %w", err)
}
endpoint := c.host + "/ota/v1/ota?device_id=" + deviceID
if limit > 0 {
endpoint += "&limit=" + fmt.Sprintf("%d", limit)
}
if order != "" && (order == "asc" || order == "desc") {
endpoint += "&order=" + order
}
res, err := c.performGetRequest(endpoint, userRequestToken.AccessToken)
if err != nil {
return nil, err
}
defer res.Body.Close()
bodyb, err := io.ReadAll(res.Body)
if res.StatusCode == 200 {
var otaResponse OtaStatusList
if err == nil && bodyb != nil {
err = json.Unmarshal(bodyb, &otaResponse)
if err != nil {
return nil, err
}
}
return &otaResponse, nil
} else if res.StatusCode == 404 || res.StatusCode == 400 {
return nil, fmt.Errorf("device-id %s not found", deviceID)
} else if res.StatusCode == 409 {
return nil, ErrAlreadyInProgress
}
return nil, err
}
func (c *OtaApiClient) CancelOta(otaid string) (bool, error) {
if otaid == "" {
return false, fmt.Errorf("invalid ota-id: empty")
}
userRequestToken, err := c.src.Token()
if err != nil {
if strings.Contains(err.Error(), "401") {
return false, errors.New("wrong credentials")
}
return false, fmt.Errorf("cannot retrieve a valid token: %w", err)
}
endpoint := c.host + "/ota/v1/ota/" + otaid + "/cancel"
res, err := c.performRequest(endpoint, "PUT", userRequestToken.AccessToken)
if err != nil {
return false, err
}
defer res.Body.Close()
if res.StatusCode == 200 {
return true, nil
} else if res.StatusCode == 404 || res.StatusCode == 400 {
return false, fmt.Errorf("ota-id %s not found", otaid)
} else if res.StatusCode == 409 {
return false, ErrAlreadyCancelled
}
return false, err
}