Skip to content

Commit a27a5d4

Browse files
authored
Merge pull request #3535 from infrahq/dnephin/api-client-methods-ctx-2
Add context.Context argument to api.Client methods - part 2
2 parents bcb85e0 + f49c20b commit a27a5d4

18 files changed

+146
-166
lines changed

api/client.go

+64-76
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,11 @@ func get[Res any](ctx context.Context, client Client, path string, query Query)
173173
return request[Res](client, req)
174174
}
175175

176-
func post[Req, Res any](client Client, path string, req *Req) (*Res, error) {
176+
func post[Res any](ctx context.Context, client Client, path string, req any) (*Res, error) {
177177
body, err := encodeRequestBody(req)
178178
if err != nil {
179179
return nil, err
180180
}
181-
ctx := context.TODO()
182181
httpReq, err := client.buildRequest(ctx, http.MethodPost, path, nil, body)
183182
if err != nil {
184183
return nil, err
@@ -197,34 +196,31 @@ func encodeRequestBody(req any) (io.Reader, error) {
197196
return bytes.NewReader(b), nil
198197
}
199198

200-
func put[Req, Res any](client Client, path string, req *Req) (*Res, error) {
199+
func put[Res any](ctx context.Context, client Client, path string, req any) (*Res, error) {
201200
body, err := encodeRequestBody(req)
202201
if err != nil {
203202
return nil, err
204203
}
205-
ctx := context.TODO()
206204
httpReq, err := client.buildRequest(ctx, http.MethodPut, path, nil, body)
207205
if err != nil {
208206
return nil, err
209207
}
210208
return request[Res](client, httpReq)
211209
}
212210

213-
func patch[Req, Res any](client Client, path string, req *Req) (*Res, error) {
211+
func patch[Res any](ctx context.Context, client Client, path string, req any) (*Res, error) {
214212
body, err := encodeRequestBody(req)
215213
if err != nil {
216214
return nil, err
217215
}
218-
ctx := context.TODO()
219216
httpReq, err := client.buildRequest(ctx, http.MethodPatch, path, nil, body)
220217
if err != nil {
221218
return nil, err
222219
}
223220
return request[Res](client, httpReq)
224221
}
225222

226-
func delete(client Client, path string, query Query) error {
227-
ctx := context.TODO()
223+
func delete(ctx context.Context, client Client, path string, query Query) error {
228224
httpReq, err := client.buildRequest(ctx, http.MethodDelete, path, query, nil)
229225
if err != nil {
230226
return err
@@ -244,31 +240,28 @@ func (c Client) ListUsers(ctx context.Context, req ListUsersRequest) (*ListRespo
244240
})
245241
}
246242

247-
func (c Client) GetUser(id uid.ID) (*User, error) {
248-
ctx := context.TODO()
243+
func (c Client) GetUser(ctx context.Context, id uid.ID) (*User, error) {
249244
return get[User](ctx, c, fmt.Sprintf("/api/users/%s", id), Query{})
250245
}
251246

252-
func (c Client) CreateUser(req *CreateUserRequest) (*CreateUserResponse, error) {
253-
return post[CreateUserRequest, CreateUserResponse](c, "/api/users", req)
247+
func (c Client) CreateUser(ctx context.Context, req *CreateUserRequest) (*CreateUserResponse, error) {
248+
return post[CreateUserResponse](ctx, c, "/api/users", req)
254249
}
255250

256-
func (c Client) UpdateUser(req *UpdateUserRequest) (*User, error) {
257-
return put[UpdateUserRequest, User](c, fmt.Sprintf("/api/users/%s", req.ID.String()), req)
251+
func (c Client) UpdateUser(ctx context.Context, req *UpdateUserRequest) (*User, error) {
252+
return put[User](ctx, c, fmt.Sprintf("/api/users/%s", req.ID.String()), req)
258253
}
259254

260-
func (c Client) DeleteUser(id uid.ID) error {
261-
return delete(c, fmt.Sprintf("/api/users/%s", id), Query{})
255+
func (c Client) DeleteUser(ctx context.Context, id uid.ID) error {
256+
return delete(ctx, c, fmt.Sprintf("/api/users/%s", id), Query{})
262257
}
263258

264-
func (c Client) StartDeviceFlow() (*DeviceFlowResponse, error) {
265-
return post[EmptyRequest, DeviceFlowResponse](c, "/api/device", nil)
259+
func (c Client) StartDeviceFlow(ctx context.Context) (*DeviceFlowResponse, error) {
260+
return post[DeviceFlowResponse](ctx, c, "/api/device", nil)
266261
}
267262

268-
func (c Client) GetDeviceFlowStatus(req *DeviceFlowStatusRequest) (*DeviceFlowStatusResponse, error) {
269-
return post[DeviceFlowStatusRequest, DeviceFlowStatusResponse](c, "/api/device/status", &DeviceFlowStatusRequest{
270-
DeviceCode: req.DeviceCode,
271-
})
263+
func (c Client) GetDeviceFlowStatus(ctx context.Context, req *DeviceFlowStatusRequest) (*DeviceFlowStatusResponse, error) {
264+
return post[DeviceFlowStatusResponse](ctx, c, "/api/device/status", req)
272265
}
273266

274267
func (c Client) ListGroups(ctx context.Context, req ListGroupsRequest) (*ListResponse[Group], error) {
@@ -278,21 +271,20 @@ func (c Client) ListGroups(ctx context.Context, req ListGroupsRequest) (*ListRes
278271
})
279272
}
280273

281-
func (c Client) GetGroup(id uid.ID) (*Group, error) {
282-
ctx := context.TODO()
274+
func (c Client) GetGroup(ctx context.Context, id uid.ID) (*Group, error) {
283275
return get[Group](ctx, c, fmt.Sprintf("/api/groups/%s", id), Query{})
284276
}
285277

286-
func (c Client) CreateGroup(req *CreateGroupRequest) (*Group, error) {
287-
return post[CreateGroupRequest, Group](c, "/api/groups", req)
278+
func (c Client) CreateGroup(ctx context.Context, req *CreateGroupRequest) (*Group, error) {
279+
return post[Group](ctx, c, "/api/groups", req)
288280
}
289281

290-
func (c Client) DeleteGroup(id uid.ID) error {
291-
return delete(c, fmt.Sprintf("/api/groups/%s", id), Query{})
282+
func (c Client) DeleteGroup(ctx context.Context, id uid.ID) error {
283+
return delete(ctx, c, fmt.Sprintf("/api/groups/%s", id), Query{})
292284
}
293285

294-
func (c Client) UpdateUsersInGroup(req *UpdateUsersInGroupRequest) error {
295-
_, err := patch[UpdateUsersInGroupRequest, EmptyResponse](c, fmt.Sprintf("/api/groups/%s/users", req.GroupID), req)
286+
func (c Client) UpdateUsersInGroup(ctx context.Context, req *UpdateUsersInGroupRequest) error {
287+
_, err := patch[EmptyResponse](ctx, c, fmt.Sprintf("/api/groups/%s/users", req.GroupID), req)
296288
return err
297289
}
298290

@@ -309,38 +301,36 @@ func (c Client) ListOrganizations(ctx context.Context, req ListOrganizationsRequ
309301
})
310302
}
311303

312-
func (c Client) GetOrganization(id uid.ID) (*Organization, error) {
313-
ctx := context.TODO()
304+
func (c Client) GetOrganization(ctx context.Context, id uid.ID) (*Organization, error) {
314305
return get[Organization](ctx, c, fmt.Sprintf("/api/organizations/%s", id), Query{})
315306
}
316307

317-
func (c Client) CreateOrganization(req *CreateOrganizationRequest) (*Organization, error) {
318-
return post[CreateOrganizationRequest, Organization](c, "/api/organizations", req)
308+
func (c Client) CreateOrganization(ctx context.Context, req *CreateOrganizationRequest) (*Organization, error) {
309+
return post[Organization](ctx, c, "/api/organizations", req)
319310
}
320311

321-
func (c Client) DeleteOrganization(id uid.ID) error {
322-
return delete(c, fmt.Sprintf("/api/organizations/%s", id), Query{})
312+
func (c Client) DeleteOrganization(ctx context.Context, id uid.ID) error {
313+
return delete(ctx, c, fmt.Sprintf("/api/organizations/%s", id), Query{})
323314
}
324315

325-
func (c Client) GetProvider(id uid.ID) (*Provider, error) {
326-
ctx := context.TODO()
316+
func (c Client) GetProvider(ctx context.Context, id uid.ID) (*Provider, error) {
327317
return get[Provider](ctx, c, fmt.Sprintf("/api/providers/%s", id), Query{})
328318
}
329319

330-
func (c Client) CreateProvider(req *CreateProviderRequest) (*Provider, error) {
331-
return post[CreateProviderRequest, Provider](c, "/api/providers", req)
320+
func (c Client) CreateProvider(ctx context.Context, req *CreateProviderRequest) (*Provider, error) {
321+
return post[Provider](ctx, c, "/api/providers", req)
332322
}
333323

334-
func (c Client) PatchProvider(req PatchProviderRequest) (*Provider, error) {
335-
return patch[PatchProviderRequest, Provider](c, fmt.Sprintf("/api/providers/%s", req.ID.String()), &req)
324+
func (c Client) PatchProvider(ctx context.Context, req PatchProviderRequest) (*Provider, error) {
325+
return patch[Provider](ctx, c, fmt.Sprintf("/api/providers/%s", req.ID.String()), &req)
336326
}
337327

338-
func (c Client) UpdateProvider(req UpdateProviderRequest) (*Provider, error) {
339-
return put[UpdateProviderRequest, Provider](c, fmt.Sprintf("/api/providers/%s", req.ID.String()), &req)
328+
func (c Client) UpdateProvider(ctx context.Context, req UpdateProviderRequest) (*Provider, error) {
329+
return put[Provider](ctx, c, fmt.Sprintf("/api/providers/%s", req.ID.String()), &req)
340330
}
341331

342-
func (c Client) DeleteProvider(id uid.ID) error {
343-
return delete(c, fmt.Sprintf("/api/providers/%s", id), Query{})
332+
func (c Client) DeleteProvider(ctx context.Context, id uid.ID) error {
333+
return delete(ctx, c, fmt.Sprintf("/api/providers/%s", id), Query{})
344334
}
345335

346336
func (c Client) ListGrants(ctx context.Context, req ListGrantsRequest) (*ListResponse[Grant], error) {
@@ -358,12 +348,12 @@ func (c Client) ListGrants(ctx context.Context, req ListGrantsRequest) (*ListRes
358348
})
359349
}
360350

361-
func (c Client) CreateGrant(req *GrantRequest) (*CreateGrantResponse, error) {
362-
return post[GrantRequest, CreateGrantResponse](c, "/api/grants", req)
351+
func (c Client) CreateGrant(ctx context.Context, req *GrantRequest) (*CreateGrantResponse, error) {
352+
return post[CreateGrantResponse](ctx, c, "/api/grants", req)
363353
}
364354

365-
func (c Client) DeleteGrant(id uid.ID) error {
366-
return delete(c, fmt.Sprintf("/api/grants/%s", id), Query{})
355+
func (c Client) DeleteGrant(ctx context.Context, id uid.ID) error {
356+
return delete(ctx, c, fmt.Sprintf("/api/grants/%s", id), Query{})
367357
}
368358

369359
func (c Client) ListDestinations(ctx context.Context, req ListDestinationsRequest) (*ListResponse[Destination], error) {
@@ -374,16 +364,16 @@ func (c Client) ListDestinations(ctx context.Context, req ListDestinationsReques
374364
})
375365
}
376366

377-
func (c Client) CreateDestination(req *CreateDestinationRequest) (*Destination, error) {
378-
return post[CreateDestinationRequest, Destination](c, "/api/destinations", req)
367+
func (c Client) CreateDestination(ctx context.Context, req *CreateDestinationRequest) (*Destination, error) {
368+
return post[Destination](ctx, c, "/api/destinations", req)
379369
}
380370

381-
func (c Client) UpdateDestination(req UpdateDestinationRequest) (*Destination, error) {
382-
return put[UpdateDestinationRequest, Destination](c, fmt.Sprintf("/api/destinations/%s", req.ID.String()), &req)
371+
func (c Client) UpdateDestination(ctx context.Context, req UpdateDestinationRequest) (*Destination, error) {
372+
return put[Destination](ctx, c, fmt.Sprintf("/api/destinations/%s", req.ID.String()), &req)
383373
}
384374

385-
func (c Client) DeleteDestination(id uid.ID) error {
386-
return delete(c, fmt.Sprintf("/api/destinations/%s", id), Query{})
375+
func (c Client) DeleteDestination(ctx context.Context, id uid.ID) error {
376+
return delete(ctx, c, fmt.Sprintf("/api/destinations/%s", id), Query{})
387377
}
388378

389379
func (c Client) ListAccessKeys(ctx context.Context, req ListAccessKeysRequest) (*ListResponse[AccessKey], error) {
@@ -395,47 +385,45 @@ func (c Client) ListAccessKeys(ctx context.Context, req ListAccessKeysRequest) (
395385
})
396386
}
397387

398-
func (c Client) CreateAccessKey(req *CreateAccessKeyRequest) (*CreateAccessKeyResponse, error) {
399-
return post[CreateAccessKeyRequest, CreateAccessKeyResponse](c, "/api/access-keys", req)
388+
func (c Client) CreateAccessKey(ctx context.Context, req *CreateAccessKeyRequest) (*CreateAccessKeyResponse, error) {
389+
return post[CreateAccessKeyResponse](ctx, c, "/api/access-keys", req)
400390
}
401391

402-
func (c Client) DeleteAccessKey(id uid.ID) error {
403-
return delete(c, fmt.Sprintf("/api/access-keys/%s", id), Query{})
392+
func (c Client) DeleteAccessKey(ctx context.Context, id uid.ID) error {
393+
return delete(ctx, c, fmt.Sprintf("/api/access-keys/%s", id), Query{})
404394
}
405395

406-
func (c Client) DeleteAccessKeyByName(name string) error {
407-
return delete(c, "/api/access-keys", Query{"name": []string{name}})
396+
func (c Client) DeleteAccessKeyByName(ctx context.Context, name string) error {
397+
return delete(ctx, c, "/api/access-keys", Query{"name": []string{name}})
408398
}
409399

410-
func (c Client) CreateToken() (*CreateTokenResponse, error) {
411-
return post[EmptyRequest, CreateTokenResponse](c, "/api/tokens", &EmptyRequest{})
400+
func (c Client) CreateToken(ctx context.Context) (*CreateTokenResponse, error) {
401+
return post[CreateTokenResponse](ctx, c, "/api/tokens", &EmptyRequest{})
412402
}
413403

414-
func (c Client) Login(req *LoginRequest) (*LoginResponse, error) {
415-
return post[LoginRequest, LoginResponse](c, "/api/login", req)
404+
func (c Client) Login(ctx context.Context, req *LoginRequest) (*LoginResponse, error) {
405+
return post[LoginResponse](ctx, c, "/api/login", req)
416406
}
417407

418-
func (c Client) Logout() error {
419-
_, err := post[EmptyRequest, EmptyResponse](c, "/api/logout", &EmptyRequest{})
408+
func (c Client) Logout(ctx context.Context) error {
409+
_, err := post[EmptyResponse](ctx, c, "/api/logout", &EmptyRequest{})
420410
return err
421411
}
422412

423-
func (c Client) Signup(req *SignupRequest) (*SignupResponse, error) {
424-
return post[SignupRequest, SignupResponse](c, "/api/signup", req)
413+
func (c Client) Signup(ctx context.Context, req *SignupRequest) (*SignupResponse, error) {
414+
return post[SignupResponse](ctx, c, "/api/signup", req)
425415
}
426416

427-
func (c Client) GetServerVersion() (*Version, error) {
428-
ctx := context.TODO()
417+
func (c Client) GetServerVersion(ctx context.Context) (*Version, error) {
429418
return get[Version](ctx, c, "/api/version", Query{})
430419
}
431420

432-
func (c Client) GetSettings() (*Settings, error) {
433-
ctx := context.TODO()
421+
func (c Client) GetSettings(ctx context.Context) (*Settings, error) {
434422
return get[Settings](ctx, c, "/api/settings", Query{})
435423
}
436424

437-
func (c Client) UpdateSettings(req *Settings) (*Settings, error) {
438-
return put[Settings, Settings](c, "/api/settings", req)
425+
func (c Client) UpdateSettings(ctx context.Context, req *Settings) (*Settings, error) {
426+
return put[Settings](ctx, c, "/api/settings", req)
439427
}
440428

441429
func partialText(body []byte, limit int) string {

api/client_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func TestGet(t *testing.T) {
128128
}
129129

130130
func TestDelete(t *testing.T) {
131+
ctx := context.Background()
131132
ch := make(chan *http.Request, 1)
132133
handler := func(rw http.ResponseWriter, r *http.Request) {
133134
ch <- r
@@ -153,7 +154,7 @@ func TestDelete(t *testing.T) {
153154
}
154155

155156
t.Run("headers", func(t *testing.T) {
156-
err := delete(c, "/good", Query{})
157+
err := delete(ctx, c, "/good", Query{})
157158
assert.NilError(t, err)
158159

159160
r := <-ch

internal/cmd/destinations.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func newDestinationsRemoveCmd(cli *CLI) *cobra.Command {
151151
logging.Debugf("deleting %d destinations named %q...", destinations.Count, name)
152152
for _, d := range destinations.Items {
153153
logging.Debugf("...call server: delete destination %s", d.ID)
154-
err := client.DeleteDestination(d.ID)
154+
err := client.DeleteDestination(ctx, d.ID)
155155
if err != nil {
156156
if api.ErrorStatusCode(err) == 403 {
157157
logging.Debugf("%s", err.Error())

internal/cmd/grants.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func removeGrant(cli *CLI, cmdOptions grantsCmdOptions) error {
292292

293293
for _, g := range grants.Items {
294294
logging.Debugf("call server: delete grant %s", g.ID)
295-
err := client.DeleteGrant(g.ID)
295+
err := client.DeleteGrant(ctx, g.ID)
296296
if err != nil {
297297
if api.ErrorStatusCode(err) == 403 {
298298
logging.Debugf("%s", err.Error())
@@ -378,7 +378,7 @@ func addGrant(cli *CLI, cmdOptions grantsCmdOptions) error {
378378
}
379379

380380
if userID == 0 && cmdOptions.UserName != "" {
381-
user, err := createUser(client, cmdOptions.UserName)
381+
user, err := createUser(ctx, client, cmdOptions.UserName)
382382
if err != nil {
383383
if api.ErrorStatusCode(err) == 403 {
384384
logging.Debugf("%s", err.Error())
@@ -393,9 +393,8 @@ func addGrant(cli *CLI, cmdOptions grantsCmdOptions) error {
393393
userID = user.ID
394394

395395
} else if groupID == 0 && cmdOptions.GroupName != "" {
396-
group, err := createGroup(client, cmdOptions.GroupName)
396+
group, err := client.CreateGroup(ctx, &api.CreateGroupRequest{Name: cmdOptions.GroupName})
397397
if err != nil {
398-
399398
if api.ErrorStatusCode(err) == 403 {
400399
logging.Debugf("%s", err.Error())
401400
return Error{
@@ -422,7 +421,7 @@ func addGrant(cli *CLI, cmdOptions grantsCmdOptions) error {
422421
Resource: cmdOptions.Resource,
423422
}
424423
logging.Debugf("call server: create grant %#v", createGrantReq)
425-
response, err := client.CreateGrant(createGrantReq)
424+
response, err := client.CreateGrant(ctx, createGrantReq)
426425
if err != nil {
427426
if api.ErrorStatusCode(err) == 403 {
428427
logging.Debugf("%s", err.Error())

0 commit comments

Comments
 (0)