57 lines
1.9 KiB
Go
57 lines
1.9 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
// CreatePlant creates a plant instance in a garden.
|
|
func (c *Client) CreatePlant(ctx context.Context, gardenID int, input PlantInput) (Plant, *Response, error) {
|
|
return c.writePlant(ctx, http.MethodPost, plantCollectionPath(gardenID), input)
|
|
}
|
|
|
|
// Plants lists all plant instances in a garden.
|
|
func (c *Client) Plants(ctx context.Context, gardenID int) ([]Plant, *Response, error) {
|
|
var envelope struct {
|
|
Plants []Plant `json:"plants"`
|
|
}
|
|
response, err := c.do(ctx, http.MethodGet, plantCollectionPath(gardenID), nil, &envelope)
|
|
return envelope.Plants, response, err
|
|
}
|
|
|
|
// Plant returns one plant from a garden.
|
|
func (c *Client) Plant(ctx context.Context, gardenID, plantID int) (Plant, *Response, error) {
|
|
var envelope struct {
|
|
Plant Plant `json:"plant"`
|
|
}
|
|
response, err := c.do(ctx, http.MethodGet, plantPath(gardenID, plantID), nil, &envelope)
|
|
return envelope.Plant, response, err
|
|
}
|
|
|
|
// UpdatePlant partially updates a plant in a garden.
|
|
func (c *Client) UpdatePlant(ctx context.Context, gardenID, plantID int, input PlantInput) (Plant, *Response, error) {
|
|
return c.writePlant(ctx, http.MethodPatch, plantPath(gardenID, plantID), input)
|
|
}
|
|
|
|
// DeletePlant deletes a plant from a garden.
|
|
func (c *Client) DeletePlant(ctx context.Context, gardenID, plantID int) (*Response, error) {
|
|
return c.do(ctx, http.MethodDelete, plantPath(gardenID, plantID), nil, nil)
|
|
}
|
|
|
|
func (c *Client) writePlant(ctx context.Context, method, path string, input PlantInput) (Plant, *Response, error) {
|
|
var envelope struct {
|
|
Plant Plant `json:"plant"`
|
|
}
|
|
response, err := c.do(ctx, method, path, input, &envelope)
|
|
return envelope.Plant, response, err
|
|
}
|
|
|
|
func plantCollectionPath(gardenID int) string {
|
|
return "v1/gardens/" + strconv.Itoa(gardenID) + "/plants"
|
|
}
|
|
|
|
func plantPath(gardenID, plantID int) string {
|
|
return plantCollectionPath(gardenID) + "/" + strconv.Itoa(plantID)
|
|
}
|