Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added WithLinkNotFoundCallback from main branch #511

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/plgd-dev/device/v2/client/core"
"github.com/plgd-dev/device/v2/client/core/otm"
"github.com/plgd-dev/device/v2/pkg/net/coap"
"github.com/plgd-dev/device/v2/schema"
"github.com/plgd-dev/go-coap/v3/net/blockwise"
"github.com/plgd-dev/go-coap/v3/options"
coapSync "github.com/plgd-dev/go-coap/v3/pkg/sync"
Expand Down Expand Up @@ -397,3 +398,29 @@ func NewDeviceOwnerFromConfig(cfg *Config, dialTLS core.DialTLS, dialDTLS core.D
}
return newDeviceOwnershipNone(), nil
}

func (c *Client) GetDeviceLinkForHref(
ctx context.Context,
deviceID string,
href string,
discoveryCfg core.DiscoveryConfiguration,
callback LinkNotFoundCallback,
) (*core.Device, schema.ResourceLink, error) {
d, links, err := c.GetDevice(ctx, deviceID, WithDiscoveryConfiguration(discoveryCfg))
if err != nil {
return nil, schema.ResourceLink{}, err
}

link, err := core.GetResourceLink(links, href)
if err != nil {
if callback.linkNotFoundCallback != nil {
link, err = callback.linkNotFoundCallback(links, href)
if err != nil {
return nil, schema.ResourceLink{}, err
}
} else {
return nil, schema.ResourceLink{}, err
}
}
return d, link, nil
}
1 change: 1 addition & 0 deletions client/core/provisionDevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func TestSettingCloudResource(t *testing.T) {
AuthorizationProvider: "testAuthorizationProvider",
URL: "testURL",
AuthorizationCode: "testAuthorizationCode",
CloudID: "00000000-0000-0000-0000-000000000000",
}
err = pc.SetCloudResource(context.Background(), r)
require.NoError(t, err)
Expand Down
9 changes: 2 additions & 7 deletions client/createResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,10 @@ func (c *Client) CreateResource(
cfg = o.applyOnCreate(cfg)
}

d, links, err := c.GetDevice(ctx, deviceID, WithDiscoveryConfiguration(cfg.discoveryConfiguration))
device, link, err := c.GetDeviceLinkForHref(ctx, deviceID, href, cfg.discoveryConfiguration, LinkNotFoundCallback{linkNotFoundCallback: cfg.linkNotFoundCallback})
if err != nil {
return err
}

link, err := core.GetResourceLink(links, href)
if err != nil {
return err
}

return d.UpdateResourceWithCodec(ctx, link, cfg.codec, request, response, cfg.opts...)
return device.UpdateResourceWithCodec(ctx, link, cfg.codec, request, response, cfg.opts...)
}
27 changes: 27 additions & 0 deletions client/createResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/plgd-dev/device/v2/client"
"github.com/plgd-dev/device/v2/client/core"
"github.com/plgd-dev/device/v2/schema"
"github.com/plgd-dev/device/v2/schema/device"
"github.com/plgd-dev/device/v2/schema/interfaces"
"github.com/plgd-dev/device/v2/test"
Expand Down Expand Up @@ -60,6 +61,32 @@ func TestClientCreateResource(t *testing.T) {
},
}),
},
{
name: "test link not found callback",
args: args{
deviceID: deviceID,
href: "/link/not/found",
body: test.MakeSwitchResourceDefaultData(),
opts: []client.CreateOption{
client.WithDiscoveryConfiguration(core.DefaultDiscoveryConfiguration()),
client.WithLinkNotFoundCallback(func(links schema.ResourceLinks, href string) (schema.ResourceLink, error) {
_, linkFound := links.GetResourceLink(href)
require.False(t, linkFound)
resourceLink, linkFound := links.GetResourceLink(test.TestResourceSwitchesHref)
require.True(t, linkFound)
return resourceLink, nil
}),
},
},
want: test.MakeSwitchResourceData(map[string]interface{}{
"href": test.TestResourceSwitchesInstanceHref("2"),
"rep": map[interface{}]interface{}{
"if": []interface{}{interfaces.OC_IF_A, interfaces.OC_IF_BASELINE},
"rt": []interface{}{types.BINARY_SWITCH},
"value": false,
},
}),
},
{
name: "invalid href",
args: args{
Expand Down
9 changes: 2 additions & 7 deletions client/deleteResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,10 @@ func (c *Client) DeleteResource(
cfg = o.applyOnDelete(cfg)
}

d, links, err := c.GetDevice(ctx, deviceID, WithDiscoveryConfiguration(cfg.discoveryConfiguration))
device, link, err := c.GetDeviceLinkForHref(ctx, deviceID, href, cfg.discoveryConfiguration, LinkNotFoundCallback{linkNotFoundCallback: cfg.linkNotFoundCallback})
if err != nil {
return err
}

link, err := core.GetResourceLink(links, href)
if err != nil {
return err
}

return d.DeleteResourceWithCodec(ctx, link, cfg.codec, response, cfg.opts...)
return device.DeleteResourceWithCodec(ctx, link, cfg.codec, response, cfg.opts...)
}
58 changes: 42 additions & 16 deletions client/deleteResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/plgd-dev/device/v2/client"
"github.com/plgd-dev/device/v2/client/core"
"github.com/plgd-dev/device/v2/schema"
"github.com/plgd-dev/device/v2/schema/configuration"
"github.com/plgd-dev/device/v2/schema/device"
"github.com/plgd-dev/device/v2/schema/interfaces"
"github.com/plgd-dev/device/v2/schema/resources"
Expand Down Expand Up @@ -51,7 +52,28 @@ func createSwitches(ctx context.Context, t *testing.T, c *client.Client, deviceI

func TestClientDeleteResource(t *testing.T) {
deviceID := test.MustFindDeviceByName(test.DevsimName)
const switchID = "1"
const (
switchID_1 = "1"
switchID_2 = "2"
)

c, err := NewTestSecureClient()
require.NoError(t, err)
defer func() {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID, client.WithOTMs([]client.OTMType{client.OTMType_JustWorks}))
require.NoError(t, err)
defer disown(t, c, deviceID)

createSwitches(ctx, t, c, deviceID, 2)
var nonDiscoverableResource map[string]interface{}
err = c.CreateResource(ctx, deviceID, test.TestResourceSwitchesHref, test.MakeNonDiscoverableSwitchData(), &nonDiscoverableResource)
require.NoError(t, err)

type args struct {
deviceID string
href string
Expand All @@ -66,10 +88,28 @@ func TestClientDeleteResource(t *testing.T) {
name: "valid",
args: args{
deviceID: deviceID,
href: test.TestResourceSwitchesInstanceHref(switchID),
href: test.TestResourceSwitchesInstanceHref(switchID_1),
opts: []client.DeleteOption{client.WithDiscoveryConfiguration(core.DefaultDiscoveryConfiguration())},
},
},
{
name: "delete non-discoverable resource",
args: args{
deviceID: deviceID,
href: nonDiscoverableResource["href"].(string),
opts: []client.DeleteOption{
client.WithDiscoveryConfiguration(core.DefaultDiscoveryConfiguration()),
// create the link for non-discoverable resource by utilizing the linkNotFoundCallback
// as the only thing that we need in the link is the href and endpoints we will reuse
// some known discoverable resource
client.WithLinkNotFoundCallback(func(links schema.ResourceLinks, href string) (schema.ResourceLink, error) {
resourceLink, _ := links.GetResourceLink(configuration.ResourceURI)
resourceLink.Href = href
return resourceLink, nil
}),
},
},
},
{
name: "invalid href",
args: args{
Expand All @@ -88,20 +128,6 @@ func TestClientDeleteResource(t *testing.T) {
},
}

c, err := NewTestSecureClient()
require.NoError(t, err)
defer func() {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID, client.WithOTMs([]client.OTMType{client.OTMType_JustWorks}))
require.NoError(t, err)
defer disown(t, c, deviceID)

createSwitches(ctx, t, c, deviceID, 1)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := c.DeleteResource(ctx, tt.args.deviceID, tt.args.href, nil, tt.args.opts...)
Expand Down
8 changes: 2 additions & 6 deletions client/getResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,11 @@ func (c *Client) GetResource(
for _, o := range opts {
cfg = o.applyOnGet(cfg)
}
d, links, err := c.GetDevice(ctx, deviceID, WithDiscoveryConfiguration(cfg.discoveryConfiguration))
if err != nil {
return err
}

link, err := core.GetResourceLink(links, href)
device, link, err := c.GetDeviceLinkForHref(ctx, deviceID, href, cfg.discoveryConfiguration, LinkNotFoundCallback{linkNotFoundCallback: cfg.linkNotFoundCallback})
if err != nil {
return err
}

return d.GetResourceWithCodec(ctx, link, cfg.codec, response, cfg.opts...)
return device.GetResourceWithCodec(ctx, link, cfg.codec, response, cfg.opts...)
}
54 changes: 43 additions & 11 deletions client/getResource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ import (

func TestClientGetResource(t *testing.T) {
deviceID := test.MustFindDeviceByName(test.DevsimName)
c, err := NewTestSecureClient()
require.NoError(t, err)
defer func() {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID)
require.NoError(t, err)
defer disown(t, c, deviceID)

var nonDiscoverableResource map[string]interface{}
err = c.CreateResource(ctx, deviceID, test.TestResourceSwitchesHref, test.MakeNonDiscoverableSwitchData(), &nonDiscoverableResource)
require.NoError(t, err)

type args struct {
deviceID string
href string
Expand Down Expand Up @@ -85,6 +101,33 @@ func TestClientGetResource(t *testing.T) {
},
},
},
{
name: "get non-discoverable resource",
args: args{
deviceID: deviceID,
href: nonDiscoverableResource["href"].(string),
opts: []client.GetOption{
client.WithDiscoveryConfiguration(core.DefaultDiscoveryConfiguration()),
// create the link for non-discoverable resource by utilizing the linkNotFoundCallback
// as the only thing that we need in the link is the href and endpoints we will reuse
// some known discoverable resource
client.WithLinkNotFoundCallback(func(links schema.ResourceLinks, href string) (schema.ResourceLink, error) {
resourceLink, ok := links.GetResourceLink(configuration.ResourceURI)
if !ok {
return schema.ResourceLink{}, fmt.Errorf("failed to get resource link: %w", err)
}
resourceLink.Href = href
return resourceLink, nil
}),
},
},
want: coap.DetailedResponse[interface{}]{
Code: codes.Content,
Body: map[interface{}]interface{}{
"value": false,
},
},
},
{
name: "invalid href",
args: args{
Expand All @@ -103,17 +146,6 @@ func TestClientGetResource(t *testing.T) {
},
}

c, err := NewTestSecureClient()
require.NoError(t, err)
defer func() {
errC := c.Close(context.Background())
require.NoError(t, errC)
}()
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout)
defer cancel()
deviceID, err = c.OwnDevice(ctx, deviceID)
require.NoError(t, err)
defer disown(t, c, deviceID)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got coap.DetailedResponse[interface{}]
Expand Down
11 changes: 3 additions & 8 deletions client/observeResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,22 +225,17 @@ func (c *Client) ObserveResource(
return getObservationID(key, resourceObservationID.String()), nil
}

d, links, err := c.GetDevice(ctx, deviceID, WithDiscoveryConfiguration(cfg.discoveryConfiguration))
device, link, err := c.GetDeviceLinkForHref(ctx, deviceID, href, cfg.discoveryConfiguration, LinkNotFoundCallback{linkNotFoundCallback: cfg.linkNotFoundCallback})
if err != nil {
return "", err
}

link, err := core.GetResourceLink(links, href)
observationID, err = device.ObserveResourceWithCodec(ctx, link, observerCodec{contentFormat: cfg.codec.ContentFormat()}, h, cfg.opts...)
if err != nil {
return "", err
}

observationID, err = d.ObserveResourceWithCodec(ctx, link, observerCodec{contentFormat: cfg.codec.ContentFormat()}, h, cfg.opts...)
if err != nil {
return "", err
}

dev, _ := c.deviceCache.UpdateOrStoreDevice(d)
dev, _ := c.deviceCache.UpdateOrStoreDevice(device)
h.observationID = observationID
h.device = dev

Expand Down
Loading
Loading