From 61bd91ab058f1c855ec180b3d64afd937c6ababb Mon Sep 17 00:00:00 2001 From: Chris Hung Date: Thu, 16 Feb 2023 15:50:54 +0800 Subject: [PATCH] feat!: add device service name in Add Event API endpoint BREAKING CHANGE: update EventClient 'Add' method signature closes #799 Signed-off-by: Chris Hung --- clients/http/event.go | 6 +++--- clients/http/event_test.go | 7 ++++--- clients/interfaces/event.go | 4 ++-- common/constants.go | 20 ++++++++++---------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/clients/http/event.go b/clients/http/event.go index 3956f038..08b63854 100644 --- a/clients/http/event.go +++ b/clients/http/event.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2021 IOTech Ltd +// Copyright (C) 2021-2023 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -31,9 +31,9 @@ func NewEventClient(baseUrl string) interfaces.EventClient { } } -func (ec *eventClient) Add(ctx context.Context, req requests.AddEventRequest) ( +func (ec *eventClient) Add(ctx context.Context, serviceName string, req requests.AddEventRequest) ( dtoCommon.BaseWithIdResponse, errors.EdgeX) { - path := path.Join(common.ApiEventRoute, req.Event.ProfileName, req.Event.DeviceName, req.Event.SourceName) + path := path.Join(common.ApiEventRoute, serviceName, req.Event.ProfileName, req.Event.DeviceName, req.Event.SourceName) var br dtoCommon.BaseWithIdResponse bytes, encoding, err := req.Encode() diff --git a/clients/http/event_test.go b/clients/http/event_test.go index 679bf5e8..f776986d 100644 --- a/clients/http/event_test.go +++ b/clients/http/event_test.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2021 IOTech Ltd +// Copyright (C) 2021-2023 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -23,13 +23,14 @@ import ( ) func TestAddEvent(t *testing.T) { + serviceName := "serviceName" event := dtos.Event{ProfileName: "profileName", DeviceName: "deviceName"} - apiRoute := path.Join(common.ApiEventRoute, event.ProfileName, event.DeviceName) + apiRoute := path.Join(common.ApiEventRoute, serviceName, event.ProfileName, event.DeviceName, event.SourceName) ts := newTestServer(http.MethodPost, apiRoute, dtoCommon.BaseWithIdResponse{}) defer ts.Close() client := NewEventClient(ts.URL) - res, err := client.Add(context.Background(), requests.AddEventRequest{Event: event}) + res, err := client.Add(context.Background(), serviceName, requests.AddEventRequest{Event: event}) require.NoError(t, err) assert.IsType(t, dtoCommon.BaseWithIdResponse{}, res) } diff --git a/clients/interfaces/event.go b/clients/interfaces/event.go index b7bc40c9..03378a2b 100644 --- a/clients/interfaces/event.go +++ b/clients/interfaces/event.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2021 IOTech Ltd +// Copyright (C) 2021-2023 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -17,7 +17,7 @@ import ( // EventClient defines the interface for interactions with the Event endpoint on the EdgeX Foundry core-data service. type EventClient interface { // Add adds new event. - Add(ctx context.Context, req requests.AddEventRequest) (common.BaseWithIdResponse, errors.EdgeX) + Add(ctx context.Context, serviceName string, req requests.AddEventRequest) (common.BaseWithIdResponse, errors.EdgeX) // AllEvents returns all events sorted in descending order of created time. // The result can be limited in a certain range by specifying the offset and limit parameters. // offset: The number of items to skip before starting to collect the result set. Default is 0. diff --git a/common/constants.go b/common/constants.go index 14c21f36..f0a145e5 100644 --- a/common/constants.go +++ b/common/constants.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2020-2022 IOTech Ltd +// Copyright (C) 2020-2023 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -10,15 +10,15 @@ const ( ApiVersion = "v2" ApiBase = "/api/v2" - ApiEventRoute = ApiBase + "/event" - ApiEventProfileNameDeviceNameSourceNameRoute = ApiEventRoute + "/{" + ProfileName + "}" + "/{" + DeviceName + "}" + "/{" + SourceName + "}" - ApiAllEventRoute = ApiEventRoute + "/" + All - ApiEventIdRoute = ApiEventRoute + "/" + Id + "/{" + Id + "}" - ApiEventCountRoute = ApiEventRoute + "/" + Count - ApiEventCountByDeviceNameRoute = ApiEventCountRoute + "/" + Device + "/" + Name + "/{" + Name + "}" - ApiEventByDeviceNameRoute = ApiEventRoute + "/" + Device + "/" + Name + "/{" + Name + "}" - ApiEventByTimeRangeRoute = ApiEventRoute + "/" + Start + "/{" + Start + "}/" + End + "/{" + End + "}" - ApiEventByAgeRoute = ApiEventRoute + "/" + Age + "/{" + Age + "}" + ApiEventRoute = ApiBase + "/event" + ApiEventServiceNameProfileNameDeviceNameSourceNameRoute = ApiEventRoute + "/{" + ServiceName + "}" + "/{" + ProfileName + "}" + "/{" + DeviceName + "}" + "/{" + SourceName + "}" + ApiAllEventRoute = ApiEventRoute + "/" + All + ApiEventIdRoute = ApiEventRoute + "/" + Id + "/{" + Id + "}" + ApiEventCountRoute = ApiEventRoute + "/" + Count + ApiEventCountByDeviceNameRoute = ApiEventCountRoute + "/" + Device + "/" + Name + "/{" + Name + "}" + ApiEventByDeviceNameRoute = ApiEventRoute + "/" + Device + "/" + Name + "/{" + Name + "}" + ApiEventByTimeRangeRoute = ApiEventRoute + "/" + Start + "/{" + Start + "}/" + End + "/{" + End + "}" + ApiEventByAgeRoute = ApiEventRoute + "/" + Age + "/{" + Age + "}" ApiReadingRoute = ApiBase + "/reading" ApiAllReadingRoute = ApiReadingRoute + "/" + All