@@ -17,10 +17,11 @@ import (
17
17
)
18
18
19
19
type NgsiV2Client struct {
20
- c * http.Client
21
- url string
22
- timeout time.Duration
23
- apiRes * model.APIResources
20
+ c * http.Client
21
+ url string
22
+ timeout time.Duration
23
+ apiRes * model.APIResources
24
+ customGlobalHeaders map [string ]string
24
25
}
25
26
26
27
// ClientOptionFunc is a function that configures a NgsiV2Client.
@@ -29,7 +30,8 @@ type ClientOptionFunc func(*NgsiV2Client) error
29
30
// NewNgsiV2Client creates a new NGSIv2 client.
30
31
func NewNgsiV2Client (options ... ClientOptionFunc ) (* NgsiV2Client , error ) {
31
32
c := & NgsiV2Client {
32
- timeout : time .Second * 15 ,
33
+ timeout : time .Second * 15 ,
34
+ customGlobalHeaders : make (map [string ]string ),
33
35
}
34
36
35
37
// apply the options
@@ -62,18 +64,33 @@ func SetUrl(url string) ClientOptionFunc {
62
64
}
63
65
}
64
66
67
+ // SetGlobalHeader is used a custom header applied to all the requests
68
+ // made to the context broker
69
+ func SetGlobalHeader (key string , value string ) ClientOptionFunc {
70
+ return func (c * NgsiV2Client ) error {
71
+ c .customGlobalHeaders [key ] = value
72
+ return nil
73
+ }
74
+ }
75
+
65
76
type additionalHeader struct {
66
77
key string
67
78
value string
68
79
}
69
80
70
- func newRequest (method , url string , body io.Reader , additionalHeaders ... additionalHeader ) (* http.Request , error ) {
81
+ func ( c * NgsiV2Client ) newRequest (method , url string , body io.Reader , additionalHeaders ... additionalHeader ) (* http.Request , error ) {
71
82
req , err := http .NewRequest (method , url , body )
72
83
if err != nil {
73
84
return nil , err
74
85
}
75
86
req .Header .Add ("User-Agent" , "ngsiv2-client" )
76
87
req .Header .Add ("Accept" , "application/json" )
88
+
89
+ // set the global headers
90
+ for header , value := range c .customGlobalHeaders {
91
+ req .Header .Add (header , value )
92
+ }
93
+
77
94
for _ , ah := range additionalHeaders {
78
95
req .Header .Add (ah .key , ah .value )
79
96
}
@@ -85,7 +102,7 @@ func (c *NgsiV2Client) BatchUpdate(msg *model.BatchUpdate) error {
85
102
if err != nil {
86
103
return fmt .Errorf ("Could not serialize message: %+v" , err )
87
104
}
88
- req , err := newRequest ("POST" , fmt .Sprintf ("%s/v2/op/update" , c .url ), bytes .NewBuffer (jsonValue ))
105
+ req , err := c . newRequest ("POST" , fmt .Sprintf ("%s/v2/op/update" , c .url ), bytes .NewBuffer (jsonValue ))
89
106
if err != nil {
90
107
return fmt .Errorf ("Could not create request for batch update: %+v" , err )
91
108
}
@@ -116,7 +133,7 @@ func (c *NgsiV2Client) BatchQuery(msg *model.BatchQuery, options ...BatchQueryPa
116
133
if err != nil {
117
134
return nil , fmt .Errorf ("could not serialize message: %+v" , err )
118
135
}
119
- req , err := newRequest ("POST" , fmt .Sprintf ("%s/v2/op/query" , c .url ), bytes .NewBuffer (jsonValue ))
136
+ req , err := c . newRequest ("POST" , fmt .Sprintf ("%s/v2/op/query" , c .url ), bytes .NewBuffer (jsonValue ))
120
137
if err != nil {
121
138
return nil , fmt .Errorf ("could not create request for batch query: %+v" , err )
122
139
}
@@ -209,7 +226,7 @@ func BatchQuerySetOptions(opts string) BatchQueryParamFunc {
209
226
// RetrieveAPIResources gives url link values for retrieving resources.
210
227
// See: https://orioncontextbroker.docs.apiary.io/#reference/api-entry-point/retrieve-api-resources/retrieve-api-resources
211
228
func (c * NgsiV2Client ) RetrieveAPIResources () (* model.APIResources , error ) {
212
- req , err := newRequest ("GET" , fmt .Sprintf ("%s/v2" , c .url ), nil )
229
+ req , err := c . newRequest ("GET" , fmt .Sprintf ("%s/v2" , c .url ), nil )
213
230
if err != nil {
214
231
return nil , fmt .Errorf ("Could not create request for API resources: %+v" , err )
215
232
}
@@ -355,7 +372,7 @@ func (c *NgsiV2Client) RetrieveEntity(id string, options ...RetrieveEntityParamF
355
372
return nil , err
356
373
}
357
374
358
- req , err := newRequest ("GET" , fmt .Sprintf ("%s/%s" , eUrl , params .id ), nil , params .headers ()... )
375
+ req , err := c . newRequest ("GET" , fmt .Sprintf ("%s/%s" , eUrl , params .id ), nil , params .headers ()... )
359
376
if err != nil {
360
377
return nil , fmt .Errorf ("Could not create request for API resources: %+v" , err )
361
378
}
@@ -562,7 +579,7 @@ func (c *NgsiV2Client) ListEntities(options ...ListEntitiesParamFunc) ([]*model.
562
579
return nil , err
563
580
}
564
581
565
- req , err := newRequest ("GET" , fmt .Sprintf ("%s" , eUrl ), nil , params .headers ()... )
582
+ req , err := c . newRequest ("GET" , fmt .Sprintf ("%s" , eUrl ), nil , params .headers ()... )
566
583
if err != nil {
567
584
return nil , fmt .Errorf ("Could not create request for API resources: %+v" , err )
568
585
}
@@ -647,7 +664,7 @@ func (c *NgsiV2Client) CountEntities(options ...ListEntitiesParamFunc) (int, err
647
664
return 0 , err
648
665
}
649
666
650
- req , err := newRequest ("GET" , fmt .Sprintf ("%s" , eUrl ), nil , params .headers ()... )
667
+ req , err := c . newRequest ("GET" , fmt .Sprintf ("%s" , eUrl ), nil , params .headers ()... )
651
668
if err != nil {
652
669
return 0 , fmt .Errorf ("Could not create request for API resources: %+v" , err )
653
670
}
@@ -777,7 +794,7 @@ func (c *NgsiV2Client) CreateEntity(entity *model.Entity, options ...CreateEntit
777
794
if err != nil {
778
795
return "" , false , fmt .Errorf ("Could not serialize message: %v" , err )
779
796
}
780
- req , err := newRequest ("POST" , eUrl , bytes .NewBuffer (jsonEntity ), params .headers ()... )
797
+ req , err := c . newRequest ("POST" , eUrl , bytes .NewBuffer (jsonEntity ), params .headers ()... )
781
798
if err != nil {
782
799
return "" , false , fmt .Errorf ("Could not create request for batch update: %v" , err )
783
800
}
@@ -851,7 +868,7 @@ func (c *NgsiV2Client) CreateSubscription(subscription *model.Subscription, opti
851
868
if err != nil {
852
869
return "" , err
853
870
}
854
- req , err := newRequest ("POST" , sUrl , bytes .NewBuffer (jsonValue ), params .headers ()... )
871
+ req , err := c . newRequest ("POST" , sUrl , bytes .NewBuffer (jsonValue ), params .headers ()... )
855
872
if err != nil {
856
873
return "" , fmt .Errorf ("Could not create request for subscription creation: %+v" , err )
857
874
}
@@ -879,7 +896,7 @@ func (c *NgsiV2Client) RetrieveSubscription(id string) (*model.Subscription, err
879
896
if err != nil {
880
897
return nil , err
881
898
}
882
- req , err := newRequest ("GET" , fmt .Sprintf ("%s/%s" , sUrl , id ), nil )
899
+ req , err := c . newRequest ("GET" , fmt .Sprintf ("%s/%s" , sUrl , id ), nil )
883
900
if err != nil {
884
901
return nil , fmt .Errorf ("Could not create request for subscription retrieval: %+v" , err )
885
902
}
@@ -976,7 +993,7 @@ func (c *NgsiV2Client) RetrieveSubscriptions(options ...RetrieveSubscriptionsPar
976
993
if err != nil {
977
994
return nil , err
978
995
}
979
- req , err := newRequest ("GET" , sUrl , nil , params .headers ()... )
996
+ req , err := c . newRequest ("GET" , sUrl , nil , params .headers ()... )
980
997
if err != nil {
981
998
return nil , fmt .Errorf ("Could not create request for subscriptions retrieval: %+v" , err )
982
999
}
@@ -1041,7 +1058,7 @@ func (c *NgsiV2Client) UpdateSubscription(id string, patchSubscription *model.Su
1041
1058
}
1042
1059
}
1043
1060
1044
- req , err := newRequest ("PATCH" , fmt .Sprintf ("%s/%s" , sUrl , id ), bytes .NewBuffer (jsonValue ), params .headers ()... )
1061
+ req , err := c . newRequest ("PATCH" , fmt .Sprintf ("%s/%s" , sUrl , id ), bytes .NewBuffer (jsonValue ), params .headers ()... )
1045
1062
if err != nil {
1046
1063
return fmt .Errorf ("Could not create request for subscription updating: %+v" , err )
1047
1064
}
@@ -1079,7 +1096,7 @@ func (c *NgsiV2Client) DeleteSubscription(id string, options ...SubscriptionPara
1079
1096
}
1080
1097
}
1081
1098
1082
- req , err := newRequest ("DELETE" , fmt .Sprintf ("%s/%s" , sUrl , id ), nil , params .headers ()... )
1099
+ req , err := c . newRequest ("DELETE" , fmt .Sprintf ("%s/%s" , sUrl , id ), nil , params .headers ()... )
1083
1100
if err != nil {
1084
1101
return fmt .Errorf ("Could not create request for subscription deletion: %+v" , err )
1085
1102
}
0 commit comments