-
Notifications
You must be signed in to change notification settings - Fork 67
/
event.go
128 lines (113 loc) · 4.33 KB
/
event.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
//
// Copyright (C) 2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0
package http
import (
"context"
"net/url"
"path"
"strconv"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/http/utils"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/requests"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/responses"
"github.com/edgexfoundry/go-mod-core-contracts/v3/errors"
)
type eventClient struct {
baseUrl string
}
// NewEventClient creates an instance of EventClient
func NewEventClient(baseUrl string) interfaces.EventClient {
return &eventClient{
baseUrl: baseUrl,
}
}
func (ec *eventClient) Add(ctx context.Context, req requests.AddEventRequest) (
dtoCommon.BaseWithIdResponse, errors.EdgeX) {
path := path.Join(common.ApiEventRoute, req.Event.ProfileName, req.Event.DeviceName, req.Event.SourceName)
var br dtoCommon.BaseWithIdResponse
bytes, encoding, err := req.Encode()
if err != nil {
return br, errors.NewCommonEdgeXWrapper(err)
}
err = utils.PostRequest(ctx, &br, ec.baseUrl, path, bytes, encoding)
if err != nil {
return br, errors.NewCommonEdgeXWrapper(err)
}
return br, nil
}
func (ec *eventClient) AllEvents(ctx context.Context, offset, limit int) (responses.MultiEventsResponse, errors.EdgeX) {
requestParams := url.Values{}
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
res := responses.MultiEventsResponse{}
err := utils.GetRequest(ctx, &res, ec.baseUrl, common.ApiAllEventRoute, requestParams)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}
func (ec *eventClient) EventCount(ctx context.Context) (dtoCommon.CountResponse, errors.EdgeX) {
res := dtoCommon.CountResponse{}
err := utils.GetRequest(ctx, &res, ec.baseUrl, common.ApiEventCountRoute, nil)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}
func (ec *eventClient) EventCountByDeviceName(ctx context.Context, name string) (dtoCommon.CountResponse, errors.EdgeX) {
requestPath := path.Join(common.ApiEventCountRoute, common.Device, common.Name, name)
res := dtoCommon.CountResponse{}
err := utils.GetRequest(ctx, &res, ec.baseUrl, requestPath, nil)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}
func (ec *eventClient) EventsByDeviceName(ctx context.Context, name string, offset, limit int) (
responses.MultiEventsResponse, errors.EdgeX) {
requestPath := path.Join(common.ApiEventRoute, common.Device, common.Name, name)
requestParams := url.Values{}
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
res := responses.MultiEventsResponse{}
err := utils.GetRequest(ctx, &res, ec.baseUrl, requestPath, requestParams)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}
func (ec *eventClient) DeleteByDeviceName(ctx context.Context, name string) (dtoCommon.BaseResponse, errors.EdgeX) {
path := path.Join(common.ApiEventRoute, common.Device, common.Name, name)
res := dtoCommon.BaseResponse{}
err := utils.DeleteRequest(ctx, &res, ec.baseUrl, path)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}
func (ec *eventClient) EventsByTimeRange(ctx context.Context, start, end, offset, limit int) (
responses.MultiEventsResponse, errors.EdgeX) {
requestPath := path.Join(common.ApiEventRoute, common.Start, strconv.Itoa(start), common.End, strconv.Itoa(end))
requestParams := url.Values{}
requestParams.Set(common.Offset, strconv.Itoa(offset))
requestParams.Set(common.Limit, strconv.Itoa(limit))
res := responses.MultiEventsResponse{}
err := utils.GetRequest(ctx, &res, ec.baseUrl, requestPath, requestParams)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}
func (ec *eventClient) DeleteByAge(ctx context.Context, age int) (dtoCommon.BaseResponse, errors.EdgeX) {
path := path.Join(common.ApiEventRoute, common.Age, strconv.Itoa(age))
res := dtoCommon.BaseResponse{}
err := utils.DeleteRequest(ctx, &res, ec.baseUrl, path)
if err != nil {
return res, errors.NewCommonEdgeXWrapper(err)
}
return res, nil
}