feat: Single query for all workspaces with optional filter (#1537)

* feat: Add single query for all workspaces using a filter
This commit is contained in:
Steven Masley
2022-05-18 10:09:07 -05:00
committed by GitHub
parent 894646cb7c
commit a3556b12da
16 changed files with 254 additions and 196 deletions

View File

@ -131,3 +131,41 @@ func (c *Client) UpdateWorkspaceAutostop(ctx context.Context, id uuid.UUID, req
}
return nil
}
type WorkspaceFilter struct {
OrganizationID uuid.UUID
// Owner can be a user_id (uuid), "me", or a username
Owner string
}
// asRequestOption returns a function that can be used in (*Client).Request.
// It modifies the request query parameters.
func (f WorkspaceFilter) asRequestOption() requestOption {
return func(r *http.Request) {
q := r.URL.Query()
if f.OrganizationID != uuid.Nil {
q.Set("organization_id", f.OrganizationID.String())
}
if f.Owner != "" {
q.Set("owner_id", f.Owner)
}
r.URL.RawQuery = q.Encode()
}
}
// Workspaces returns all workspaces the authenticated user has access to.
func (c *Client) Workspaces(ctx context.Context, filter WorkspaceFilter) ([]Workspace, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/workspaces", nil, filter.asRequestOption())
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, readBodyAsError(res)
}
var workspaces []Workspace
return workspaces, json.NewDecoder(res.Body).Decode(&workspaces)
}