Skip to content

Commit c89fb87

Browse files
committed
migrate apikey.go
1 parent e9487de commit c89fb87

File tree

2 files changed

+45
-88
lines changed

2 files changed

+45
-88
lines changed

codersdk/apikey.go

+40-72
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package codersdk
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"net/http"
87
"time"
@@ -65,17 +64,12 @@ type GenerateAPIKeyResponse struct {
6564
// custom expiration. These tokens can be used for long-lived access,
6665
// like for use with CI.
6766
func (c *Client) CreateToken(ctx context.Context, userID string, req CreateTokenRequest) (GenerateAPIKeyResponse, error) {
68-
res, err := c.Request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/users/%s/keys/tokens", userID), req)
69-
if err != nil {
70-
return GenerateAPIKeyResponse{}, err
71-
}
72-
defer res.Body.Close()
73-
if res.StatusCode > http.StatusCreated {
74-
return GenerateAPIKeyResponse{}, ReadBodyAsError(res)
75-
}
76-
77-
var apiKey GenerateAPIKeyResponse
78-
return apiKey, json.NewDecoder(res.Body).Decode(&apiKey)
67+
return makeSDKRequest[GenerateAPIKeyResponse](ctx, c, sdkRequestArgs{
68+
Method: http.MethodPost,
69+
URL: fmt.Sprintf("/api/v2/users/%s/keys/tokens", userID),
70+
Body: req,
71+
ExpectCode: http.StatusCreated,
72+
})
7973
}
8074

8175
// CreateAPIKey generates an API key for the user ID provided.
@@ -84,17 +78,13 @@ func (c *Client) CreateToken(ctx context.Context, userID string, req CreateToken
8478
// Only use CreateAPIKey if you want to emulate the session created for
8579
// a browser like login.
8680
func (c *Client) CreateAPIKey(ctx context.Context, user string) (GenerateAPIKeyResponse, error) {
87-
res, err := c.Request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/users/%s/keys", user), nil)
88-
if err != nil {
89-
return GenerateAPIKeyResponse{}, err
90-
}
91-
defer res.Body.Close()
92-
if res.StatusCode > http.StatusCreated {
93-
return GenerateAPIKeyResponse{}, ReadBodyAsError(res)
94-
}
95-
96-
var apiKey GenerateAPIKeyResponse
97-
return apiKey, json.NewDecoder(res.Body).Decode(&apiKey)
81+
return makeSDKRequest[GenerateAPIKeyResponse](ctx, c, sdkRequestArgs{
82+
Method: http.MethodPost,
83+
URL: fmt.Sprintf("/api/v2/users/%s/keys", user),
84+
Body: nil,
85+
ReqOpts: nil,
86+
ExpectCode: http.StatusCreated,
87+
})
9888
}
9989

10090
type TokensFilter struct {
@@ -122,69 +112,47 @@ func (f TokensFilter) asRequestOption() RequestOption {
122112

123113
// Tokens list machine API keys.
124114
func (c *Client) Tokens(ctx context.Context, userID string, filter TokensFilter) ([]APIKeyWithOwner, error) {
125-
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/keys/tokens", userID), nil, filter.asRequestOption())
126-
if err != nil {
127-
return nil, err
128-
}
129-
defer res.Body.Close()
130-
if res.StatusCode > http.StatusOK {
131-
return nil, ReadBodyAsError(res)
132-
}
133-
apiKey := []APIKeyWithOwner{}
134-
return apiKey, json.NewDecoder(res.Body).Decode(&apiKey)
115+
return makeSDKRequest[[]APIKeyWithOwner](ctx, c, sdkRequestArgs{
116+
Method: http.MethodGet,
117+
URL: fmt.Sprintf("/api/v2/users/%s/keys/tokens", userID),
118+
ReqOpts: []RequestOption{filter.asRequestOption()},
119+
ExpectCode: http.StatusOK,
120+
})
135121
}
136122

137123
// APIKeyByID returns the api key by id.
138124
func (c *Client) APIKeyByID(ctx context.Context, userID string, id string) (*APIKey, error) {
139-
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/keys/%s", userID, id), nil)
140-
if err != nil {
141-
return nil, err
142-
}
143-
defer res.Body.Close()
144-
if res.StatusCode > http.StatusCreated {
145-
return nil, ReadBodyAsError(res)
146-
}
147-
apiKey := &APIKey{}
148-
return apiKey, json.NewDecoder(res.Body).Decode(apiKey)
125+
return makeSDKRequest[*APIKey](ctx, c, sdkRequestArgs{
126+
Method: http.MethodGet,
127+
URL: fmt.Sprintf("/api/v2/users/%s/keys/%s", userID, id),
128+
ExpectCode: http.StatusOK,
129+
})
149130
}
150131

151132
// APIKeyByName returns the api key by name.
152133
func (c *Client) APIKeyByName(ctx context.Context, userID string, name string) (*APIKey, error) {
153-
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/keys/tokens/%s", userID, name), nil)
154-
if err != nil {
155-
return nil, err
156-
}
157-
defer res.Body.Close()
158-
if res.StatusCode > http.StatusCreated {
159-
return nil, ReadBodyAsError(res)
160-
}
161-
apiKey := &APIKey{}
162-
return apiKey, json.NewDecoder(res.Body).Decode(apiKey)
134+
return makeSDKRequest[*APIKey](ctx, c, sdkRequestArgs{
135+
Method: http.MethodGet,
136+
URL: fmt.Sprintf("/api/v2/users/%s/keys/tokens/%s", userID, name),
137+
ExpectCode: http.StatusOK,
138+
})
163139
}
164140

165141
// DeleteAPIKey deletes API key by id.
166142
func (c *Client) DeleteAPIKey(ctx context.Context, userID string, id string) error {
167-
res, err := c.Request(ctx, http.MethodDelete, fmt.Sprintf("/api/v2/users/%s/keys/%s", userID, id), nil)
168-
if err != nil {
169-
return err
170-
}
171-
defer res.Body.Close()
172-
if res.StatusCode > http.StatusNoContent {
173-
return ReadBodyAsError(res)
174-
}
175-
return nil
143+
_, err := makeSDKRequest[noResponse](ctx, c, sdkRequestArgs{
144+
Method: http.MethodDelete,
145+
URL: fmt.Sprintf("/api/v2/users/%s/keys/%s", userID, id),
146+
ExpectCode: http.StatusNoContent,
147+
})
148+
return err
176149
}
177150

178151
// GetTokenConfig returns deployment options related to token management
179152
func (c *Client) GetTokenConfig(ctx context.Context, userID string) (TokenConfig, error) {
180-
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/keys/tokens/tokenconfig", userID), nil)
181-
if err != nil {
182-
return TokenConfig{}, err
183-
}
184-
defer res.Body.Close()
185-
if res.StatusCode > http.StatusOK {
186-
return TokenConfig{}, ReadBodyAsError(res)
187-
}
188-
tokenConfig := TokenConfig{}
189-
return tokenConfig, json.NewDecoder(res.Body).Decode(&tokenConfig)
153+
return makeSDKRequest[TokenConfig](ctx, c, sdkRequestArgs{
154+
Method: http.MethodGet,
155+
URL: fmt.Sprintf("/api/v2/users/%s/keys/tokens/tokenconfig", userID),
156+
ExpectCode: http.StatusOK,
157+
})
190158
}

codersdk/organizations.go

+5-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package codersdk
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"net/http"
87
"strings"
@@ -265,22 +264,12 @@ func (c *Client) DeleteOrganization(ctx context.Context, orgID string) error {
265264

266265
// ProvisionerDaemons returns provisioner daemons available.
267266
func (c *Client) ProvisionerDaemons(ctx context.Context) ([]ProvisionerDaemon, error) {
268-
res, err := c.Request(ctx, http.MethodGet,
267+
return makeSDKRequest[[]ProvisionerDaemon](ctx, c, sdkRequestArgs{
268+
Method: http.MethodGet,
269269
// TODO: the organization path parameter is currently ignored.
270-
"/api/v2/organizations/default/provisionerdaemons",
271-
nil,
272-
)
273-
if err != nil {
274-
return nil, xerrors.Errorf("execute request: %w", err)
275-
}
276-
defer res.Body.Close()
277-
278-
if res.StatusCode != http.StatusOK {
279-
return nil, ReadBodyAsError(res)
280-
}
281-
282-
var daemons []ProvisionerDaemon
283-
return daemons, json.NewDecoder(res.Body).Decode(&daemons)
270+
URL: "/api/v2/organizations/default/provisionerdaemons",
271+
ExpectCode: http.StatusOK,
272+
})
284273
}
285274

286275
func (c *Client) OrganizationProvisionerDaemons(ctx context.Context, organizationID uuid.UUID) ([]ProvisionerDaemon, error) {

0 commit comments

Comments
 (0)