@@ -2,7 +2,6 @@ package codersdk
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"fmt"
7
6
"net/http"
8
7
"time"
@@ -65,17 +64,12 @@ type GenerateAPIKeyResponse struct {
65
64
// custom expiration. These tokens can be used for long-lived access,
66
65
// like for use with CI.
67
66
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
+ })
79
73
}
80
74
81
75
// 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
84
78
// Only use CreateAPIKey if you want to emulate the session created for
85
79
// a browser like login.
86
80
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
+ })
98
88
}
99
89
100
90
type TokensFilter struct {
@@ -122,69 +112,47 @@ func (f TokensFilter) asRequestOption() RequestOption {
122
112
123
113
// Tokens list machine API keys.
124
114
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
+ })
135
121
}
136
122
137
123
// APIKeyByID returns the api key by id.
138
124
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
+ })
149
130
}
150
131
151
132
// APIKeyByName returns the api key by name.
152
133
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
+ })
163
139
}
164
140
165
141
// DeleteAPIKey deletes API key by id.
166
142
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
176
149
}
177
150
178
151
// GetTokenConfig returns deployment options related to token management
179
152
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
+ })
190
158
}
0 commit comments