-
Notifications
You must be signed in to change notification settings - Fork 3
/
entity.go
78 lines (65 loc) · 2.06 KB
/
entity.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 lytics
import (
"encoding/json"
"fmt"
"net/url"
"strings"
)
const (
entityEndpoint = "entity"
)
// EntityHandler for use in paging
type EntityHandler func(*Entity)
// Entity is the main data source for Lytics User Profiles/Entities.
// All users, content, etc. are referred to as "entities".
type Entity struct {
Fields map[string]interface{}
Meta map[string]interface{}
}
// GetEntity returns all the availble attributes for a single entity (user, content, etc)
// https://learn.lytics.com/api-docs/personalization
func (l *Client) GetEntity(entitytype, fieldname, fieldval string, fields []string) (*Entity, error) {
return l.GetEntityParams(entitytype, fieldname, fieldval, fields, url.Values{})
}
// GetEntity returns all the availble attributes for a single entity (user, content, etc)
// https://learn.lytics.com/api-docs/personalization
func (l *Client) GetEntityParams(entitytype, fieldname, fieldval string, fields []string, params url.Values) (*Entity, error) {
res := ApiResp{}
data := Entity{}
entFields := make(map[string]interface{})
toAppend := ""
endpointParams := map[string]string{}
// handle optional endpointParams
if entitytype == "" {
entitytype = "user"
}
toAppend = fmt.Sprintf("/%s", ":entitytype")
endpointParams["entitytype"] = entitytype
if fieldname != "" {
toAppend = fmt.Sprintf("%s/%s", toAppend, ":fieldname")
endpointParams["fieldname"] = fieldname
if fieldval != "" {
toAppend = fmt.Sprintf("%s/%s", toAppend, ":fieldval")
endpointParams["fieldval"] = fieldval
}
}
// build dynamic endpoint
endpoint := fmt.Sprintf("%s%s", entityEndpoint, toAppend)
// if there are also fields, add them
if len(fields) > 0 {
params.Add("fields", strings.Join(fields, ","))
}
// make the request
entityUrl := parseLyticsURL(endpoint, endpointParams)
err := l.Get(entityUrl, params, nil, &res, &entFields)
if err != nil {
return nil, err
}
data.Meta = res.Meta
data.Fields = entFields
return &data, nil
}
func (e *Entity) PrettyJson() string {
by, _ := json.MarshalIndent(e.Fields, "", " ")
return string(by)
}