73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"git.franklin.lab/steve.cliff/pcli/model"
|
|
)
|
|
|
|
// ProjectCreateFields represents the fields required to create a project
|
|
type ProjectCreateFields struct {
|
|
Type string `json:"type"`
|
|
Name string `json:"name"`
|
|
Description *string `json:"description,omitempty"`
|
|
}
|
|
|
|
func (c *Client) ListProjects(ctx context.Context) ([]model.Project, error) {
|
|
data, err := c.DoNoBody(ctx, "GET", "/api/projects")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var response struct {
|
|
Items []model.Project `json:"items"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &response); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal projects response: %w", err)
|
|
}
|
|
|
|
return response.Items, nil
|
|
}
|
|
|
|
func (c *Client) GetProject(ctx context.Context, id string) (*model.Project, error) {
|
|
data, err := c.DoNoBody(ctx, "GET", fmt.Sprintf("/api/projects/%s", id))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var response struct {
|
|
Item model.Project `json:"item"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &response); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal project response: %w", err)
|
|
}
|
|
|
|
return &response.Item, nil
|
|
}
|
|
|
|
func (c *Client) CreateProject(ctx context.Context, fields ProjectCreateFields) (*model.Project, error) {
|
|
data, err := c.Do(ctx, "POST", "/api/projects", fields)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var response struct {
|
|
Item model.Project `json:"item"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &response); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal project response: %w", err)
|
|
}
|
|
|
|
return &response.Item, nil
|
|
}
|
|
|
|
func (c *Client) DeleteProject(ctx context.Context, id string) error {
|
|
_, err := c.DoNoBody(ctx, "DELETE", fmt.Sprintf("/api/projects/%s", id))
|
|
return err
|
|
}
|