on main help sign in
fix(client): add ListWorkspaces method to retrieve all workspaces from the server
eb7ed4fAyman Bagabas
internal/client/proto.go+17
@@ -22,6 +22,23 @@ import (
22 "github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
22 "github.com/charmbracelet/x/powernap/pkg/lsp/protocol"
23)
23)
24
24
25+// ListWorkspaces retrieves all workspaces from the server.
26+func (c *Client) ListWorkspaces(ctx context.Context) ([]proto.Workspace, error) {
27+ rsp, err := c.get(ctx, "/workspaces", nil, nil)
28+ if err != nil {
29+ return nil, fmt.Errorf("failed to list workspaces: %w", err)
30+ }
31+ defer rsp.Body.Close()
32+ if rsp.StatusCode != http.StatusOK {
33+ return nil, fmt.Errorf("failed to list workspaces: status code %d", rsp.StatusCode)
34+ }
35+ var workspaces []proto.Workspace
36+ if err := json.NewDecoder(rsp.Body).Decode(&workspaces); err != nil {
37+ return nil, fmt.Errorf("failed to decode workspaces: %w", err)
38+ }
39+ return workspaces, nil
40+}
41+
25// CreateWorkspace creates a new workspace on the server.
42// CreateWorkspace creates a new workspace on the server.
26func (c *Client) CreateWorkspace(ctx context.Context, ws proto.Workspace) (*proto.Workspace, error) {
43func (c *Client) CreateWorkspace(ctx context.Context, ws proto.Workspace) (*proto.Workspace, error) {
27 rsp, err := c.post(ctx, "/workspaces", nil, jsonBody(ws), http.Header{"Content-Type": []string{"application/json"}})
44 rsp, err := c.post(ctx, "/workspaces", nil, jsonBody(ws), http.Header{"Content-Type": []string{"application/json"}})