/
services_sessions_participants_message_interactions.go
241 lines (196 loc) · 7.69 KB
/
services_sessions_participants_message_interactions.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* This code was generated by
* ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __
* | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/
* | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \
*
* Twilio - Proxy
* This is the public Twilio REST API.
*
* NOTE: This class is auto generated by OpenAPI Generator.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package openapi
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/twilio/twilio-go/client"
)
// Optional parameters for the method 'CreateMessageInteraction'
type CreateMessageInteractionParams struct {
// The message to send to the participant
Body *string `json:"Body,omitempty"`
// Reserved. Not currently supported.
MediaUrl *[]string `json:"MediaUrl,omitempty"`
}
func (params *CreateMessageInteractionParams) SetBody(Body string) *CreateMessageInteractionParams {
params.Body = &Body
return params
}
func (params *CreateMessageInteractionParams) SetMediaUrl(MediaUrl []string) *CreateMessageInteractionParams {
params.MediaUrl = &MediaUrl
return params
}
// Create a new message Interaction to send directly from your system to one [Participant](https://www.twilio.com/docs/proxy/api/participant). The `inbound` properties for the Interaction will always be empty.
func (c *ApiService) CreateMessageInteraction(ServiceSid string, SessionSid string, ParticipantSid string, params *CreateMessageInteractionParams) (*ProxyV1MessageInteraction, error) {
path := "/v1/Services/{ServiceSid}/Sessions/{SessionSid}/Participants/{ParticipantSid}/MessageInteractions"
path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
path = strings.Replace(path, "{"+"SessionSid"+"}", SessionSid, -1)
path = strings.Replace(path, "{"+"ParticipantSid"+"}", ParticipantSid, -1)
data := url.Values{}
headers := make(map[string]interface{})
if params != nil && params.Body != nil {
data.Set("Body", *params.Body)
}
if params != nil && params.MediaUrl != nil {
for _, item := range *params.MediaUrl {
data.Add("MediaUrl", item)
}
}
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ProxyV1MessageInteraction{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}
//
func (c *ApiService) FetchMessageInteraction(ServiceSid string, SessionSid string, ParticipantSid string, Sid string) (*ProxyV1MessageInteraction, error) {
path := "/v1/Services/{ServiceSid}/Sessions/{SessionSid}/Participants/{ParticipantSid}/MessageInteractions/{Sid}"
path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
path = strings.Replace(path, "{"+"SessionSid"+"}", SessionSid, -1)
path = strings.Replace(path, "{"+"ParticipantSid"+"}", ParticipantSid, -1)
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
data := url.Values{}
headers := make(map[string]interface{})
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ProxyV1MessageInteraction{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}
// Optional parameters for the method 'ListMessageInteraction'
type ListMessageInteractionParams struct {
// How many resources to return in each list page. The default is 50, and the maximum is 1000.
PageSize *int `json:"PageSize,omitempty"`
// Max number of records to return.
Limit *int `json:"limit,omitempty"`
}
func (params *ListMessageInteractionParams) SetPageSize(PageSize int) *ListMessageInteractionParams {
params.PageSize = &PageSize
return params
}
func (params *ListMessageInteractionParams) SetLimit(Limit int) *ListMessageInteractionParams {
params.Limit = &Limit
return params
}
// Retrieve a single page of MessageInteraction records from the API. Request is executed immediately.
func (c *ApiService) PageMessageInteraction(ServiceSid string, SessionSid string, ParticipantSid string, params *ListMessageInteractionParams, pageToken, pageNumber string) (*ListMessageInteractionResponse, error) {
path := "/v1/Services/{ServiceSid}/Sessions/{SessionSid}/Participants/{ParticipantSid}/MessageInteractions"
path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
path = strings.Replace(path, "{"+"SessionSid"+"}", SessionSid, -1)
path = strings.Replace(path, "{"+"ParticipantSid"+"}", ParticipantSid, -1)
data := url.Values{}
headers := make(map[string]interface{})
if params != nil && params.PageSize != nil {
data.Set("PageSize", fmt.Sprint(*params.PageSize))
}
if pageToken != "" {
data.Set("PageToken", pageToken)
}
if pageNumber != "" {
data.Set("Page", pageNumber)
}
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ListMessageInteractionResponse{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}
// Lists MessageInteraction records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
func (c *ApiService) ListMessageInteraction(ServiceSid string, SessionSid string, ParticipantSid string, params *ListMessageInteractionParams) ([]ProxyV1MessageInteraction, error) {
response, errors := c.StreamMessageInteraction(ServiceSid, SessionSid, ParticipantSid, params)
records := make([]ProxyV1MessageInteraction, 0)
for record := range response {
records = append(records, record)
}
if err := <-errors; err != nil {
return nil, err
}
return records, nil
}
// Streams MessageInteraction records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
func (c *ApiService) StreamMessageInteraction(ServiceSid string, SessionSid string, ParticipantSid string, params *ListMessageInteractionParams) (chan ProxyV1MessageInteraction, chan error) {
if params == nil {
params = &ListMessageInteractionParams{}
}
params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
recordChannel := make(chan ProxyV1MessageInteraction, 1)
errorChannel := make(chan error, 1)
response, err := c.PageMessageInteraction(ServiceSid, SessionSid, ParticipantSid, params, "", "")
if err != nil {
errorChannel <- err
close(recordChannel)
close(errorChannel)
} else {
go c.streamMessageInteraction(response, params, recordChannel, errorChannel)
}
return recordChannel, errorChannel
}
func (c *ApiService) streamMessageInteraction(response *ListMessageInteractionResponse, params *ListMessageInteractionParams, recordChannel chan ProxyV1MessageInteraction, errorChannel chan error) {
curRecord := 1
for response != nil {
responseRecords := response.Interactions
for item := range responseRecords {
recordChannel <- responseRecords[item]
curRecord += 1
if params.Limit != nil && *params.Limit < curRecord {
close(recordChannel)
close(errorChannel)
return
}
}
record, err := client.GetNext(c.baseURL, response, c.getNextListMessageInteractionResponse)
if err != nil {
errorChannel <- err
break
} else if record == nil {
break
}
response = record.(*ListMessageInteractionResponse)
}
close(recordChannel)
close(errorChannel)
}
func (c *ApiService) getNextListMessageInteractionResponse(nextPageUrl string) (interface{}, error) {
if nextPageUrl == "" {
return nil, nil
}
resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ListMessageInteractionResponse{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, nil
}