-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
getDevice.go
145 lines (136 loc) · 3.72 KB
/
getDevice.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
package service
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"github.com/google/go-querystring/query"
"github.com/gorilla/mux"
jsoniter "github.com/json-iterator/go"
"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"
)
func isNotModifiedResponse(result interface{}) bool {
m, ok := result.(map[string]interface{})
if !ok {
return false
}
data, ok := m["data"]
if ok {
var tmp map[string]interface{}
tmp, ok = data.(map[string]interface{})
if !ok {
return false
}
_, ok = tmp["status"]
if ok {
m = tmp
}
}
v, ok := m["status"]
if !ok {
return false
}
statusVal, ok := v.(string)
if !ok {
return false
}
return statusVal == commands.Status_NOT_MODIFIED.String()
}
func writeSimpleResponse(w http.ResponseWriter, rec *httptest.ResponseRecorder, bodyForEncode interface{}, writeError func(w http.ResponseWriter, err error)) {
if isNotModifiedResponse(bodyForEncode) {
w.WriteHeader(http.StatusNotModified)
return
}
encoder := jsoniter.NewEncoder(w)
// copy everything from response recorder
// to actual response writer
for k, v := range rec.Header() {
w.Header()[k] = v
}
w.WriteHeader(rec.Code)
err := encoder.Encode(bodyForEncode)
if err != nil {
writeError(w, kitNetGrpc.ForwardErrorf(codes.Internal, "cannot encode response: %v", err))
}
}
func getResponse(rec *httptest.ResponseRecorder, allowEmpty bool, responseKeys ...string) (interface{}, error) {
if len(rec.Body.Bytes()) == 0 && allowEmpty {
return nil, nil
}
iter := json.NewDecoder(bytes.NewReader(rec.Body.Bytes()))
datas := make([]interface{}, 0, 1)
for {
var v interface{}
err := iter.Decode(&v)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, err
}
datas = append(datas, v)
}
if len(datas) == 0 {
return nil, kitNetGrpc.ForwardErrorf(codes.NotFound, "not found")
}
if len(datas) != 1 {
return nil, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, "invalid number of responses")
}
result := datas[0]
for _, key := range responseKeys {
m, ok := result.(map[string]interface{})
if !ok {
break
}
tmp, ok := m[key]
if ok {
result = tmp
} else {
break
}
}
return result, nil
}
func toSimpleResponse(w http.ResponseWriter, rec *httptest.ResponseRecorder, allowEmpty bool, writeError func(w http.ResponseWriter, err error), responseKeys ...string) {
result, err := getResponse(rec, allowEmpty, responseKeys...)
if err != nil {
writeError(w, err)
return
}
writeSimpleResponse(w, rec, result, writeError)
}
func (requestHandler *RequestHandler) serveDevicesRequest(r *http.Request) (string, *httptest.ResponseRecorder, error) {
vars := mux.Vars(r)
deviceID := vars[uri.DeviceIDKey]
type Options struct {
DeviceIDFilter []string `url:"deviceIdFilter"`
}
opt := Options{
DeviceIDFilter: []string{deviceID},
}
v, err := query.Values(opt)
if err != nil {
return deviceID, nil, err
}
r.URL.Path = uri.Devices
r.URL.RawQuery = v.Encode()
rec := httptest.NewRecorder()
requestHandler.mux.ServeHTTP(rec, r)
return deviceID, rec, nil
}
func (requestHandler *RequestHandler) getDevice(w http.ResponseWriter, r *http.Request) {
deviceID, rec, err := requestHandler.serveDevicesRequest(r)
if err != nil {
serverMux.WriteError(w, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, "cannot get device('%v'): %v", deviceID, err))
return
}
toSimpleResponse(w, rec, false, func(w http.ResponseWriter, err error) {
serverMux.WriteError(w, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, "cannot get device('%v'): %v", deviceID, err))
}, streamResponseKey)
}