/
updateResource.go
57 lines (48 loc) · 1.5 KB
/
updateResource.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
package service
import (
"bytes"
"fmt"
"io"
"net/http"
"github.com/gorilla/mux"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/hub/v2/http-gateway/serverMux"
"github.com/plgd-dev/hub/v2/http-gateway/uri"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/encoding/protojson"
)
func createContentBody(body io.ReadCloser) (io.ReadCloser, error) {
data, err := io.ReadAll(body)
if err != nil {
return nil, fmt.Errorf("read body: %w", err)
}
req := commands.Content{
ContentType: message.AppJSON.String(),
CoapContentFormat: int32(message.AppJSON),
Data: data,
}
reqData, err := protojson.Marshal(&req)
if err != nil {
return nil, fmt.Errorf("cannot marshal to protojson: %w", err)
}
return io.NopCloser(bytes.NewReader(reqData)), nil
}
func (requestHandler *RequestHandler) updateResource(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
deviceID := vars[uri.DeviceIDKey]
href := vars[uri.ResourceHrefKey]
contentType := r.Header.Get(uri.ContentTypeHeaderKey)
if contentType == uri.ApplicationProtoJsonContentType {
requestHandler.mux.ServeHTTP(w, r)
return
}
newBody, err := createContentBody(r.Body)
if err != nil {
serverMux.WriteError(w, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, "cannot update resource('/%v%v'): %v", deviceID, href, err))
return
}
r.Body = newBody
requestHandler.mux.ServeHTTP(w, r)
}