Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: idea: unify http responses further #941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli/workspaceautostart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/stretchr/testify/require"
)

func TestWorkspaceAutostart(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion cli/workspaceautostop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/stretchr/testify/require"
)

func TestWorkspaceAutostop(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"google.golang.org/api/idtoken"

chitrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/go-chi/chi.v5"
Expand Down Expand Up @@ -67,8 +66,7 @@ func New(options *Options) (http.Handler, func()) {
})
r.Route("/buildinfo", func(r chi.Router) {
r.Get("/", func(rw http.ResponseWriter, r *http.Request) {
render.Status(r, http.StatusOK)
render.JSON(rw, r, codersdk.BuildInfoResponse{
httpapi.Write(rw, http.StatusOK, codersdk.BuildInfoResponse{
ExternalURL: buildinfo.ExternalURL(),
Version: buildinfo.Version(),
})
Expand Down
8 changes: 3 additions & 5 deletions coderd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"

"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
Expand Down Expand Up @@ -44,8 +43,7 @@ func (api *api) postFile(rw http.ResponseWriter, r *http.Request) {
file, err := api.Database.GetFileByHash(r.Context(), hash)
if err == nil {
// The file already exists!
render.Status(r, http.StatusOK)
render.JSON(rw, r, codersdk.UploadResponse{
httpapi.Write(rw, http.StatusOK, codersdk.UploadResponse{
Hash: file.Hash,
})
return
Expand All @@ -63,8 +61,8 @@ func (api *api) postFile(rw http.ResponseWriter, r *http.Request) {
})
return
}
render.Status(r, http.StatusCreated)
render.JSON(rw, r, codersdk.UploadResponse{

httpapi.Write(rw, http.StatusCreated, codersdk.UploadResponse{
Hash: file.Hash,
})
}
Expand Down
11 changes: 3 additions & 8 deletions coderd/gitsshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"net/http"

"github.com/go-chi/render"

"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/gitsshkey"
"github.com/coder/coder/coderd/httpapi"
Expand Down Expand Up @@ -44,8 +42,7 @@ func (api *api) regenerateGitSSHKey(rw http.ResponseWriter, r *http.Request) {
return
}

render.Status(r, http.StatusOK)
render.JSON(rw, r, codersdk.GitSSHKey{
httpapi.Write(rw, http.StatusOK, codersdk.GitSSHKey{
UserID: newKey.UserID,
CreatedAt: newKey.CreatedAt,
UpdatedAt: newKey.UpdatedAt,
Expand All @@ -64,8 +61,7 @@ func (api *api) gitSSHKey(rw http.ResponseWriter, r *http.Request) {
return
}

render.Status(r, http.StatusOK)
render.JSON(rw, r, codersdk.GitSSHKey{
httpapi.Write(rw, http.StatusOK, codersdk.GitSSHKey{
UserID: gitSSHKey.UserID,
CreatedAt: gitSSHKey.CreatedAt,
UpdatedAt: gitSSHKey.UpdatedAt,
Expand Down Expand Up @@ -108,8 +104,7 @@ func (api *api) agentGitSSHKey(rw http.ResponseWriter, r *http.Request) {
return
}

render.Status(r, http.StatusOK)
render.JSON(rw, r, codersdk.AgentGitSSHKey{
httpapi.Write(rw, http.StatusOK, codersdk.AgentGitSSHKey{
PrivateKey: gitSSHKey.PrivateKey,
})
}
2 changes: 1 addition & 1 deletion coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Error struct {
}

// Write outputs a standardized format to an HTTP response body.
func Write(rw http.ResponseWriter, status int, response Response) {
func Write(rw http.ResponseWriter, status int, response interface{}) {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(true)
Expand Down
4 changes: 1 addition & 3 deletions coderd/httpmw/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/go-chi/httprate"
"github.com/go-chi/render"

"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
Expand All @@ -26,8 +25,7 @@ func RateLimitPerMinute(count int) func(http.Handler) http.Handler {
return httprate.KeyByIP(r)
}, httprate.KeyByEndpoint),
httprate.WithLimitHandler(func(w http.ResponseWriter, r *http.Request) {
render.Status(r, http.StatusTooManyRequests)
render.JSON(w, r, httpapi.Response{
httpapi.Write(w, http.StatusTooManyRequests, httpapi.Response{
Message: "You've been rate limited for sending too many requests!",
})
}),
Expand Down
20 changes: 7 additions & 13 deletions coderd/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/google/uuid"
"github.com/moby/moby/pkg/namesgenerator"
"golang.org/x/xerrors"
Expand All @@ -20,8 +19,7 @@ import (

func (*api) organization(rw http.ResponseWriter, r *http.Request) {
organization := httpmw.OrganizationParam(r)
render.Status(r, http.StatusOK)
render.JSON(rw, r, convertOrganization(organization))
httpapi.Write(rw, http.StatusOK, convertOrganization(organization))
}

func (api *api) provisionerDaemonsByOrganization(rw http.ResponseWriter, r *http.Request) {
Expand All @@ -38,8 +36,7 @@ func (api *api) provisionerDaemonsByOrganization(rw http.ResponseWriter, r *http
if daemons == nil {
daemons = []database.ProvisionerDaemon{}
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, daemons)
httpapi.Write(rw, http.StatusOK, daemons)
}

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

render.Status(r, http.StatusCreated)
render.JSON(rw, r, convertTemplateVersion(templateVersion, convertProvisionerJob(provisionerJob)))
httpapi.Write(rw, http.StatusCreated, convertTemplateVersion(templateVersion, convertProvisionerJob(provisionerJob)))
}

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

render.Status(r, http.StatusCreated)
render.JSON(rw, r, template)
httpapi.Write(rw, http.StatusCreated, template)
}

func (api *api) templatesByOrganization(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -284,8 +279,8 @@ func (api *api) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
})
return
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, convertTemplates(templates, workspaceCounts))

httpapi.Write(rw, http.StatusOK, convertTemplates(templates, workspaceCounts))
}

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

render.Status(r, http.StatusOK)
render.JSON(rw, r, convertTemplate(template, count))
httpapi.Write(rw, http.StatusOK, convertTemplate(template, count))
}

// convertOrganization consumes the database representation and outputs an API friendly representation.
Expand Down
7 changes: 2 additions & 5 deletions coderd/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/google/uuid"

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

render.Status(r, http.StatusCreated)
render.JSON(rw, r, convertParameterValue(parameterValue))
httpapi.Write(rw, http.StatusCreated, convertParameterValue(parameterValue))
}

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

render.Status(r, http.StatusOK)
render.JSON(rw, r, apiParameterValues)
httpapi.Write(rw, http.StatusOK, apiParameterValues)
}

func (api *api) deleteParameter(rw http.ResponseWriter, r *http.Request) {
Expand Down
9 changes: 4 additions & 5 deletions coderd/provisionerjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strconv"
"time"

"github.com/go-chi/render"
"github.com/google/uuid"

"cdr.dev/slog"
Expand Down Expand Up @@ -87,8 +86,8 @@ func (api *api) provisionerJobLogs(rw http.ResponseWriter, r *http.Request, job
if logs == nil {
logs = []database.ProvisionerJobLog{}
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, logs)

httpapi.Write(rw, http.StatusOK, logs)
return
}

Expand Down Expand Up @@ -229,8 +228,8 @@ func (api *api) provisionerJobResources(rw http.ResponseWriter, r *http.Request,
}
apiResources = append(apiResources, convertWorkspaceResource(resource, agents))
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, apiResources)

httpapi.Write(rw, http.StatusOK, apiResources)
}

func convertProvisionerJobLog(provisionerJobLog database.ProvisionerJobLog) codersdk.ProvisionerJobLog {
Expand Down
11 changes: 4 additions & 7 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/google/uuid"

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

render.Status(r, http.StatusOK)
render.JSON(rw, r, convertTemplate(template, count))
httpapi.Write(rw, http.StatusOK, convertTemplate(template, count))
}

func (api *api) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -114,8 +112,8 @@ func (api *api) templateVersionsByTemplate(rw http.ResponseWriter, r *http.Reque
}
apiVersion = append(apiVersion, convertTemplateVersion(version, convertProvisionerJob(job)))
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, apiVersion)

httpapi.Write(rw, http.StatusOK, apiVersion)
}

func (api *api) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -148,8 +146,7 @@ func (api *api) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
return
}

render.Status(r, http.StatusOK)
render.JSON(rw, r, convertTemplateVersion(templateVersion, convertProvisionerJob(job)))
httpapi.Write(rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job)))
}

func (api *api) patchActiveTemplateVersion(rw http.ResponseWriter, r *http.Request) {
Expand Down
14 changes: 6 additions & 8 deletions coderd/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"fmt"
"net/http"

"github.com/go-chi/render"

"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/httpmw"
Expand All @@ -24,8 +22,8 @@ func (api *api) templateVersion(rw http.ResponseWriter, r *http.Request) {
})
return
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, convertTemplateVersion(templateVersion, convertProvisionerJob(job)))

httpapi.Write(rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job)))
}

func (api *api) patchCancelTemplateVersion(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -95,8 +93,8 @@ func (api *api) templateVersionSchema(rw http.ResponseWriter, r *http.Request) {
if schemas == nil {
schemas = []database.ParameterSchema{}
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, schemas)

httpapi.Write(rw, http.StatusOK, schemas)
}

func (api *api) templateVersionParameters(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -132,8 +130,8 @@ func (api *api) templateVersionParameters(rw http.ResponseWriter, r *http.Reques
if values == nil {
values = []parameter.ComputedValue{}
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, values)

httpapi.Write(rw, http.StatusOK, values)
}

func (api *api) templateVersionResources(rw http.ResponseWriter, r *http.Request) {
Expand Down
Loading