Skip to content

Commit d9d4599

Browse files
authored
chore: idea: unify http responses further (#941)
1 parent 4f0f216 commit d9d4599

16 files changed

+59
-105
lines changed

coderd/coderd.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"time"
1010

1111
"github.com/go-chi/chi/v5"
12-
"github.com/go-chi/render"
1312
"google.golang.org/api/idtoken"
1413

1514
chitrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/go-chi/chi.v5"
@@ -67,8 +66,7 @@ func New(options *Options) (http.Handler, func()) {
6766
})
6867
r.Route("/buildinfo", func(r chi.Router) {
6968
r.Get("/", func(rw http.ResponseWriter, r *http.Request) {
70-
render.Status(r, http.StatusOK)
71-
render.JSON(rw, r, codersdk.BuildInfoResponse{
69+
httpapi.Write(rw, http.StatusOK, codersdk.BuildInfoResponse{
7270
ExternalURL: buildinfo.ExternalURL(),
7371
Version: buildinfo.Version(),
7472
})

coderd/files.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net/http"
1111

1212
"github.com/go-chi/chi/v5"
13-
"github.com/go-chi/render"
1413

1514
"github.com/coder/coder/coderd/database"
1615
"github.com/coder/coder/coderd/httpapi"
@@ -44,8 +43,7 @@ func (api *api) postFile(rw http.ResponseWriter, r *http.Request) {
4443
file, err := api.Database.GetFileByHash(r.Context(), hash)
4544
if err == nil {
4645
// The file already exists!
47-
render.Status(r, http.StatusOK)
48-
render.JSON(rw, r, codersdk.UploadResponse{
46+
httpapi.Write(rw, http.StatusOK, codersdk.UploadResponse{
4947
Hash: file.Hash,
5048
})
5149
return
@@ -63,8 +61,8 @@ func (api *api) postFile(rw http.ResponseWriter, r *http.Request) {
6361
})
6462
return
6563
}
66-
render.Status(r, http.StatusCreated)
67-
render.JSON(rw, r, codersdk.UploadResponse{
64+
65+
httpapi.Write(rw, http.StatusCreated, codersdk.UploadResponse{
6866
Hash: file.Hash,
6967
})
7068
}

coderd/gitsshkey.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"net/http"
66

7-
"github.com/go-chi/render"
8-
97
"github.com/coder/coder/coderd/database"
108
"github.com/coder/coder/coderd/gitsshkey"
119
"github.com/coder/coder/coderd/httpapi"
@@ -44,8 +42,7 @@ func (api *api) regenerateGitSSHKey(rw http.ResponseWriter, r *http.Request) {
4442
return
4543
}
4644

47-
render.Status(r, http.StatusOK)
48-
render.JSON(rw, r, codersdk.GitSSHKey{
45+
httpapi.Write(rw, http.StatusOK, codersdk.GitSSHKey{
4946
UserID: newKey.UserID,
5047
CreatedAt: newKey.CreatedAt,
5148
UpdatedAt: newKey.UpdatedAt,
@@ -64,8 +61,7 @@ func (api *api) gitSSHKey(rw http.ResponseWriter, r *http.Request) {
6461
return
6562
}
6663

67-
render.Status(r, http.StatusOK)
68-
render.JSON(rw, r, codersdk.GitSSHKey{
64+
httpapi.Write(rw, http.StatusOK, codersdk.GitSSHKey{
6965
UserID: gitSSHKey.UserID,
7066
CreatedAt: gitSSHKey.CreatedAt,
7167
UpdatedAt: gitSSHKey.UpdatedAt,
@@ -108,8 +104,7 @@ func (api *api) agentGitSSHKey(rw http.ResponseWriter, r *http.Request) {
108104
return
109105
}
110106

111-
render.Status(r, http.StatusOK)
112-
render.JSON(rw, r, codersdk.AgentGitSSHKey{
107+
httpapi.Write(rw, http.StatusOK, codersdk.AgentGitSSHKey{
113108
PrivateKey: gitSSHKey.PrivateKey,
114109
})
115110
}

coderd/httpapi/httpapi.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type Error struct {
6363
}
6464

6565
// Write outputs a standardized format to an HTTP response body.
66-
func Write(rw http.ResponseWriter, status int, response Response) {
66+
func Write(rw http.ResponseWriter, status int, response interface{}) {
6767
buf := &bytes.Buffer{}
6868
enc := json.NewEncoder(buf)
6969
enc.SetEscapeHTML(true)

coderd/httpmw/ratelimit.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"time"
66

77
"github.com/go-chi/httprate"
8-
"github.com/go-chi/render"
98

109
"github.com/coder/coder/coderd/database"
1110
"github.com/coder/coder/coderd/httpapi"
@@ -26,8 +25,7 @@ func RateLimitPerMinute(count int) func(http.Handler) http.Handler {
2625
return httprate.KeyByIP(r)
2726
}, httprate.KeyByEndpoint),
2827
httprate.WithLimitHandler(func(w http.ResponseWriter, r *http.Request) {
29-
render.Status(r, http.StatusTooManyRequests)
30-
render.JSON(w, r, httpapi.Response{
28+
httpapi.Write(w, http.StatusTooManyRequests, httpapi.Response{
3129
Message: "You've been rate limited for sending too many requests!",
3230
})
3331
}),

coderd/organizations.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88

99
"github.com/go-chi/chi/v5"
10-
"github.com/go-chi/render"
1110
"github.com/google/uuid"
1211
"github.com/moby/moby/pkg/namesgenerator"
1312
"golang.org/x/xerrors"
@@ -20,8 +19,7 @@ import (
2019

2120
func (*api) organization(rw http.ResponseWriter, r *http.Request) {
2221
organization := httpmw.OrganizationParam(r)
23-
render.Status(r, http.StatusOK)
24-
render.JSON(rw, r, convertOrganization(organization))
22+
httpapi.Write(rw, http.StatusOK, convertOrganization(organization))
2523
}
2624

2725
func (api *api) provisionerDaemonsByOrganization(rw http.ResponseWriter, r *http.Request) {
@@ -38,8 +36,7 @@ func (api *api) provisionerDaemonsByOrganization(rw http.ResponseWriter, r *http
3836
if daemons == nil {
3937
daemons = []database.ProvisionerDaemon{}
4038
}
41-
render.Status(r, http.StatusOK)
42-
render.JSON(rw, r, daemons)
39+
httpapi.Write(rw, http.StatusOK, daemons)
4340
}
4441

4542
// Creates a new version of a template. An import job is queued to parse the storage method provided.
@@ -147,8 +144,7 @@ func (api *api) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
147144
return
148145
}
149146

150-
render.Status(r, http.StatusCreated)
151-
render.JSON(rw, r, convertTemplateVersion(templateVersion, convertProvisionerJob(provisionerJob)))
147+
httpapi.Write(rw, http.StatusCreated, convertTemplateVersion(templateVersion, convertProvisionerJob(provisionerJob)))
152148
}
153149

154150
// Create a new template in an organization.
@@ -252,8 +248,7 @@ func (api *api) postTemplatesByOrganization(rw http.ResponseWriter, r *http.Requ
252248
return
253249
}
254250

255-
render.Status(r, http.StatusCreated)
256-
render.JSON(rw, r, template)
251+
httpapi.Write(rw, http.StatusCreated, template)
257252
}
258253

259254
func (api *api) templatesByOrganization(rw http.ResponseWriter, r *http.Request) {
@@ -284,8 +279,8 @@ func (api *api) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
284279
})
285280
return
286281
}
287-
render.Status(r, http.StatusOK)
288-
render.JSON(rw, r, convertTemplates(templates, workspaceCounts))
282+
283+
httpapi.Write(rw, http.StatusOK, convertTemplates(templates, workspaceCounts))
289284
}
290285

291286
func (api *api) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Request) {
@@ -325,8 +320,7 @@ func (api *api) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
325320
count = uint32(workspaceCounts[0].Count)
326321
}
327322

328-
render.Status(r, http.StatusOK)
329-
render.JSON(rw, r, convertTemplate(template, count))
323+
httpapi.Write(rw, http.StatusOK, convertTemplate(template, count))
330324
}
331325

332326
// convertOrganization consumes the database representation and outputs an API friendly representation.

coderd/parameters.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88

99
"github.com/go-chi/chi/v5"
10-
"github.com/go-chi/render"
1110
"github.com/google/uuid"
1211

1312
"github.com/coder/coder/coderd/database"
@@ -59,8 +58,7 @@ func (api *api) postParameter(rw http.ResponseWriter, r *http.Request) {
5958
return
6059
}
6160

62-
render.Status(r, http.StatusCreated)
63-
render.JSON(rw, r, convertParameterValue(parameterValue))
61+
httpapi.Write(rw, http.StatusCreated, convertParameterValue(parameterValue))
6462
}
6563

6664
func (api *api) parameters(rw http.ResponseWriter, r *http.Request) {
@@ -86,8 +84,7 @@ func (api *api) parameters(rw http.ResponseWriter, r *http.Request) {
8684
apiParameterValues = append(apiParameterValues, convertParameterValue(parameterValue))
8785
}
8886

89-
render.Status(r, http.StatusOK)
90-
render.JSON(rw, r, apiParameterValues)
87+
httpapi.Write(rw, http.StatusOK, apiParameterValues)
9188
}
9289

9390
func (api *api) deleteParameter(rw http.ResponseWriter, r *http.Request) {

coderd/provisionerjobs.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strconv"
1111
"time"
1212

13-
"github.com/go-chi/render"
1413
"github.com/google/uuid"
1514

1615
"cdr.dev/slog"
@@ -87,8 +86,8 @@ func (api *api) provisionerJobLogs(rw http.ResponseWriter, r *http.Request, job
8786
if logs == nil {
8887
logs = []database.ProvisionerJobLog{}
8988
}
90-
render.Status(r, http.StatusOK)
91-
render.JSON(rw, r, logs)
89+
90+
httpapi.Write(rw, http.StatusOK, logs)
9291
return
9392
}
9493

@@ -229,8 +228,8 @@ func (api *api) provisionerJobResources(rw http.ResponseWriter, r *http.Request,
229228
}
230229
apiResources = append(apiResources, convertWorkspaceResource(resource, agents))
231230
}
232-
render.Status(r, http.StatusOK)
233-
render.JSON(rw, r, apiResources)
231+
232+
httpapi.Write(rw, http.StatusOK, apiResources)
234233
}
235234

236235
func convertProvisionerJobLog(provisionerJobLog database.ProvisionerJobLog) codersdk.ProvisionerJobLog {

coderd/templates.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88

99
"github.com/go-chi/chi/v5"
10-
"github.com/go-chi/render"
1110
"github.com/google/uuid"
1211

1312
"github.com/coder/coder/coderd/database"
@@ -34,8 +33,7 @@ func (api *api) template(rw http.ResponseWriter, r *http.Request) {
3433
count = uint32(workspaceCounts[0].Count)
3534
}
3635

37-
render.Status(r, http.StatusOK)
38-
render.JSON(rw, r, convertTemplate(template, count))
36+
httpapi.Write(rw, http.StatusOK, convertTemplate(template, count))
3937
}
4038

4139
func (api *api) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
@@ -114,8 +112,8 @@ func (api *api) templateVersionsByTemplate(rw http.ResponseWriter, r *http.Reque
114112
}
115113
apiVersion = append(apiVersion, convertTemplateVersion(version, convertProvisionerJob(job)))
116114
}
117-
render.Status(r, http.StatusOK)
118-
render.JSON(rw, r, apiVersion)
115+
116+
httpapi.Write(rw, http.StatusOK, apiVersion)
119117
}
120118

121119
func (api *api) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
@@ -148,8 +146,7 @@ func (api *api) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
148146
return
149147
}
150148

151-
render.Status(r, http.StatusOK)
152-
render.JSON(rw, r, convertTemplateVersion(templateVersion, convertProvisionerJob(job)))
149+
httpapi.Write(rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job)))
153150
}
154151

155152
func (api *api) patchActiveTemplateVersion(rw http.ResponseWriter, r *http.Request) {

coderd/templateversions.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"fmt"
77
"net/http"
88

9-
"github.com/go-chi/render"
10-
119
"github.com/coder/coder/coderd/database"
1210
"github.com/coder/coder/coderd/httpapi"
1311
"github.com/coder/coder/coderd/httpmw"
@@ -24,8 +22,8 @@ func (api *api) templateVersion(rw http.ResponseWriter, r *http.Request) {
2422
})
2523
return
2624
}
27-
render.Status(r, http.StatusOK)
28-
render.JSON(rw, r, convertTemplateVersion(templateVersion, convertProvisionerJob(job)))
25+
26+
httpapi.Write(rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job)))
2927
}
3028

3129
func (api *api) patchCancelTemplateVersion(rw http.ResponseWriter, r *http.Request) {
@@ -95,8 +93,8 @@ func (api *api) templateVersionSchema(rw http.ResponseWriter, r *http.Request) {
9593
if schemas == nil {
9694
schemas = []database.ParameterSchema{}
9795
}
98-
render.Status(r, http.StatusOK)
99-
render.JSON(rw, r, schemas)
96+
97+
httpapi.Write(rw, http.StatusOK, schemas)
10098
}
10199

102100
func (api *api) templateVersionParameters(rw http.ResponseWriter, r *http.Request) {
@@ -132,8 +130,8 @@ func (api *api) templateVersionParameters(rw http.ResponseWriter, r *http.Reques
132130
if values == nil {
133131
values = []parameter.ComputedValue{}
134132
}
135-
render.Status(r, http.StatusOK)
136-
render.JSON(rw, r, values)
133+
134+
httpapi.Write(rw, http.StatusOK, values)
137135
}
138136

139137
func (api *api) templateVersionResources(rw http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)