Skip to content

Commit e9487de

Browse files
committed
add few more examples
1 parent ab4c5f9 commit e9487de

File tree

1 file changed

+39
-99
lines changed

1 file changed

+39
-99
lines changed

codersdk/organizations.go

Lines changed: 39 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,12 @@ func (c *Client) Organization(ctx context.Context, id uuid.UUID) (Organization,
233233

234234
// CreateOrganization creates an organization and adds the user making the request as an owner.
235235
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+
})
248242
}
249243

250244
// UpdateOrganization will update information about the corresponding organization, based on
@@ -290,97 +284,49 @@ func (c *Client) ProvisionerDaemons(ctx context.Context) ([]ProvisionerDaemon, e
290284
}
291285

292286
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+
})
308292
}
309293

310294
// CreateTemplateVersion processes source-code and optionally associates the version with a template.
311295
// Executing without a template is useful for validating source-code.
312296
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+
})
328303
}
329304

330305
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+
})
346311
}
347312

348313
// CreateTemplate creates a new template inside an organization.
349314
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+
})
365321
}
366322

367323
// TemplatesByOrganization lists all templates inside of an organization.
368324
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+
})
384330
}
385331

386332
type TemplateFilter struct {
@@ -449,16 +395,10 @@ func (c *Client) CreateWorkspace(ctx context.Context, _ uuid.UUID, user string,
449395

450396
// CreateUserWorkspace creates a new workspace for the template specified.
451397
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+
})
464404
}

0 commit comments

Comments
 (0)