-
Notifications
You must be signed in to change notification settings - Fork 2
/
identity.go
114 lines (91 loc) · 2.86 KB
/
identity.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package flagsmithapi
import (
"fmt"
)
func (c *Client) GetIdentity(environmentKey string, identityID int64) (*Identity, error) {
url := fmt.Sprintf("%s/environments/%s/identities/%d/", c.baseURL, environmentKey, identityID)
identity := Identity{}
resp, err := c.client.R().SetResult(&identity).Get(url)
if err != nil {
return nil, err
}
if !resp.IsSuccess() {
return nil, fmt.Errorf("flagsmithapi: Error fetching identity: %v", resp)
}
return &identity, nil
}
func (c *Client) CreateIdentity(environmentKey string, identity *Identity) error {
url := fmt.Sprintf("%s/environments/%s/identities/", c.baseURL, environmentKey)
resp, err := c.client.R().SetBody(identity).SetResult(&identity).Post(url)
if err != nil {
return err
}
if !resp.IsSuccess() {
return fmt.Errorf("flagsmithapi: Error creating identity: %s", resp)
}
return nil
}
func (c *Client) DeleteIdentity(environmentKey string, identityID int64) error {
url := fmt.Sprintf("%s/environments/%s/identities/%d/", c.baseURL, environmentKey, identityID)
resp, err := c.client.R().Delete(url)
if err != nil {
return err
}
if !resp.IsSuccess() {
return fmt.Errorf("flagsmithapi: Error deleting identity: %s", resp)
}
return nil
}
func (c *Client) GetTraits(environmentKey string, identityID int64) ([]Trait, error) {
url := fmt.Sprintf("%s/environments/%s/identities/%d/traits/", c.baseURL, environmentKey, identityID)
result := struct {
Traits []Trait `json:"results"`
}{}
resp, err := c.client.R().SetResult(&result).Get(url)
if err != nil {
return nil, err
}
if !resp.IsSuccess() {
return nil, fmt.Errorf("flagsmithapi: Error fetching traits: %v", resp)
}
return result.Traits, nil
}
func (c *Client) CreateTrait(environmentKey string, identityID int64, trait *Trait) error {
url := fmt.Sprintf("%s/environments/%s/identities/%d/traits/", c.baseURL, environmentKey, identityID)
resp, err := c.client.R().
SetBody(trait).
SetResult(&trait).
Post(url)
if err != nil {
return err
}
if !resp.IsSuccess() {
return fmt.Errorf("flagsmithapi: Error creating trait: %v", resp)
}
return nil
}
func (c *Client) UpdateTrait(environmentKey string, identityID int64, trait *Trait) error {
url := fmt.Sprintf("%s/environments/%s/identities/%d/traits/%d/", c.baseURL, environmentKey, identityID, trait.ID)
resp, err := c.client.R().
SetBody(trait).
Put(url)
if err != nil {
return err
}
if !resp.IsSuccess() {
return fmt.Errorf("flagsmithapi: Error updating trait: %v", resp)
}
return nil
}
func (c *Client) DeleteTrait(environmentKey string, identityID int64, traitID int64) error {
url := fmt.Sprintf("%s/environments/%s/identities/%d/traits/%d/", c.baseURL, environmentKey, identityID, traitID)
resp, err := c.client.R().
Delete(url)
if err != nil {
return err
}
if !resp.IsSuccess() {
return fmt.Errorf("flagsmithapi: Error deleting trait: %v", resp)
}
return nil
}