summaryrefslogtreecommitdiff
path: root/grocy
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2023-09-03 11:51:38 -0400
committerCara Salter <cara@devcara.com>2023-09-03 11:51:38 -0400
commit1a69cd2e8b919ab450b42802ae578c2d23948a07 (patch)
tree2e0e1bfaa96e80f0a671b80cb197ad81f953ece1 /grocy
downloadgrocy-reminders-1a69cd2e8b919ab450b42802ae578c2d23948a07.tar.gz
grocy-reminders-1a69cd2e8b919ab450b42802ae578c2d23948a07.zip
Initial commit
Create API client for grocy
Diffstat (limited to 'grocy')
-rw-r--r--grocy/chores.go53
-rw-r--r--grocy/grocy.go57
-rw-r--r--grocy/user.go53
3 files changed, 163 insertions, 0 deletions
diff --git a/grocy/chores.go b/grocy/chores.go
new file mode 100644
index 0000000..3dcdf2e
--- /dev/null
+++ b/grocy/chores.go
@@ -0,0 +1,53 @@
+package grocy
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+)
+
+type Chore struct {
+ ID int `json:"id"`
+ ChoreID int `json:"chore_id"`
+ ChoreName string `json:"chore_name"`
+ LastTrackedTime string `json:"last_tracked_time"`
+ NextEstimatedExecutionTime string `json:"next_estimated_execution_time"`
+ TrackDateOnly int `json:"track_date_only"`
+ NextExecutionAssignedToUserID int `json:"next_execution_assigned_to_user_id"`
+ IsRescheduled int `json:"is_rescheduled"`
+ IsReassigned int `json:"is_reassigned"`
+ NextExecutionAssignedUser User `json:"next_execution_assigned_user"`
+}
+
+type ChoreList []Chore
+
+func (c *Client) GetChores(ctx context.Context) (ChoreList, error) {
+ req, err := http.NewRequest("GET", fmt.Sprintf("%s/chores", c.BaseUrl), nil)
+
+ if err != nil {
+ return nil, err
+ }
+ req.WithContext(ctx)
+
+ var res ChoreList
+ if err := c.sendRequest(req, &res); err != nil {
+ return nil, err
+ }
+
+ return res, nil
+}
+
+func (c *Client) GetChore(ctx context.Context, choreId int) (Chore, error) {
+ req, err := http.NewRequest("GET", fmt.Sprintf("%s/chores/%d", c.BaseUrl, choreId), nil)
+
+ if err != nil {
+ return *new(Chore), err
+ }
+ req.WithContext(ctx)
+
+ var res Chore
+ if err := c.sendRequest(req, &res); err != nil {
+ return *new(Chore), err
+ }
+ return res, nil
+}
diff --git a/grocy/grocy.go b/grocy/grocy.go
new file mode 100644
index 0000000..55c3d2d
--- /dev/null
+++ b/grocy/grocy.go
@@ -0,0 +1,57 @@
+package grocy
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "net/http"
+ "time"
+)
+
+type Client struct {
+ BaseUrl string
+ apiKey string
+ HTTPClient *http.Client
+}
+
+func NewClient(baseUrl, apiKey string) *Client {
+ return &Client{
+ BaseUrl: baseUrl,
+ apiKey: apiKey,
+ HTTPClient: &http.Client{
+ Timeout: time.Minute,
+ },
+ }
+}
+
+type ErrorResponse struct {
+ ErrorMessage string `json:"error_message"`
+}
+
+func (c *Client) sendRequest(req *http.Request, v interface{}) error {
+ req.Header.Set("Content-Type", "application/json; charset=utf-8")
+ req.Header.Set("Accept", "application/json; charset=utf-8")
+ req.Header.Set("GROCY-API-KEY", c.apiKey)
+
+ res, err := c.HTTPClient.Do(req)
+ if err != nil {
+ return err
+ }
+
+ defer res.Body.Close()
+
+ if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
+ var errRes ErrorResponse
+ if err = json.NewDecoder(res.Body).Decode(&errRes); err == nil {
+ return errors.New(errRes.ErrorMessage)
+ }
+
+ return fmt.Errorf("unknown error, status code: %d", res.StatusCode)
+ }
+
+ if err = json.NewDecoder(res.Body).Decode(&v); err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/grocy/user.go b/grocy/user.go
new file mode 100644
index 0000000..58d3d08
--- /dev/null
+++ b/grocy/user.go
@@ -0,0 +1,53 @@
+package grocy
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+)
+
+type (
+ User struct {
+ Id int `json:"id"`
+ Username string `json:"username"`
+ FirstName string `json:"first_name"`
+ LastName string `json:"last_name"`
+ RowCreated string `json:"row_created_timestamp"`
+ DisplayName string `json:"display_name"`
+ PictureFileName string `json:"picture_file_name"`
+ }
+)
+
+func (c *Client) GetUsers(ctx context.Context) ([]User, error) {
+ req, err := http.NewRequest("GET", fmt.Sprintf("%s/users", c.BaseUrl), nil)
+
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Add("GROCY-API-KEY", c.apiKey)
+ req.WithContext(ctx)
+
+ var res []User
+ if err := c.sendRequest(req, &res); err != nil {
+ return nil, err
+ }
+
+ return res, nil
+}
+
+func (c *Client) GetUserFields(ctx context.Context, userId int) (map[string]string, error) {
+ req, err := http.NewRequest("GET", fmt.Sprintf("%s/userfields/users/%d", c.BaseUrl, userId), nil)
+
+ if err != nil {
+ return nil, err
+ }
+
+ req.WithContext(ctx)
+
+ var res map[string]string
+ if err := c.sendRequest(req, &res); err != nil {
+ return nil, err
+ }
+
+ return res, nil
+}