-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedback.go
More file actions
141 lines (122 loc) · 3.55 KB
/
feedback.go
File metadata and controls
141 lines (122 loc) · 3.55 KB
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
134
135
136
137
138
139
140
141
// Copyright 2017-2026 Allow2 Pty Ltd. All rights reserved.
// Use of this source code is governed by the Allow2 API and SDK Licence.
package allow2service
import (
"fmt"
"net/url"
)
// feedbackManager manages feedback submission and discussion threads via the Allow2 API.
type feedbackManager struct {
httpClient HTTPClient
apiHost string
}
// Submit submits new feedback. Returns the discussion ID.
func (m *feedbackManager) Submit(accessToken, userID string, category FeedbackCategory, message string) (string, error) {
response, err := m.httpClient.Post(
m.apiHost+"/feedback/submit",
map[string]interface{}{
"access_token": accessToken,
"category": category.Value(),
"message": message,
},
nil,
)
if err != nil {
return "", &ApiError{
Allow2Error: Allow2Error{Message: fmt.Sprintf("Failed to submit feedback: %v", err)},
}
}
if response.IsUnauthorized() {
return "", &UnpairedError{
Allow2Error: Allow2Error{Message: "Service account is no longer linked. Re-pairing required."},
UserID: userID,
}
}
if !response.IsSuccess() {
body := response.JSON()
return "", &ApiError{
Allow2Error: Allow2Error{Message: fmt.Sprintf("Failed to submit feedback: HTTP %d", response.StatusCode)},
HTTPStatusCode: response.StatusCode,
ResponseBody: body,
}
}
data := response.JSON()
if data == nil {
return "", nil
}
for _, key := range []string{"discussionId", "feedbackId", "id"} {
if v, ok := data[key]; ok {
return fmt.Sprintf("%v", v), nil
}
}
return "", nil
}
// Load loads all feedback discussions for the authenticated user.
func (m *feedbackManager) Load(accessToken, userID string) ([]interface{}, error) {
response, err := m.httpClient.Get(
m.apiHost+"/feedback/load",
map[string]string{"Authorization": "Bearer " + accessToken},
)
if err != nil {
return nil, &ApiError{
Allow2Error: Allow2Error{Message: fmt.Sprintf("Failed to load feedback: %v", err)},
}
}
if response.IsUnauthorized() {
return nil, &UnpairedError{
Allow2Error: Allow2Error{Message: "Service account is no longer linked. Re-pairing required."},
UserID: userID,
}
}
if !response.IsSuccess() {
body := response.JSON()
return nil, &ApiError{
Allow2Error: Allow2Error{Message: fmt.Sprintf("Failed to load feedback: HTTP %d", response.StatusCode)},
HTTPStatusCode: response.StatusCode,
ResponseBody: body,
}
}
data := response.JSON()
if data == nil {
return nil, nil
}
for _, key := range []string{"discussions", "feedback"} {
if v, ok := data[key]; ok {
if arr, ok := v.([]interface{}); ok {
return arr, nil
}
}
}
return nil, nil
}
// Reply replies to an existing feedback discussion thread.
func (m *feedbackManager) Reply(accessToken, userID, discussionID, message string) error {
response, err := m.httpClient.Post(
m.apiHost+"/feedback/"+url.PathEscape(discussionID)+"/reply",
map[string]interface{}{
"access_token": accessToken,
"message": message,
},
nil,
)
if err != nil {
return &ApiError{
Allow2Error: Allow2Error{Message: fmt.Sprintf("Failed to reply to feedback: %v", err)},
}
}
if response.IsUnauthorized() {
return &UnpairedError{
Allow2Error: Allow2Error{Message: "Service account is no longer linked. Re-pairing required."},
UserID: userID,
}
}
if !response.IsSuccess() {
body := response.JSON()
return &ApiError{
Allow2Error: Allow2Error{Message: fmt.Sprintf("Failed to reply to feedback: HTTP %d", response.StatusCode)},
HTTPStatusCode: response.StatusCode,
ResponseBody: body,
}
}
return nil
}