This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
collection.go
78 lines (67 loc) · 2.32 KB
/
collection.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package nbiot
import (
"fmt"
"time"
)
// Collection represents a collection.
type Collection struct {
ID string `json:"collectionId"`
TeamID string `json:"teamId,omitempty"`
FieldMask *FieldMask `json:"fieldMask,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
}
// FieldMask indicates which fields will be masked from API responses.
type FieldMask struct {
IMSI *bool `json:"imsi"`
IMEI *bool `json:"imei"`
Location *bool `json:"location"`
MSISDN *bool `json:"msisdn"`
}
// Collection gets a collection.
func (c *Client) Collection(id string) (Collection, error) {
var collection Collection
err := c.get("/collections/"+id, &collection)
return collection, err
}
// Collections gets all collections that the user has access to.
func (c *Client) Collections() ([]Collection, error) {
var collections struct {
Collections []Collection `json:"collections"`
}
err := c.get("/collections", &collections)
return collections.Collections, err
}
// CreateCollection creates a collection.
func (c *Client) CreateCollection(collection Collection) (Collection, error) {
err := c.create("/collections", &collection)
return collection, err
}
// UpdateCollection updates a collection.
// No tags are deleted, only added or updated.
func (c *Client) UpdateCollection(collection Collection) (Collection, error) {
err := c.update("/collections/"+collection.ID, &collection)
return collection, err
}
// DeleteCollectionTag deletes a tag from a collection.
func (c *Client) DeleteCollectionTag(id, name string) error {
return c.delete(fmt.Sprintf("/collections/%s/tags/%s", id, name))
}
// DeleteCollection deletes a collection.
func (c *Client) DeleteCollection(id string) error {
return c.delete("/collections/" + id)
}
// CollectionData returns all the stored data for the collection.
func (c *Client) CollectionData(collectionID string, since time.Time, until time.Time, limit int) ([]OutputDataMessage, error) {
var s, u int64
if !since.IsZero() {
s = since.UnixNano() / int64(time.Millisecond)
}
if !until.IsZero() {
u = until.UnixNano() / int64(time.Millisecond)
}
var data struct {
Messages []OutputDataMessage `json:"messages"`
}
err := c.get(fmt.Sprintf("/collections/%s/data?since=%d&until=%d&limit=%d", collectionID, s, u, limit), &data)
return data.Messages, err
}