Skip to content

Commit

Permalink
add GET /local-devices list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
IngoRoessner committed Jul 11, 2024
1 parent 082ce9e commit 79f0a35
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lib/api/localdevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/SENERGY-Platform/device-manager/lib/config"
"github.com/SENERGY-Platform/device-manager/lib/model"
"github.com/SENERGY-Platform/models/go/models"
"github.com/SENERGY-Platform/service-commons/pkg/jwt"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
Expand All @@ -37,6 +38,55 @@ func init() {
func LocalDevicesEndpoints(config config.Config, control Controller, router *httprouter.Router) {
resource := "/local-devices"

//query-parameter:
// - limit: number; default 100, will be ignored if 'ids' is set
// - offset: number; default 0, will be ignored if 'ids' is set
// - ids: filter by comma seperated id list
router.GET(resource, func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
token, err := jwt.GetParsedToken(request)
if err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
return
}

ownerId := request.URL.Query().Get("owner_id")
if ownerId == "" {
ownerId = token.GetUserId()
}

result := []models.Device{}

query := request.URL.Query()
idsStr := query.Get("ids")
if idsStr != "" {
localIds := strings.Split(idsStr, ",")
for _, localId := range localIds {
device, err, errCode := control.ReadDeviceByLocalId(token, ownerId, localId)
if err == nil {
result = append(result, device)
}
if err != nil && errCode >= 500 {
http.Error(writer, err.Error(), errCode)
return
}
}
} else {
var errCode int
result, err, errCode = control.ListDevices(token, query)
if err != nil {
http.Error(writer, err.Error(), errCode)
return
}
}

writer.Header().Set("Content-Type", "application/json; charset=utf-8")
err = json.NewEncoder(writer).Encode(result)
if err != nil {
log.Println("ERROR: unable to encode response", err)
}
return
})

router.GET(resource+"/:id", func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
id := params.ByName("id")
token, err := auth.GetParsedToken(request)
Expand Down
21 changes: 21 additions & 0 deletions lib/tests/local_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,27 @@ func testLocalDevice(t *testing.T, port string) {
t.Fatal(result)
}

resp, err = helper.Jwtget(userjwt, "http://localhost:"+port+"/local-devices?ids="+url.QueryEscape(device.LocalId+",unknown,foo"))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
t.Fatal(resp.Status, resp.StatusCode, string(b))
}

list := []models.Device{}
err = json.NewDecoder(resp.Body).Decode(&list)
if err != nil {
t.Fatal(err)
}

if len(list) != 1 || list[0].Name != "updated_device_name" || list[0].LocalId != "lid1" || list[0].DeviceTypeId != dt.Id {
t.Fatal(list)
}

//delete
resp, err = helper.Jwtdelete(userjwt, "http://localhost:"+port+"/local-devices/"+url.PathEscape(device.LocalId))
if err != nil {
Expand Down

0 comments on commit 79f0a35

Please sign in to comment.