57 lines
2.0 KiB
Go
57 lines
2.0 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
// CreateLocation creates a location within a garden.
|
|
func (c *Client) CreateLocation(ctx context.Context, gardenID int, input LocationInput) (Location, *Response, error) {
|
|
return c.writeLocation(ctx, http.MethodPost, locationCollectionPath(gardenID), input)
|
|
}
|
|
|
|
// Locations lists all locations in a garden.
|
|
func (c *Client) Locations(ctx context.Context, gardenID int) ([]Location, *Response, error) {
|
|
var envelope struct {
|
|
Locations []Location `json:"locations"`
|
|
}
|
|
response, err := c.do(ctx, http.MethodGet, locationCollectionPath(gardenID), nil, &envelope)
|
|
return envelope.Locations, response, err
|
|
}
|
|
|
|
// Location returns one location from a garden.
|
|
func (c *Client) Location(ctx context.Context, gardenID, locationID int) (Location, *Response, error) {
|
|
var envelope struct {
|
|
Location Location `json:"location"`
|
|
}
|
|
response, err := c.do(ctx, http.MethodGet, locationPath(gardenID, locationID), nil, &envelope)
|
|
return envelope.Location, response, err
|
|
}
|
|
|
|
// UpdateLocation partially updates a location.
|
|
func (c *Client) UpdateLocation(ctx context.Context, gardenID, locationID int, input LocationInput) (Location, *Response, error) {
|
|
return c.writeLocation(ctx, http.MethodPatch, locationPath(gardenID, locationID), input)
|
|
}
|
|
|
|
// DeleteLocation deletes a location.
|
|
func (c *Client) DeleteLocation(ctx context.Context, gardenID, locationID int) (*Response, error) {
|
|
return c.do(ctx, http.MethodDelete, locationPath(gardenID, locationID), nil, nil)
|
|
}
|
|
|
|
func (c *Client) writeLocation(ctx context.Context, method, path string, input LocationInput) (Location, *Response, error) {
|
|
var envelope struct {
|
|
Location Location `json:"location"`
|
|
}
|
|
response, err := c.do(ctx, method, path, input, &envelope)
|
|
return envelope.Location, response, err
|
|
}
|
|
|
|
func locationCollectionPath(gardenID int) string {
|
|
return "v1/gardens/" + strconv.Itoa(gardenID) + "/locations"
|
|
}
|
|
|
|
func locationPath(gardenID, locationID int) string {
|
|
return locationCollectionPath(gardenID) + "/" + strconv.Itoa(locationID)
|
|
}
|