-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeofences.go
57 lines (48 loc) · 1.78 KB
/
geofences.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package client
import (
"fmt"
"net/http"
)
// Geofence describes a Geofence schema object
type Geofence struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Area string `json:"area"`
CalendarID int `json:"calendarId"`
Attributes Attributes `json:"attributes"`
}
// ListGeofences fetches a list of Geofences
// Without params, it returns a list of Geofences the user has access to
//
// Params:
// WithAll - Can only be used by admins or managers to fetch all entities
// WithUserID - Standard users can use this only with their own userId
// WithDeviceID - Standard users can use this only with _deviceId_s, they have access to
// WithGroupID - Standard users can use this only with _groupId_s, they have access to
// Refresh
func (c *Client) ListGeofences(params ...QueryParameter) (geofences []Geofence, err error) {
err = c.doRequest(http.MethodGet, "geofences"+query(params), nil, &geofences)
return
}
// CreateGeofence creates a Geofence
func (c *Client) CreateGeofence(geofence Geofence) (Geofence, error) {
return c.doGeofence(http.MethodPost, "geofences", geofence)
}
// DeleteGeofence deletes a Geofence
func (c *Client) DeleteGeofence(id int) error {
return c.doRequest(http.MethodDelete, fmt.Sprintf("geofences/%d", id), nil, nil)
}
// UpdateGeofence updates a Geofence
func (c *Client) UpdateGeofence(id int, geofence Geofence) (Geofence, error) {
geofence.ID = id
return c.doGeofence(http.MethodPut, fmt.Sprintf("geofence/%d", id), geofence)
}
func (c *Client) doGeofence(method, path string, geofence Geofence) (Geofence, error) {
body, err := jsonBody(geofence)
if err != nil {
return geofence, err
}
err = c.doRequest(method, path, body, &geofence)
return geofence, err
}