1
1
//
2
2
// Copyright (C) 2020-2021 Unknown author
3
3
// Copyright (C) 2023 Intel Corporation
4
- // Copyright (C) 2024 IOTech Ltd
4
+ // Copyright (C) 2024-2025 IOTech Ltd
5
5
//
6
6
// SPDX-License-Identifier: Apache-2.0
7
7
@@ -13,6 +13,7 @@ import (
13
13
"strconv"
14
14
"strings"
15
15
16
+ "github.com/edgexfoundry/go-mod-core-contracts/v4/clients"
16
17
"github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils"
17
18
"github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces"
18
19
"github.com/edgexfoundry/go-mod-core-contracts/v4/common"
@@ -23,22 +24,35 @@ import (
23
24
)
24
25
25
26
type DeviceClient struct {
26
- baseUrl string
27
+ baseUrlFunc clients. ClientBaseUrlFunc
27
28
authInjector interfaces.AuthenticationInjector
28
29
enableNameFieldEscape bool
29
30
}
30
31
31
32
// NewDeviceClient creates an instance of DeviceClient
32
33
func NewDeviceClient (baseUrl string , authInjector interfaces.AuthenticationInjector , enableNameFieldEscape bool ) interfaces.DeviceClient {
33
34
return & DeviceClient {
34
- baseUrl : baseUrl ,
35
+ baseUrlFunc : clients .GetDefaultClientBaseUrlFunc (baseUrl ),
36
+ authInjector : authInjector ,
37
+ enableNameFieldEscape : enableNameFieldEscape ,
38
+ }
39
+ }
40
+
41
+ // NewDeviceClientWithUrlCallback creates an instance of DeviceClient with ClientBaseUrlFunc.
42
+ func NewDeviceClientWithUrlCallback (baseUrlFunc clients.ClientBaseUrlFunc , authInjector interfaces.AuthenticationInjector , enableNameFieldEscape bool ) interfaces.DeviceClient {
43
+ return & DeviceClient {
44
+ baseUrlFunc : baseUrlFunc ,
35
45
authInjector : authInjector ,
36
46
enableNameFieldEscape : enableNameFieldEscape ,
37
47
}
38
48
}
39
49
40
50
func (dc DeviceClient ) Add (ctx context.Context , reqs []requests.AddDeviceRequest ) (res []dtoCommon.BaseWithIdResponse , err errors.EdgeX ) {
41
- err = utils .PostRequestWithRawData (ctx , & res , dc .baseUrl , common .ApiDeviceRoute , nil , reqs , dc .authInjector )
51
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
52
+ if goErr != nil {
53
+ return res , errors .NewCommonEdgeXWrapper (goErr )
54
+ }
55
+ err = utils .PostRequestWithRawData (ctx , & res , baseUrl , common .ApiDeviceRoute , nil , reqs , dc .authInjector )
42
56
if err != nil {
43
57
return res , errors .NewCommonEdgeXWrapper (err )
44
58
}
@@ -50,15 +64,23 @@ func (dc DeviceClient) AddWithQueryParams(ctx context.Context, reqs []requests.A
50
64
for k , v := range queryParams {
51
65
requestParams .Set (k , v )
52
66
}
53
- err = utils .PostRequestWithRawData (ctx , & res , dc .baseUrl , common .ApiDeviceRoute , requestParams , reqs , dc .authInjector )
67
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
68
+ if goErr != nil {
69
+ return res , errors .NewCommonEdgeXWrapper (goErr )
70
+ }
71
+ err = utils .PostRequestWithRawData (ctx , & res , baseUrl , common .ApiDeviceRoute , requestParams , reqs , dc .authInjector )
54
72
if err != nil {
55
73
return res , errors .NewCommonEdgeXWrapper (err )
56
74
}
57
75
return res , nil
58
76
}
59
77
60
78
func (dc DeviceClient ) Update (ctx context.Context , reqs []requests.UpdateDeviceRequest ) (res []dtoCommon.BaseResponse , err errors.EdgeX ) {
61
- err = utils .PatchRequest (ctx , & res , dc .baseUrl , common .ApiDeviceRoute , nil , reqs , dc .authInjector )
79
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
80
+ if goErr != nil {
81
+ return res , errors .NewCommonEdgeXWrapper (goErr )
82
+ }
83
+ err = utils .PatchRequest (ctx , & res , baseUrl , common .ApiDeviceRoute , nil , reqs , dc .authInjector )
62
84
if err != nil {
63
85
return res , errors .NewCommonEdgeXWrapper (err )
64
86
}
@@ -70,7 +92,11 @@ func (dc DeviceClient) UpdateWithQueryParams(ctx context.Context, reqs []request
70
92
for k , v := range queryParams {
71
93
requestParams .Set (k , v )
72
94
}
73
- err = utils .PatchRequest (ctx , & res , dc .baseUrl , common .ApiDeviceRoute , requestParams , reqs , dc .authInjector )
95
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
96
+ if goErr != nil {
97
+ return res , errors .NewCommonEdgeXWrapper (goErr )
98
+ }
99
+ err = utils .PatchRequest (ctx , & res , baseUrl , common .ApiDeviceRoute , requestParams , reqs , dc .authInjector )
74
100
if err != nil {
75
101
return res , errors .NewCommonEdgeXWrapper (err )
76
102
}
@@ -84,7 +110,11 @@ func (dc DeviceClient) AllDevices(ctx context.Context, labels []string, offset i
84
110
}
85
111
requestParams .Set (common .Offset , strconv .Itoa (offset ))
86
112
requestParams .Set (common .Limit , strconv .Itoa (limit ))
87
- err = utils .GetRequest (ctx , & res , dc .baseUrl , common .ApiAllDeviceRoute , requestParams , dc .authInjector )
113
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
114
+ if goErr != nil {
115
+ return res , errors .NewCommonEdgeXWrapper (goErr )
116
+ }
117
+ err = utils .GetRequest (ctx , & res , baseUrl , common .ApiAllDeviceRoute , requestParams , dc .authInjector )
88
118
if err != nil {
89
119
return res , errors .NewCommonEdgeXWrapper (err )
90
120
}
@@ -100,7 +130,11 @@ func (dc DeviceClient) AllDevicesWithChildren(ctx context.Context, parent string
100
130
requestParams .Set (common .MaxLevels , strconv .FormatUint (uint64 (maxLevels ), 10 ))
101
131
requestParams .Set (common .Offset , strconv .Itoa (offset ))
102
132
requestParams .Set (common .Limit , strconv .Itoa (limit ))
103
- err = utils .GetRequest (ctx , & res , dc .baseUrl , common .ApiAllDeviceRoute , requestParams , dc .authInjector )
133
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
134
+ if goErr != nil {
135
+ return res , errors .NewCommonEdgeXWrapper (goErr )
136
+ }
137
+ err = utils .GetRequest (ctx , & res , baseUrl , common .ApiAllDeviceRoute , requestParams , dc .authInjector )
104
138
if err != nil {
105
139
return res , errors .NewCommonEdgeXWrapper (err )
106
140
}
@@ -110,7 +144,11 @@ func (dc DeviceClient) AllDevicesWithChildren(ctx context.Context, parent string
110
144
func (dc DeviceClient ) DeviceNameExists (ctx context.Context , name string ) (res dtoCommon.BaseResponse , err errors.EdgeX ) {
111
145
path := common .NewPathBuilder ().EnableNameFieldEscape (dc .enableNameFieldEscape ).
112
146
SetPath (common .ApiDeviceRoute ).SetPath (common .Check ).SetPath (common .Name ).SetNameFieldPath (name ).BuildPath ()
113
- err = utils .GetRequest (ctx , & res , dc .baseUrl , path , nil , dc .authInjector )
147
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
148
+ if goErr != nil {
149
+ return res , errors .NewCommonEdgeXWrapper (goErr )
150
+ }
151
+ err = utils .GetRequest (ctx , & res , baseUrl , path , nil , dc .authInjector )
114
152
if err != nil {
115
153
return res , errors .NewCommonEdgeXWrapper (err )
116
154
}
@@ -120,7 +158,11 @@ func (dc DeviceClient) DeviceNameExists(ctx context.Context, name string) (res d
120
158
func (dc DeviceClient ) DeviceByName (ctx context.Context , name string ) (res responses.DeviceResponse , err errors.EdgeX ) {
121
159
path := common .NewPathBuilder ().EnableNameFieldEscape (dc .enableNameFieldEscape ).
122
160
SetPath (common .ApiDeviceRoute ).SetPath (common .Name ).SetNameFieldPath (name ).BuildPath ()
123
- err = utils .GetRequest (ctx , & res , dc .baseUrl , path , nil , dc .authInjector )
161
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
162
+ if goErr != nil {
163
+ return res , errors .NewCommonEdgeXWrapper (goErr )
164
+ }
165
+ err = utils .GetRequest (ctx , & res , baseUrl , path , nil , dc .authInjector )
124
166
if err != nil {
125
167
return res , errors .NewCommonEdgeXWrapper (err )
126
168
}
@@ -130,7 +172,11 @@ func (dc DeviceClient) DeviceByName(ctx context.Context, name string) (res respo
130
172
func (dc DeviceClient ) DeleteDeviceByName (ctx context.Context , name string ) (res dtoCommon.BaseResponse , err errors.EdgeX ) {
131
173
path := common .NewPathBuilder ().EnableNameFieldEscape (dc .enableNameFieldEscape ).
132
174
SetPath (common .ApiDeviceRoute ).SetPath (common .Name ).SetNameFieldPath (name ).BuildPath ()
133
- err = utils .DeleteRequest (ctx , & res , dc .baseUrl , path , dc .authInjector )
175
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
176
+ if goErr != nil {
177
+ return res , errors .NewCommonEdgeXWrapper (goErr )
178
+ }
179
+ err = utils .DeleteRequest (ctx , & res , baseUrl , path , dc .authInjector )
134
180
if err != nil {
135
181
return res , errors .NewCommonEdgeXWrapper (err )
136
182
}
@@ -143,7 +189,11 @@ func (dc DeviceClient) DevicesByProfileName(ctx context.Context, name string, of
143
189
requestParams := url.Values {}
144
190
requestParams .Set (common .Offset , strconv .Itoa (offset ))
145
191
requestParams .Set (common .Limit , strconv .Itoa (limit ))
146
- err = utils .GetRequest (ctx , & res , dc .baseUrl , requestPath , requestParams , dc .authInjector )
192
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
193
+ if goErr != nil {
194
+ return res , errors .NewCommonEdgeXWrapper (goErr )
195
+ }
196
+ err = utils .GetRequest (ctx , & res , baseUrl , requestPath , requestParams , dc .authInjector )
147
197
if err != nil {
148
198
return res , errors .NewCommonEdgeXWrapper (err )
149
199
}
@@ -156,7 +206,11 @@ func (dc DeviceClient) DevicesByServiceName(ctx context.Context, name string, of
156
206
requestParams := url.Values {}
157
207
requestParams .Set (common .Offset , strconv .Itoa (offset ))
158
208
requestParams .Set (common .Limit , strconv .Itoa (limit ))
159
- err = utils .GetRequest (ctx , & res , dc .baseUrl , requestPath , requestParams , dc .authInjector )
209
+ baseUrl , goErr := clients .GetBaseUrl (dc .baseUrlFunc )
210
+ if goErr != nil {
211
+ return res , errors .NewCommonEdgeXWrapper (goErr )
212
+ }
213
+ err = utils .GetRequest (ctx , & res , baseUrl , requestPath , requestParams , dc .authInjector )
160
214
if err != nil {
161
215
return res , errors .NewCommonEdgeXWrapper (err )
162
216
}
0 commit comments