99
1010 "github.com/tidepool-org/platform/auth"
1111 "github.com/tidepool-org/platform/client"
12+ "github.com/tidepool-org/platform/errors"
1213 platformlog "github.com/tidepool-org/platform/log"
1314 "github.com/tidepool-org/platform/log/null"
1415 "github.com/tidepool-org/platform/platform"
@@ -43,6 +44,8 @@ type PlatformClient interface {
4344 requestBody interface {}, responseBody interface {}, inspectors ... request.ResponseInspector ) error
4445}
4546
47+ // TokenProvider retrieves session tokens for calling the alerts API.
48+ //
4649// client.External is one implementation
4750type TokenProvider interface {
4851 // ServerSessionToken provides a server-to-server API authentication token.
@@ -51,12 +54,12 @@ type TokenProvider interface {
5154
5255// request performs common operations before passing a request off to the
5356// underlying platform.Client.
54- func (c * Client ) request (ctx context.Context , method , url string , body any ) error {
57+ func (c * Client ) request (ctx context.Context , method , url string , reqBody , resBody any ) error {
5558 // Platform's client.Client expects a logger to exist in the request's
5659 // context. If it doesn't exist, request processing will panic.
5760 loggingCtx := platformlog .NewContextWithLogger (ctx , c .logger )
5861 // Make sure the auth token is injected into the request's headers.
59- return c .requestWithAuth (loggingCtx , method , url , body )
62+ return c .requestWithAuth (loggingCtx , method , url , reqBody , resBody )
6063}
6164
6265// requestWithAuth injects an auth token before calling platform.Client.RequestData.
@@ -65,24 +68,48 @@ func (c *Client) request(ctx context.Context, method, url string, body any) erro
6568// platform.Client. It might be nice to be able to use a mutator, but the auth
6669// is specifically handled by the platform.Client via the context field, and
6770// if left blank, platform.Client errors.
68- func (c * Client ) requestWithAuth (ctx context.Context , method , url string , body any ) error {
71+ func (c * Client ) requestWithAuth (ctx context.Context , method , url string , reqBody , resBody any ) error {
6972 authCtx , err := c .ctxWithAuth (ctx )
7073 if err != nil {
7174 return err
7275 }
73- return c .client .RequestData (authCtx , method , url , nil , body , nil )
76+ return c .client .RequestData (authCtx , method , url , nil , reqBody , resBody )
7477}
7578
7679// Upsert updates cfg if it exists or creates it if it doesn't.
7780func (c * Client ) Upsert (ctx context.Context , cfg * Config ) error {
7881 url := c .client .ConstructURL ("v1" , "users" , cfg .FollowedUserID , "followers" , cfg .UserID , "alerts" )
79- return c .request (ctx , http .MethodPost , url , cfg )
82+ return c .request (ctx , http .MethodPost , url , cfg , nil )
8083}
8184
8285// Delete the alerts config.
8386func (c * Client ) Delete (ctx context.Context , cfg * Config ) error {
8487 url := c .client .ConstructURL ("v1" , "users" , cfg .FollowedUserID , "followers" , cfg .UserID , "alerts" )
85- return c .request (ctx , http .MethodDelete , url , nil )
88+ return c .request (ctx , http .MethodDelete , url , nil , nil )
89+ }
90+
91+ // Get a user's alerts configuration for the followed user.
92+ func (c * Client ) Get (ctx context.Context , followedUserID , userID string ) (* Config , error ) {
93+ url := c .client .ConstructURL ("v1" , "users" , followedUserID , "followers" , userID , "alerts" )
94+ cfg := & Config {}
95+ err := c .request (ctx , http .MethodGet , url , nil , cfg )
96+ if err != nil {
97+ return nil , errors .Wrap (err , "Unable to request alerts config" )
98+ }
99+ return cfg , nil
100+ }
101+
102+ // List the alerts configurations that follow the given user.
103+ //
104+ // This method should only be called via an authenticated service session.
105+ func (c * Client ) List (ctx context.Context , followedUserID string ) ([]* Config , error ) {
106+ url := c .client .ConstructURL ("v1" , "users" , followedUserID , "followers" , "alerts" )
107+ configs := []* Config {}
108+ err := c .request (ctx , http .MethodGet , url , nil , & configs )
109+ if err != nil {
110+ return nil , errors .Wrap (err , "Unable to request alerts configs list" )
111+ }
112+ return configs , nil
86113}
87114
88115// ctxWithAuth injects a server session token into the context.
0 commit comments