forked from intercom/intercom-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_api.go
105 lines (91 loc) · 3.41 KB
/
user_api.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
package intercom
import (
"encoding/json"
"errors"
"fmt"
"gopkg.in/intercom/intercom-go.v2/interfaces"
)
// UserRepository defines the interface for working with Users through the API.
type UserRepository interface {
find(UserIdentifiers) (User, error)
list(userListParams) (UserList, error)
scroll(scrollParam string) (UserList, error)
save(*User) (User, error)
delete(id string) (User, error)
}
// UserAPI implements UserRepository
type UserAPI struct {
httpClient interfaces.HTTPClient
}
type requestScroll struct {
ScrollParam string `json:"scroll_param,omitempty"`
}
type requestUser struct {
ID string `json:"id,omitempty"`
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
UserID string `json:"user_id,omitempty"`
Name string `json:"name,omitempty"`
SignedUpAt int64 `json:"signed_up_at,omitempty"`
RemoteCreatedAt int64 `json:"remote_created_at,omitempty"`
LastRequestAt int64 `json:"last_request_at,omitempty"`
LastSeenIP string `json:"last_seen_ip,omitempty"`
UnsubscribedFromEmails *bool `json:"unsubscribed_from_emails,omitempty"`
Companies []UserCompany `json:"companies,omitempty"`
CustomAttributes map[string]interface{} `json:"custom_attributes,omitempty"`
UpdateLastRequestAt *bool `json:"update_last_request_at,omitempty"`
NewSession *bool `json:"new_session,omitempty"`
LastSeenUserAgent string `json:"last_seen_user_agent,omitempty"`
}
func (api UserAPI) find(params UserIdentifiers) (User, error) {
return unmarshalToUser(api.getClientForFind(params))
}
func (api UserAPI) getClientForFind(params UserIdentifiers) ([]byte, error) {
switch {
case params.ID != "":
return api.httpClient.Get(fmt.Sprintf("/users/%s", params.ID), nil)
case params.UserID != "", params.Email != "":
return api.httpClient.Get("/users", params)
}
return nil, errors.New("Missing User Identifier")
}
func (api UserAPI) list(params userListParams) (UserList, error) {
userList := UserList{}
data, err := api.httpClient.Get("/users", params)
if err != nil {
return userList, err
}
err = json.Unmarshal(data, &userList)
return userList, err
}
func (api UserAPI) scroll(scrollParam string) (UserList, error) {
userList := UserList{}
url := "/users/scroll"
params := scrollParams{ ScrollParam: scrollParam }
data, err := api.httpClient.Get(url, params)
if err != nil {
return userList, err
}
err = json.Unmarshal(data, &userList)
return userList, err
}
func (api UserAPI) save(user *User) (User, error) {
return unmarshalToUser(api.httpClient.Post("/users", RequestUserMapper{}.ConvertUser(user)))
}
func unmarshalToUser(data []byte, err error) (User, error) {
savedUser := User{}
if err != nil {
return savedUser, err
}
err = json.Unmarshal(data, &savedUser)
return savedUser, err
}
func (api UserAPI) delete(id string) (User, error) {
user := User{}
data, err := api.httpClient.Delete(fmt.Sprintf("/users/%s", id), nil)
if err != nil {
return user, err
}
err = json.Unmarshal(data, &user)
return user, err
}