mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: refactor workspace count to single route (#4809)
Co-authored-by: Presley Pizzo <presley@coder.com>
This commit is contained in:
co-authored by
Presley Pizzo
parent
5fb9c33ecd
commit
766a2ad590
+8
-38
@@ -38,11 +38,9 @@ type WorkspacesRequest struct {
|
||||
Pagination
|
||||
}
|
||||
|
||||
type WorkspaceCountRequest struct {
|
||||
SearchQuery string `json:"q,omitempty"`
|
||||
}
|
||||
type WorkspaceCountResponse struct {
|
||||
Count int64 `json:"count"`
|
||||
type WorkspacesResponse struct {
|
||||
Workspaces []Workspace `json:"workspaces"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
// CreateWorkspaceBuildRequest provides options to update the latest workspace build.
|
||||
@@ -310,51 +308,23 @@ func (f WorkspaceFilter) asRequestOption() RequestOption {
|
||||
}
|
||||
|
||||
// Workspaces returns all workspaces the authenticated user has access to.
|
||||
func (c *Client) Workspaces(ctx context.Context, filter WorkspaceFilter) ([]Workspace, error) {
|
||||
func (c *Client) Workspaces(ctx context.Context, filter WorkspaceFilter) (WorkspacesResponse, error) {
|
||||
page := Pagination{
|
||||
Offset: filter.Offset,
|
||||
Limit: filter.Limit,
|
||||
}
|
||||
res, err := c.Request(ctx, http.MethodGet, "/api/v2/workspaces", nil, filter.asRequestOption(), page.asRequestOption())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return WorkspacesResponse{}, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, readBodyAsError(res)
|
||||
return WorkspacesResponse{}, readBodyAsError(res)
|
||||
}
|
||||
|
||||
var workspaces []Workspace
|
||||
return workspaces, json.NewDecoder(res.Body).Decode(&workspaces)
|
||||
}
|
||||
|
||||
func (c *Client) WorkspaceCount(ctx context.Context, req WorkspaceCountRequest) (WorkspaceCountResponse, error) {
|
||||
res, err := c.Request(ctx, http.MethodGet, "/api/v2/workspaces/count", nil, func(r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
var params []string
|
||||
if req.SearchQuery != "" {
|
||||
params = append(params, req.SearchQuery)
|
||||
}
|
||||
q.Set("q", strings.Join(params, " "))
|
||||
r.URL.RawQuery = q.Encode()
|
||||
})
|
||||
if err != nil {
|
||||
return WorkspaceCountResponse{}, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return WorkspaceCountResponse{}, readBodyAsError(res)
|
||||
}
|
||||
|
||||
var countRes WorkspaceCountResponse
|
||||
err = json.NewDecoder(res.Body).Decode(&countRes)
|
||||
if err != nil {
|
||||
return WorkspaceCountResponse{}, err
|
||||
}
|
||||
|
||||
return countRes, nil
|
||||
var wres WorkspacesResponse
|
||||
return wres, json.NewDecoder(res.Body).Decode(&wres)
|
||||
}
|
||||
|
||||
// WorkspaceByOwnerAndName returns a workspace by the owner's UUID and the workspace's name.
|
||||
|
||||
Reference in New Issue
Block a user