9
9
10
10
"github.com/tidepool-org/platform/auth"
11
11
"github.com/tidepool-org/platform/client"
12
+ "github.com/tidepool-org/platform/errors"
12
13
platformlog "github.com/tidepool-org/platform/log"
13
14
"github.com/tidepool-org/platform/log/null"
14
15
"github.com/tidepool-org/platform/platform"
@@ -43,6 +44,8 @@ type PlatformClient interface {
43
44
requestBody interface {}, responseBody interface {}, inspectors ... request.ResponseInspector ) error
44
45
}
45
46
47
+ // TokenProvider retrieves session tokens for calling the alerts API.
48
+ //
46
49
// client.External is one implementation
47
50
type TokenProvider interface {
48
51
// ServerSessionToken provides a server-to-server API authentication token.
@@ -51,12 +54,12 @@ type TokenProvider interface {
51
54
52
55
// request performs common operations before passing a request off to the
53
56
// 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 {
55
58
// Platform's client.Client expects a logger to exist in the request's
56
59
// context. If it doesn't exist, request processing will panic.
57
60
loggingCtx := platformlog .NewContextWithLogger (ctx , c .logger )
58
61
// 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 )
60
63
}
61
64
62
65
// 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
65
68
// platform.Client. It might be nice to be able to use a mutator, but the auth
66
69
// is specifically handled by the platform.Client via the context field, and
67
70
// 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 {
69
72
authCtx , err := c .ctxWithAuth (ctx )
70
73
if err != nil {
71
74
return err
72
75
}
73
- return c .client .RequestData (authCtx , method , url , nil , body , nil )
76
+ return c .client .RequestData (authCtx , method , url , nil , reqBody , resBody )
74
77
}
75
78
76
79
// Upsert updates cfg if it exists or creates it if it doesn't.
77
80
func (c * Client ) Upsert (ctx context.Context , cfg * Config ) error {
78
81
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 )
80
83
}
81
84
82
85
// Delete the alerts config.
83
86
func (c * Client ) Delete (ctx context.Context , cfg * Config ) error {
84
87
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
86
113
}
87
114
88
115
// ctxWithAuth injects a server session token into the context.
0 commit comments