@@ -10,6 +10,7 @@ import (
1010 "context"
1111 "encoding/json"
1212 "fmt"
13+ "io"
1314 "net/http"
1415 "net/url"
1516 "time"
@@ -21,6 +22,7 @@ import (
2122const (
2223 basePath = "/api/notification-service"
2324 createNotificationPath = "/notifications"
25+ registerOriginsPath = "/origins"
2426)
2527
2628// Client can be used to send notifications
@@ -94,3 +96,44 @@ func (c *Client) CreateNotification(ctx context.Context, notification Notificati
9496
9597 return err
9698}
99+
100+ func (c * Client ) RegisterOrigins (ctx context.Context , serviceID string , origins []Origin ) error {
101+ token , err := c .authClient .GetToken (ctx )
102+ if err != nil {
103+ return fmt .Errorf ("failed to get authentication token: %w" , err )
104+ }
105+
106+ originsSerialized , err := json .Marshal (origins )
107+ if err != nil {
108+ return fmt .Errorf ("failed to serialize origins: %w" , err )
109+ }
110+
111+ registerOriginsEndpoint , err := url .JoinPath (c .notificationServiceAddress , basePath , registerOriginsPath , serviceID )
112+ if err != nil {
113+ return fmt .Errorf ("invalid url '%s': %w" , c .notificationServiceAddress , err )
114+ }
115+
116+ req , err := http .NewRequestWithContext (ctx , http .MethodPut , registerOriginsEndpoint , bytes .NewReader (originsSerialized ))
117+ if err != nil {
118+ return fmt .Errorf ("failed to build request: %w" , err )
119+ }
120+
121+ req .Header .Set ("Authorization" , "Bearer " + token )
122+ req .Header .Set ("Content-Type" , "application/json" )
123+
124+ response , err := c .httpClient .Do (req )
125+ if err != nil {
126+ return fmt .Errorf ("failed to execute request: %w" , err )
127+ }
128+ defer func () { _ = response .Body .Close () }()
129+
130+ if response .StatusCode < 200 || response .StatusCode > 299 {
131+ body , err := io .ReadAll (response .Body )
132+ if err != nil {
133+ return fmt .Errorf ("failed to register origins, status: %s: %w body: %s" , response .Status , err , string (body ))
134+ }
135+ return fmt .Errorf ("failed to register origins, status: %s: %s" , response .Status , string (body ))
136+ }
137+
138+ return nil
139+ }
0 commit comments