@@ -233,18 +233,12 @@ func (c *Client) Organization(ctx context.Context, id uuid.UUID) (Organization,
233
233
234
234
// CreateOrganization creates an organization and adds the user making the request as an owner.
235
235
func (c * Client ) CreateOrganization (ctx context.Context , req CreateOrganizationRequest ) (Organization , error ) {
236
- res , err := c .Request (ctx , http .MethodPost , "/api/v2/organizations" , req )
237
- if err != nil {
238
- return Organization {}, err
239
- }
240
- defer res .Body .Close ()
241
-
242
- if res .StatusCode != http .StatusCreated {
243
- return Organization {}, ReadBodyAsError (res )
244
- }
245
-
246
- var org Organization
247
- return org , json .NewDecoder (res .Body ).Decode (& org )
236
+ return makeSDKRequest [Organization ](ctx , c , sdkRequestArgs {
237
+ Method : http .MethodPost ,
238
+ URL : "/api/v2/organizations" ,
239
+ Body : req ,
240
+ ExpectCode : http .StatusCreated ,
241
+ })
248
242
}
249
243
250
244
// UpdateOrganization will update information about the corresponding organization, based on
@@ -290,97 +284,49 @@ func (c *Client) ProvisionerDaemons(ctx context.Context) ([]ProvisionerDaemon, e
290
284
}
291
285
292
286
func (c * Client ) OrganizationProvisionerDaemons (ctx context.Context , organizationID uuid.UUID ) ([]ProvisionerDaemon , error ) {
293
- res , err := c .Request (ctx , http .MethodGet ,
294
- fmt .Sprintf ("/api/v2/organizations/%s/provisionerdaemons" , organizationID .String ()),
295
- nil ,
296
- )
297
- if err != nil {
298
- return nil , xerrors .Errorf ("execute request: %w" , err )
299
- }
300
- defer res .Body .Close ()
301
-
302
- if res .StatusCode != http .StatusOK {
303
- return nil , ReadBodyAsError (res )
304
- }
305
-
306
- var daemons []ProvisionerDaemon
307
- return daemons , json .NewDecoder (res .Body ).Decode (& daemons )
287
+ return makeSDKRequest [[]ProvisionerDaemon ](ctx , c , sdkRequestArgs {
288
+ Method : http .MethodGet ,
289
+ URL : fmt .Sprintf ("/api/v2/organizations/%s/provisionerdaemons" , organizationID .String ()),
290
+ ExpectCode : http .StatusOK ,
291
+ })
308
292
}
309
293
310
294
// CreateTemplateVersion processes source-code and optionally associates the version with a template.
311
295
// Executing without a template is useful for validating source-code.
312
296
func (c * Client ) CreateTemplateVersion (ctx context.Context , organizationID uuid.UUID , req CreateTemplateVersionRequest ) (TemplateVersion , error ) {
313
- res , err := c .Request (ctx , http .MethodPost ,
314
- fmt .Sprintf ("/api/v2/organizations/%s/templateversions" , organizationID .String ()),
315
- req ,
316
- )
317
- if err != nil {
318
- return TemplateVersion {}, xerrors .Errorf ("execute request: %w" , err )
319
- }
320
- defer res .Body .Close ()
321
-
322
- if res .StatusCode != http .StatusCreated {
323
- return TemplateVersion {}, ReadBodyAsError (res )
324
- }
325
-
326
- var templateVersion TemplateVersion
327
- return templateVersion , json .NewDecoder (res .Body ).Decode (& templateVersion )
297
+ return makeSDKRequest [TemplateVersion ](ctx , c , sdkRequestArgs {
298
+ Method : http .MethodPost ,
299
+ URL : fmt .Sprintf ("/api/v2/organizations/%s/templateversions" , organizationID .String ()),
300
+ Body : req ,
301
+ ExpectCode : http .StatusCreated ,
302
+ })
328
303
}
329
304
330
305
func (c * Client ) TemplateVersionByOrganizationAndName (ctx context.Context , organizationID uuid.UUID , templateName , versionName string ) (TemplateVersion , error ) {
331
- res , err := c .Request (ctx , http .MethodGet ,
332
- fmt .Sprintf ("/api/v2/organizations/%s/templates/%s/versions/%s" , organizationID .String (), templateName , versionName ),
333
- nil ,
334
- )
335
- if err != nil {
336
- return TemplateVersion {}, xerrors .Errorf ("execute request: %w" , err )
337
- }
338
- defer res .Body .Close ()
339
-
340
- if res .StatusCode != http .StatusOK {
341
- return TemplateVersion {}, ReadBodyAsError (res )
342
- }
343
-
344
- var templateVersion TemplateVersion
345
- return templateVersion , json .NewDecoder (res .Body ).Decode (& templateVersion )
306
+ return makeSDKRequest [TemplateVersion ](ctx , c , sdkRequestArgs {
307
+ Method : http .MethodGet ,
308
+ URL : fmt .Sprintf ("/api/v2/organizations/%s/templates/%s/versions/%s" , organizationID .String (), templateName , versionName ),
309
+ ExpectCode : http .StatusOK ,
310
+ })
346
311
}
347
312
348
313
// CreateTemplate creates a new template inside an organization.
349
314
func (c * Client ) CreateTemplate (ctx context.Context , organizationID uuid.UUID , request CreateTemplateRequest ) (Template , error ) {
350
- res , err := c .Request (ctx , http .MethodPost ,
351
- fmt .Sprintf ("/api/v2/organizations/%s/templates" , organizationID .String ()),
352
- request ,
353
- )
354
- if err != nil {
355
- return Template {}, xerrors .Errorf ("execute request: %w" , err )
356
- }
357
- defer res .Body .Close ()
358
-
359
- if res .StatusCode != http .StatusCreated {
360
- return Template {}, ReadBodyAsError (res )
361
- }
362
-
363
- var template Template
364
- return template , json .NewDecoder (res .Body ).Decode (& template )
315
+ return makeSDKRequest [Template ](ctx , c , sdkRequestArgs {
316
+ Method : http .MethodPost ,
317
+ URL : fmt .Sprintf ("/api/v2/organizations/%s/templates" , organizationID .String ()),
318
+ Body : request ,
319
+ ExpectCode : http .StatusCreated ,
320
+ })
365
321
}
366
322
367
323
// TemplatesByOrganization lists all templates inside of an organization.
368
324
func (c * Client ) TemplatesByOrganization (ctx context.Context , organizationID uuid.UUID ) ([]Template , error ) {
369
- res , err := c .Request (ctx , http .MethodGet ,
370
- fmt .Sprintf ("/api/v2/organizations/%s/templates" , organizationID .String ()),
371
- nil ,
372
- )
373
- if err != nil {
374
- return nil , xerrors .Errorf ("execute request: %w" , err )
375
- }
376
- defer res .Body .Close ()
377
-
378
- if res .StatusCode != http .StatusOK {
379
- return nil , ReadBodyAsError (res )
380
- }
381
-
382
- var templates []Template
383
- return templates , json .NewDecoder (res .Body ).Decode (& templates )
325
+ return makeSDKRequest [[]Template ](ctx , c , sdkRequestArgs {
326
+ Method : http .MethodGet ,
327
+ URL : fmt .Sprintf ("/api/v2/organizations/%s/templates" , organizationID .String ()),
328
+ ExpectCode : http .StatusOK ,
329
+ })
384
330
}
385
331
386
332
type TemplateFilter struct {
@@ -449,16 +395,10 @@ func (c *Client) CreateWorkspace(ctx context.Context, _ uuid.UUID, user string,
449
395
450
396
// CreateUserWorkspace creates a new workspace for the template specified.
451
397
func (c * Client ) CreateUserWorkspace (ctx context.Context , user string , request CreateWorkspaceRequest ) (Workspace , error ) {
452
- res , err := c .Request (ctx , http .MethodPost , fmt .Sprintf ("/api/v2/users/%s/workspaces" , user ), request )
453
- if err != nil {
454
- return Workspace {}, err
455
- }
456
- defer res .Body .Close ()
457
-
458
- if res .StatusCode != http .StatusCreated {
459
- return Workspace {}, ReadBodyAsError (res )
460
- }
461
-
462
- var workspace Workspace
463
- return workspace , json .NewDecoder (res .Body ).Decode (& workspace )
398
+ return makeSDKRequest [Workspace ](ctx , c , sdkRequestArgs {
399
+ Method : http .MethodPost ,
400
+ URL : fmt .Sprintf ("/api/v2/users/%s/workspaces" , user ),
401
+ Body : request ,
402
+ ExpectCode : http .StatusCreated ,
403
+ })
464
404
}
0 commit comments