57 lines
1.9 KiB
Go
57 lines
1.9 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
// CreateTask creates a work item in a garden.
|
|
func (c *Client) CreateTask(ctx context.Context, gardenID int, input TaskInput) (Task, *Response, error) {
|
|
return c.writeTask(ctx, http.MethodPost, taskCollectionPath(gardenID), input)
|
|
}
|
|
|
|
// Tasks lists work items in a garden.
|
|
func (c *Client) Tasks(ctx context.Context, gardenID int) ([]Task, *Response, error) {
|
|
var envelope struct {
|
|
Tasks []Task `json:"tasks"`
|
|
}
|
|
response, err := c.do(ctx, http.MethodGet, taskCollectionPath(gardenID), nil, &envelope)
|
|
return envelope.Tasks, response, err
|
|
}
|
|
|
|
// Task returns one work item within its garden.
|
|
func (c *Client) Task(ctx context.Context, gardenID, taskID int) (Task, *Response, error) {
|
|
var envelope struct {
|
|
Task Task `json:"task"`
|
|
}
|
|
response, err := c.do(ctx, http.MethodGet, taskPath(gardenID, taskID), nil, &envelope)
|
|
return envelope.Task, response, err
|
|
}
|
|
|
|
// UpdateTask partially updates a work item within its garden.
|
|
func (c *Client) UpdateTask(ctx context.Context, gardenID, taskID int, input TaskInput) (Task, *Response, error) {
|
|
return c.writeTask(ctx, http.MethodPatch, taskPath(gardenID, taskID), input)
|
|
}
|
|
|
|
// DeleteTask removes a work item from its garden.
|
|
func (c *Client) DeleteTask(ctx context.Context, gardenID, taskID int) (*Response, error) {
|
|
return c.do(ctx, http.MethodDelete, taskPath(gardenID, taskID), nil, nil)
|
|
}
|
|
|
|
func (c *Client) writeTask(ctx context.Context, method, path string, input TaskInput) (Task, *Response, error) {
|
|
var envelope struct {
|
|
Task Task `json:"task"`
|
|
}
|
|
response, err := c.do(ctx, method, path, input, &envelope)
|
|
return envelope.Task, response, err
|
|
}
|
|
|
|
func taskCollectionPath(gardenID int) string {
|
|
return "v1/gardens/" + strconv.Itoa(gardenID) + "/tasks"
|
|
}
|
|
|
|
func taskPath(gardenID, taskID int) string {
|
|
return taskCollectionPath(gardenID) + "/" + strconv.Itoa(taskID)
|
|
}
|