Skip to content

Commit cc83c9e

Browse files
hosomoxtoacart
authored andcommitted
Add feature and matching test for setting device name
Signed-off-by: hosom <[email protected]>
1 parent 3c653bd commit cc83c9e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

v2/devices.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ func (dr *DevicesResource) Delete(ctx context.Context, deviceID string) error {
115115
return dr.do(req, nil)
116116
}
117117

118+
// SetName updates the name of the device by deviceID.
119+
func (dr *DevicesResource) SetName(ctx context.Context, deviceID, name string) error {
120+
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildURL("device", deviceID, "name"), requestBody(map[string]string{
121+
"name": name,
122+
}))
123+
if err != nil {
124+
return err
125+
}
126+
127+
return dr.do(req, nil)
128+
}
129+
118130
// SetTags updates the tags of the device identified by deviceID.
119131
func (dr *DevicesResource) SetTags(ctx context.Context, deviceID string, tags []string) error {
120132
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildURL("device", deviceID, "tags"), requestBody(map[string][]string{

v2/devices_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ func TestClient_SetDeviceAuthorized(t *testing.T) {
245245
}
246246
}
247247

248+
func TestClient_SetDeviceName(t *testing.T) {
249+
t.Parallel()
250+
251+
client, server := NewTestHarness(t)
252+
server.ResponseCode = http.StatusOK
253+
254+
const deviceID = "test"
255+
name := "test"
256+
257+
assert.NoError(t, client.Devices().SetName(context.Background(), deviceID, name))
258+
assert.EqualValues(t, http.MethodPost, server.Method)
259+
assert.EqualValues(t, "/api/v2/device/"+deviceID+"/name", server.Path)
260+
261+
body := make(map[string]string)
262+
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
263+
assert.EqualValues(t, name, body["name"])
264+
}
265+
248266
func TestClient_SetDeviceTags(t *testing.T) {
249267
t.Parallel()
250268

0 commit comments

Comments
 (0)