-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_reply.go
133 lines (103 loc) · 3.76 KB
/
service_reply.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package pss
import (
"fmt"
"io"
"github.com/ukfast/sdk-go/pkg/connection"
)
// GetReply retrieves a single reply by id
func (s *Service) GetReply(replyID string) (Reply, error) {
body, err := s.getReplyResponseBody(replyID)
return body.Data, err
}
func (s *Service) getReplyResponseBody(replyID string) (*connection.APIResponseBodyData[Reply], error) {
body := &connection.APIResponseBodyData[Reply]{}
if replyID == "" {
return body, fmt.Errorf("invalid reply id")
}
response, err := s.connection.Get(fmt.Sprintf("/pss/v1/replies/%s", replyID), connection.APIRequestParameters{})
if err != nil {
return body, err
}
return body, response.HandleResponse(body, func(resp *connection.APIResponse) error {
if response.StatusCode == 404 {
return &ReplyNotFoundError{ID: replyID}
}
return nil
})
}
// DownloadReplyAttachmentStream downloads the provided attachment, returning
// a stream of the file contents and an error
func (s *Service) DownloadReplyAttachmentStream(replyID string, attachmentName string) (contentStream io.ReadCloser, err error) {
response, err := s.downloadReplyAttachmentResponse(replyID, attachmentName)
if err != nil {
return nil, err
}
return response.Body, nil
}
func (s *Service) downloadReplyAttachmentResponse(replyID string, attachmentName string) (*connection.APIResponse, error) {
body := &connection.APIResponseBody{}
response := &connection.APIResponse{}
if replyID == "" {
return response, fmt.Errorf("invalid reply id")
}
if attachmentName == "" {
return response, fmt.Errorf("invalid attachment name")
}
response, err := s.connection.Get(fmt.Sprintf("/pss/v1/replies/%s/attachments/%s", replyID, attachmentName), connection.APIRequestParameters{})
if err != nil {
return response, err
}
if response.StatusCode == 404 {
return response, &AttachmentNotFoundError{Name: attachmentName}
}
return response, response.ValidateStatusCode([]int{}, body)
}
// UploadReplyAttachmentStream uploads the provided attachment
func (s *Service) UploadReplyAttachmentStream(replyID string, attachmentName string, stream io.Reader) (err error) {
_, err = s.uploadReplyAttachmentStreamResponseBody(replyID, attachmentName, stream)
return err
}
func (s *Service) uploadReplyAttachmentStreamResponseBody(replyID string, attachmentName string, stream io.Reader) (*connection.APIResponseBody, error) {
body := &connection.APIResponseBody{}
if replyID == "" {
return body, fmt.Errorf("invalid reply id")
}
if attachmentName == "" {
return body, fmt.Errorf("invalid attachment name")
}
if stream == nil {
return body, fmt.Errorf("invalid stream")
}
response, err := s.connection.Post(fmt.Sprintf("/pss/v1/replies/%s/attachments/%s", replyID, attachmentName), stream)
if err != nil {
return body, err
}
if response.StatusCode == 404 {
return body, &ReplyNotFoundError{ID: replyID}
}
return body, response.HandleResponse(body, nil)
}
// DeleteReplyAttachment removes a reply attachment
func (s *Service) DeleteReplyAttachment(replyID string, attachmentName string) error {
_, err := s.deleteReplyAttachmentResponseBody(replyID, attachmentName)
return err
}
func (s *Service) deleteReplyAttachmentResponseBody(replyID string, attachmentName string) (*connection.APIResponseBody, error) {
body := &connection.APIResponseBody{}
if replyID == "" {
return body, fmt.Errorf("invalid reply id")
}
if attachmentName == "" {
return body, fmt.Errorf("invalid attachment name")
}
response, err := s.connection.Delete(fmt.Sprintf("/pss/v1/replies/%s/attachments/%s", replyID, attachmentName), nil)
if err != nil {
return body, err
}
return body, response.HandleResponse(body, func(resp *connection.APIResponse) error {
if response.StatusCode == 404 {
return &AttachmentNotFoundError{Name: attachmentName}
}
return nil
})
}