Skip to content

Commit

Permalink
http-gateway: get property value from the device without hub metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed May 15, 2024
1 parent 7cfb6e5 commit 0e6a763
Show file tree
Hide file tree
Showing 5 changed files with 1,030 additions and 8 deletions.
16 changes: 10 additions & 6 deletions http-gateway/service/getResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func parseBoolQuery(str string) bool {
return val
}

func (requestHandler *RequestHandler) serveResourceRequest(r *http.Request, deviceID, resourceHref, twin, resourceInterface string) (*httptest.ResponseRecorder, error) {
func (requestHandler *RequestHandler) serveResourceRequest(r *http.Request, deviceID, resourceHref, twin, resourceInterface string) (*httptest.ResponseRecorder, bool, error) {
resourceID := pb.ResourceIdFilter{
ResourceId: commands.NewResourceID(deviceID, resourceHref),
Etag: getETags(r),
Expand All @@ -87,9 +87,9 @@ func (requestHandler *RequestHandler) serveResourceRequest(r *http.Request, devi
if (twin == "" || parseBoolQuery(twin)) && resourceInterface == "" {
rec, err := requestHandler.getResourceFromTwin(r, &resourceID)
if err != nil {
return nil, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, errFmtFromTwin, &resourceID, err)
return nil, false, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, errFmtFromTwin, &resourceID, err)
}
return rec, nil
return rec, true, nil
}

query := r.URL.Query()
Expand All @@ -103,7 +103,7 @@ func (requestHandler *RequestHandler) serveResourceRequest(r *http.Request, devi

rec := httptest.NewRecorder()
requestHandler.mux.ServeHTTP(rec, r)
return rec, nil
return rec, false, nil
}

func jsonGetValueOnPath(v interface{}, path ...string) (interface{}, error) {
Expand Down Expand Up @@ -174,14 +174,18 @@ func (requestHandler *RequestHandler) getResource(w http.ResponseWriter, r *http
twin := r.URL.Query().Get(uri.TwinQueryKey)
onlyContent := r.URL.Query().Get(uri.OnlyContentQueryKey)
resourceInterface := r.URL.Query().Get(uri.ResourceInterfaceQueryKey)
rec, err := requestHandler.serveResourceRequest(r, deviceID, resourceHref, twin, resourceInterface)
rec, fromTwin, err := requestHandler.serveResourceRequest(r, deviceID, resourceHref, twin, resourceInterface)
if err != nil {
serverMux.WriteError(w, err)
return
}
allowEmptyContent := false
if parseBoolQuery(onlyContent) {
allowEmptyContent = requestHandler.filterOnlyContent(rec, "result", "data", "content")
filterPath := []string{"result", "data", "content"}
if !fromTwin {
filterPath = []string{"data", "content"}
}
allowEmptyContent = requestHandler.filterOnlyContent(rec, filterPath...)
}
toSimpleResponse(w, rec, allowEmptyContent, func(w http.ResponseWriter, err error) {
serverMux.WriteError(w, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, "cannot get resource('%v/%v') from the device: %v", deviceID, resourceHref, err))
Expand Down
16 changes: 15 additions & 1 deletion http-gateway/service/getResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ func TestRequestHandlerGetResourceWithOnlyContent(t *testing.T) {
type args struct {
deviceID string
resourceHref string
twin *bool
}
tests := []struct {
name string
Expand All @@ -255,14 +256,24 @@ func TestRequestHandlerGetResourceWithOnlyContent(t *testing.T) {
wantCode int
}{
{
name: "json: get from resource twin",
name: "json: get resource from twin",
args: args{
deviceID: deviceID,
resourceHref: test.TestResourceLightInstanceHref("1"),
},
want: map[interface{}]interface{}{"name": "Light", "power": uint64(0x0), "state": false},
wantCode: http.StatusOK,
},
{
name: "json: get resource from device",
args: args{
deviceID: deviceID,
resourceHref: test.TestResourceLightInstanceHref("1"),
twin: newBool(false),
},
want: map[interface{}]interface{}{"name": "Light", "power": uint64(0x0), "state": false},
wantCode: http.StatusOK,
},
{
name: "json: not exists",
args: args{
Expand All @@ -278,6 +289,9 @@ func TestRequestHandlerGetResourceWithOnlyContent(t *testing.T) {
rb := httpgwTest.NewRequest(http.MethodGet, uri.AliasDeviceResource, nil).AuthToken(token)
rb.DeviceId(tt.args.deviceID).ResourceHref(tt.args.resourceHref)
rb.OnlyContent(true)
if tt.args.twin != nil {
rb.Twin(*tt.args.twin)
}
resp := httpgwTest.HTTPDo(t, rb.Build())
defer func() {
_ = resp.Body.Close()
Expand Down
2 changes: 1 addition & 1 deletion http-gateway/service/getThings.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (requestHandler *RequestHandler) thingDescriptionResponse(ctx context.Conte
func (requestHandler *RequestHandler) getThing(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
deviceID := vars[uri.DeviceIDKey]
rec, err := requestHandler.serveResourceRequest(r, deviceID, bridgeResourcesTD.ResourceURI, "", "")
rec, _, err := requestHandler.serveResourceRequest(r, deviceID, bridgeResourcesTD.ResourceURI, "", "")
if err != nil {
serverMux.WriteError(w, err)
return
Expand Down
Loading

0 comments on commit 0e6a763

Please sign in to comment.