This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
user.go
116 lines (84 loc) · 1.96 KB
/
user.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
115
116
package zendesk
import (
"fmt"
"gopkg.in/resty.v0"
)
type UserApiHandler struct {
client Client
}
type SingleUser struct {
Response User `json:"user"`
}
type MultipleUser struct {
Response []User `json:"users"`
}
func (u UserApiHandler) GetById(id int) (User, error) {
response, err := u.client.get(
fmt.Sprintf("/users/%d.json", id),
nil,
)
if err != nil {
}
return u.parseSingleObject(response), err
}
func (u UserApiHandler) GetAll() ([]User, error) {
response, err := u.client.get(
"/users.json",
nil,
)
if err != nil {
}
return u.parseMultiObjects(response), err
}
func (u UserApiHandler) Create(v User) (User, error) {
var object SingleUser
_, err := u.client.post(
"/users.json",
map[string]User{"user": v},
&object,
)
return object.Response, err
}
func (u UserApiHandler) CreateOrUpdate(v User) (User, error) {
var object SingleUser
_, err := u.client.post(
"/users/create_or_update.json",
map[string]User{"user": v},
&object,
)
return object.Response, err
}
func (u UserApiHandler) CreateOrUpdateMany(v []User) (Job, error) {
var object Job
_, err := u.client.post(
"/users/create_or_update_many.json",
map[string][]User{"users": v},
&object,
)
return object, err
}
func (u UserApiHandler) Update(v User) (User, error) {
var object SingleUser
_, err := u.client.put(
fmt.Sprintf("/users/%d.json", v.Id),
map[string]User{"user": v},
&object,
)
return object.Response, err
}
func (u UserApiHandler) Delete(id int) (int, error) {
response, err := u.client.delete(
fmt.Sprintf("/users/%d.json", id),
)
return response.StatusCode(), err
}
func (u UserApiHandler) parseMultiObjects(response *resty.Response) []User {
var object MultipleUser
u.client.parseResponseToInterface(response, &object)
return object.Response
}
func (u UserApiHandler) parseSingleObject(response *resty.Response) User {
var object SingleUser
u.client.parseResponseToInterface(response, &object)
return object.Response
}