From ca7482b02f5805e5b1e33966f9cc471973291b89 Mon Sep 17 00:00:00 2001 From: McKayla Washburn Date: Wed, 10 Jul 2024 21:48:16 +0000 Subject: [PATCH 1/3] chore: use `rw.WriteHeader` to write responses without bodies --- coderd/apidoc/docs.go | 7 ++----- coderd/apidoc/swagger.json | 7 ++----- coderd/apikey.go | 2 +- coderd/debug.go | 2 +- coderd/externalauth.go | 2 +- coderd/identityprovider/revoke.go | 2 +- coderd/oauth2.go | 4 ++-- coderd/templates.go | 2 +- coderd/users.go | 8 +++----- coderd/workspaces.go | 4 +--- docs/api/users.md | 34 +++---------------------------- 11 files changed, 18 insertions(+), 56 deletions(-) diff --git a/coderd/apidoc/docs.go b/coderd/apidoc/docs.go index 0382ab967dcd4..481831bf97f77 100644 --- a/coderd/apidoc/docs.go +++ b/coderd/apidoc/docs.go @@ -4723,11 +4723,8 @@ const docTemplate = `{ } ], "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/codersdk.User" - } + "204": { + "description": "No Content" } } } diff --git a/coderd/apidoc/swagger.json b/coderd/apidoc/swagger.json index 0c44ef0f255ed..bf987f44e67ec 100644 --- a/coderd/apidoc/swagger.json +++ b/coderd/apidoc/swagger.json @@ -4161,11 +4161,8 @@ } ], "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/codersdk.User" - } + "204": { + "description": "No Content" } } } diff --git a/coderd/apikey.go b/coderd/apikey.go index fe32b771e61ef..8676b5e1ba6c0 100644 --- a/coderd/apikey.go +++ b/coderd/apikey.go @@ -333,7 +333,7 @@ func (api *API) deleteAPIKey(rw http.ResponseWriter, r *http.Request) { return } - httpapi.Write(ctx, rw, http.StatusNoContent, nil) + rw.WriteHeader(http.StatusNoContent) } // @Summary Get token config diff --git a/coderd/debug.go b/coderd/debug.go index b1f17f29e0102..f13656886295e 100644 --- a/coderd/debug.go +++ b/coderd/debug.go @@ -235,7 +235,7 @@ func (api *API) putDeploymentHealthSettings(rw http.ResponseWriter, r *http.Requ if bytes.Equal(settingsJSON, []byte(currentSettingsJSON)) { // See: https://www.rfc-editor.org/rfc/rfc7231#section-6.3.5 - httpapi.Write(r.Context(), rw, http.StatusNoContent, nil) + rw.WriteHeader(http.StatusNoContent) return } diff --git a/coderd/externalauth.go b/coderd/externalauth.go index 8f8514fa17442..25f362e7372cf 100644 --- a/coderd/externalauth.go +++ b/coderd/externalauth.go @@ -197,7 +197,7 @@ func (api *API) postExternalAuthDeviceByID(rw http.ResponseWriter, r *http.Reque return } } - httpapi.Write(ctx, rw, http.StatusNoContent, nil) + rw.WriteHeader(http.StatusNoContent) } // @Summary Get external auth device by ID. diff --git a/coderd/identityprovider/revoke.go b/coderd/identityprovider/revoke.go index cddc150bbe364..78acb9ea0de22 100644 --- a/coderd/identityprovider/revoke.go +++ b/coderd/identityprovider/revoke.go @@ -39,6 +39,6 @@ func RevokeApp(db database.Store) http.HandlerFunc { httpapi.InternalServerError(rw, err) return } - httpapi.Write(ctx, rw, http.StatusNoContent, nil) + rw.WriteHeader(http.StatusNoContent) } } diff --git a/coderd/oauth2.go b/coderd/oauth2.go index ef68e93a1fc47..da102faf9138c 100644 --- a/coderd/oauth2.go +++ b/coderd/oauth2.go @@ -207,7 +207,7 @@ func (api *API) deleteOAuth2ProviderApp(rw http.ResponseWriter, r *http.Request) }) return } - httpapi.Write(ctx, rw, http.StatusNoContent, nil) + rw.WriteHeader(http.StatusNoContent) } // @Summary Get OAuth2 application secrets. @@ -324,7 +324,7 @@ func (api *API) deleteOAuth2ProviderAppSecret(rw http.ResponseWriter, r *http.Re }) return } - httpapi.Write(ctx, rw, http.StatusNoContent, nil) + rw.WriteHeader(http.StatusNoContent) } // @Summary OAuth2 authorization request. diff --git a/coderd/templates.go b/coderd/templates.go index 78f821a382a8d..5bf32871dcbc1 100644 --- a/coderd/templates.go +++ b/coderd/templates.go @@ -791,7 +791,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) { if updated.UpdatedAt.IsZero() { aReq.New = template - httpapi.Write(ctx, rw, http.StatusNotModified, nil) + rw.WriteHeader(http.StatusNotModified) return } aReq.New = updated diff --git a/coderd/users.go b/coderd/users.go index 4372a4f7ded33..6331cd171ff9e 100644 --- a/coderd/users.go +++ b/coderd/users.go @@ -504,7 +504,7 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) { // @Produce json // @Tags Users // @Param user path string true "User ID, name, or me" -// @Success 200 {object} codersdk.User +// @Success 204 // @Router /users/{user} [delete] func (api *API) deleteUser(rw http.ResponseWriter, r *http.Request) { ctx := r.Context() @@ -558,9 +558,7 @@ func (api *API) deleteUser(rw http.ResponseWriter, r *http.Request) { } user.Deleted = true aReq.New = user - httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{ - Message: "User has been deleted!", - }) + rw.WriteHeader(http.StatusNoContent) } // Returns the parameterized user requested. All validation @@ -1013,7 +1011,7 @@ func (api *API) putUserPassword(rw http.ResponseWriter, r *http.Request) { newUser.HashedPassword = []byte(hashedPassword) aReq.New = newUser - httpapi.Write(ctx, rw, http.StatusNoContent, nil) + rw.WriteHeader(http.StatusNoContent) } // @Summary Get user roles diff --git a/coderd/workspaces.go b/coderd/workspaces.go index bed982d5e2511..9f1ca970e609e 100644 --- a/coderd/workspaces.go +++ b/coderd/workspaces.go @@ -927,9 +927,7 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) { // If the workspace is already in the desired state do nothing! if workspace.DormantAt.Valid == req.Dormant { - httpapi.Write(ctx, rw, http.StatusNotModified, codersdk.Response{ - Message: "Nothing to do!", - }) + rw.WriteHeader(http.StatusNotModified) return } diff --git a/docs/api/users.md b/docs/api/users.md index 22d1c7b9cfca8..ac3305af96c86 100644 --- a/docs/api/users.md +++ b/docs/api/users.md @@ -410,7 +410,6 @@ To perform this operation, you must be authenticated. [Learn more](authenticatio ```shell # Example request using curl curl -X DELETE http://coder-server:8080/api/v2/users/{user} \ - -H 'Accept: application/json' \ -H 'Coder-Session-Token: API_KEY' ``` @@ -422,38 +421,11 @@ curl -X DELETE http://coder-server:8080/api/v2/users/{user} \ | ------ | ---- | ------ | -------- | -------------------- | | `user` | path | string | true | User ID, name, or me | -### Example responses - -> 200 Response - -```json -{ - "avatar_url": "http://example.com", - "created_at": "2019-08-24T14:15:22Z", - "email": "user@example.com", - "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", - "last_seen_at": "2019-08-24T14:15:22Z", - "login_type": "", - "name": "string", - "organization_ids": ["497f6eca-6276-4993-bfeb-53cbbbba6f08"], - "roles": [ - { - "display_name": "string", - "name": "string", - "organization_id": "string" - } - ], - "status": "active", - "theme_preference": "string", - "username": "string" -} -``` - ### Responses -| Status | Meaning | Description | Schema | -| ------ | ------------------------------------------------------- | ----------- | ---------------------------------------- | -| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.User](schemas.md#codersdkuser) | +| Status | Meaning | Description | Schema | +| ------ | --------------------------------------------------------------- | ----------- | ------ | +| 204 | [No Content](https://tools.ietf.org/html/rfc7231#section-6.3.5) | No Content | | To perform this operation, you must be authenticated. [Learn more](authentication.md). From b485844fbc6693da8964d6eb081220f244bbf6d3 Mon Sep 17 00:00:00 2001 From: McKayla Washburn Date: Thu, 11 Jul 2024 17:29:21 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coderd/users.go | 1 - codersdk/users.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/coderd/users.go b/coderd/users.go index 6331cd171ff9e..0cfcc63f9a3ed 100644 --- a/coderd/users.go +++ b/coderd/users.go @@ -501,7 +501,6 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) { // @Summary Delete user // @ID delete-user // @Security CoderSessionToken -// @Produce json // @Tags Users // @Param user path string true "User ID, name, or me" // @Success 204 diff --git a/codersdk/users.go b/codersdk/users.go index dd6779e3a0342..e56c9cc90d1c7 100644 --- a/codersdk/users.go +++ b/codersdk/users.go @@ -308,7 +308,7 @@ func (c *Client) DeleteUser(ctx context.Context, id uuid.UUID) error { return err } defer res.Body.Close() - if res.StatusCode != http.StatusOK { + if res.StatusCode != http.StatusNoContent { return ReadBodyAsError(res) } return nil From 16f57b890bb6f84fec01d737442110f43dbd3f0a Mon Sep 17 00:00:00 2001 From: McKayla Washburn Date: Thu, 11 Jul 2024 18:28:45 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9A=99=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coderd/apidoc/docs.go | 3 --- coderd/apidoc/swagger.json | 1 - 2 files changed, 4 deletions(-) diff --git a/coderd/apidoc/docs.go b/coderd/apidoc/docs.go index 481831bf97f77..fb51f553e7524 100644 --- a/coderd/apidoc/docs.go +++ b/coderd/apidoc/docs.go @@ -4705,9 +4705,6 @@ const docTemplate = `{ "CoderSessionToken": [] } ], - "produces": [ - "application/json" - ], "tags": [ "Users" ], diff --git a/coderd/apidoc/swagger.json b/coderd/apidoc/swagger.json index bf987f44e67ec..fa5ed4b12ca11 100644 --- a/coderd/apidoc/swagger.json +++ b/coderd/apidoc/swagger.json @@ -4147,7 +4147,6 @@ "CoderSessionToken": [] } ], - "produces": ["application/json"], "tags": ["Users"], "summary": "Delete user", "operationId": "delete-user",