chore: use rw.WriteHeader to write responses without bodies (#13870)

This commit is contained in:
Kayla Washburn-Love
2024-07-11 13:38:33 -06:00
committed by GitHub
parent fd10ea1dcc
commit de2585b0b6
12 changed files with 19 additions and 62 deletions

10
coderd/apidoc/docs.go generated
View File

@ -4705,9 +4705,6 @@ const docTemplate = `{
"CoderSessionToken": []
}
],
"produces": [
"application/json"
],
"tags": [
"Users"
],
@ -4723,11 +4720,8 @@ const docTemplate = `{
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/codersdk.User"
}
"204": {
"description": "No Content"
}
}
}

View File

@ -4147,7 +4147,6 @@
"CoderSessionToken": []
}
],
"produces": ["application/json"],
"tags": ["Users"],
"summary": "Delete user",
"operationId": "delete-user",
@ -4161,11 +4160,8 @@
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/codersdk.User"
}
"204": {
"description": "No Content"
}
}
}

View File

@ -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

View File

@ -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
}

View File

@ -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.

View File

@ -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)
}
}

View File

@ -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.

View File

@ -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

View File

@ -501,10 +501,9 @@ 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 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 +557,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 +1010,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

View File

@ -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
}

View File

@ -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

34
docs/api/users.md generated
View File

@ -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).