forked from talbright/go-desk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply_service.go
108 lines (100 loc) · 2.9 KB
/
reply_service.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
package service
import (
"encoding/json"
. "github.com/talbright/go-desk/resource"
"net/http"
"net/url"
)
type ReplyService struct {
client *Client
}
// Get retrieves a reply for a case.
// See Desk API: http://dev.desk.com/API/cases/#replies-show
func (c *ReplyService) Get(caseId string, replyId string) (*Reply, *http.Response, error) {
restful := Restful{}
reply := NewReply()
replyPath := NewIdentityResourcePath(replyId, NewReply())
casePath := NewIdentityResourcePath(caseId, NewCase()).AppendPath(replyPath)
resp, err := restful.
Get(casePath.Path()).
Json(reply).
Client(c.client).
Do()
return reply, resp, err
}
// List replies with filtering and pagination.
// See Desk API: http://dev.desk.com/API/cases/#replies-list
func (c *ReplyService) List(caseId string, params *url.Values) (*Page, *http.Response, error) {
restful := Restful{}
page := new(Page)
replyPath := NewResourcePath(NewReply())
casePath := NewIdentityResourcePath(caseId, NewCase()).AppendPath(replyPath)
resp, err := restful.
Get(casePath.Path()).
Json(page).
Params(params).
Client(c.client).
Do()
if err != nil {
return nil, resp, err
}
err = c.unravelPage(page)
if err != nil {
return nil, nil, err
}
return page, resp, err
}
// Create a reply.
// See Desk API: http://dev.desk.com/API/cases/#replies-create
func (c *ReplyService) Create(caseId string, reply *Reply) (*Reply, *http.Response, error) {
restful := Restful{}
createdReply := NewReply()
replyPath := NewResourcePath(createdReply)
casePath := NewIdentityResourcePath(caseId, NewCase()).AppendPath(replyPath)
resp, err := restful.
Post(casePath.Path()).
Body(reply).
Json(createdReply).
Client(c.client).
Do()
return createdReply, resp, err
}
// Update a reply.
// See Desk API: http://dev.desk.com/API/replies/#update
func (c *ReplyService) Update(caseId string, reply *Reply) (*Reply, *http.Response, error) {
restful := Restful{}
updatedReply := NewReply()
casePath := NewIdentityResourcePath(caseId, NewCase()).SetNested(reply)
resp, err := restful.
Patch(casePath.Path()).
Body(reply).
Json(updatedReply).
Client(c.client).
Do()
return updatedReply, resp, err
}
// Delete a reply for a case.
// See Desk API: http://dev.desk.com/API/cases/#replies-show
func (c *ReplyService) Delete(caseId string, replyId string) (*http.Response, error) {
restful := Restful{}
casePath := NewIdentityResourcePath(caseId, NewCase()).SetAction("replies").SetSuffix(replyId)
resp, err := restful.
Delete(casePath.Path()).
Client(c.client).
Do()
return resp, err
}
func (c *ReplyService) unravelPage(page *Page) error {
replies := new([]Reply)
err := json.Unmarshal(*page.Embedded.RawEntries, &replies)
if err != nil {
return err
}
page.Embedded.Entries = make([]interface{}, len(*replies))
for i, v := range *replies {
v.InitializeResource(v)
page.Embedded.Entries[i] = interface{}(v)
}
page.Embedded.RawEntries = nil
return err
}