-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
getResource.go
52 lines (46 loc) · 1.72 KB
/
getResource.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
package service
import (
"net/http"
"net/http/httptest"
"strings"
"github.com/google/go-querystring/query"
"github.com/gorilla/mux"
"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 (requestHandler *RequestHandler) getResourceFromTwin(w http.ResponseWriter, r *http.Request, resourceID string) {
type Options struct {
ResourceIDFilter []string `url:"resourceIdFilter"`
}
opt := Options{
ResourceIDFilter: []string{resourceID},
}
v, err := query.Values(opt)
if err != nil {
serverMux.WriteError(w, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, "cannot get resource('%v') from twin: %v", resourceID, err))
return
}
r.URL.Path = uri.Resources
r.URL.RawQuery = v.Encode()
rec := httptest.NewRecorder()
requestHandler.mux.ServeHTTP(rec, r)
toSimpleResponse(w, rec, func(w http.ResponseWriter, err error) {
serverMux.WriteError(w, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, "cannot get resource('%v') from twin: %v", resourceID, err))
}, streamResponseKey)
}
func (requestHandler *RequestHandler) getResource(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
deviceID := vars[uri.DeviceIDKey]
resourceHref := vars[uri.ResourceHrefKey]
resourceID := commands.NewResourceID(deviceID, resourceHref).ToString()
twin := r.URL.Query().Get(uri.TwinQueryKey)
resourceInterface := r.URL.Query().Get(uri.ResourceInterfaceQueryKey)
if (twin == "" || strings.ToLower(twin) == "true") && resourceInterface == "" {
requestHandler.getResourceFromTwin(w, r, resourceID)
return
}
requestHandler.mux.ServeHTTP(w, r)
}