github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/services_users_conversations.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Conversations 8 * This is the public Twilio REST API. 9 * 10 * NOTE: This class is auto generated by OpenAPI Generator. 11 * https://openapi-generator.tech 12 * Do not edit the class manually. 13 */ 14 15 package openapi 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "net/url" 21 "strings" 22 "time" 23 24 "github.com/twilio/twilio-go/client" 25 ) 26 27 // Delete a specific User Conversation. 28 func (c *ApiService) DeleteServiceUserConversation(ChatServiceSid string, UserSid string, ConversationSid string) error { 29 path := "/v1/Services/{ChatServiceSid}/Users/{UserSid}/Conversations/{ConversationSid}" 30 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 31 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 32 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 33 34 data := url.Values{} 35 headers := make(map[string]interface{}) 36 37 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 38 if err != nil { 39 return err 40 } 41 42 defer resp.Body.Close() 43 44 return nil 45 } 46 47 // Fetch a specific User Conversation. 48 func (c *ApiService) FetchServiceUserConversation(ChatServiceSid string, UserSid string, ConversationSid string) (*ConversationsV1ServiceUserConversation, error) { 49 path := "/v1/Services/{ChatServiceSid}/Users/{UserSid}/Conversations/{ConversationSid}" 50 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 51 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 52 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 53 54 data := url.Values{} 55 headers := make(map[string]interface{}) 56 57 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 58 if err != nil { 59 return nil, err 60 } 61 62 defer resp.Body.Close() 63 64 ps := &ConversationsV1ServiceUserConversation{} 65 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 66 return nil, err 67 } 68 69 return ps, err 70 } 71 72 // Optional parameters for the method 'ListServiceUserConversation' 73 type ListServiceUserConversationParams struct { 74 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 75 PageSize *int `json:"PageSize,omitempty"` 76 // Max number of records to return. 77 Limit *int `json:"limit,omitempty"` 78 } 79 80 func (params *ListServiceUserConversationParams) SetPageSize(PageSize int) *ListServiceUserConversationParams { 81 params.PageSize = &PageSize 82 return params 83 } 84 func (params *ListServiceUserConversationParams) SetLimit(Limit int) *ListServiceUserConversationParams { 85 params.Limit = &Limit 86 return params 87 } 88 89 // Retrieve a single page of ServiceUserConversation records from the API. Request is executed immediately. 90 func (c *ApiService) PageServiceUserConversation(ChatServiceSid string, UserSid string, params *ListServiceUserConversationParams, pageToken, pageNumber string) (*ListServiceUserConversationResponse, error) { 91 path := "/v1/Services/{ChatServiceSid}/Users/{UserSid}/Conversations" 92 93 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 94 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 95 96 data := url.Values{} 97 headers := make(map[string]interface{}) 98 99 if params != nil && params.PageSize != nil { 100 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 101 } 102 103 if pageToken != "" { 104 data.Set("PageToken", pageToken) 105 } 106 if pageNumber != "" { 107 data.Set("Page", pageNumber) 108 } 109 110 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 111 if err != nil { 112 return nil, err 113 } 114 115 defer resp.Body.Close() 116 117 ps := &ListServiceUserConversationResponse{} 118 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 119 return nil, err 120 } 121 122 return ps, err 123 } 124 125 // Lists ServiceUserConversation records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 126 func (c *ApiService) ListServiceUserConversation(ChatServiceSid string, UserSid string, params *ListServiceUserConversationParams) ([]ConversationsV1ServiceUserConversation, error) { 127 response, errors := c.StreamServiceUserConversation(ChatServiceSid, UserSid, params) 128 129 records := make([]ConversationsV1ServiceUserConversation, 0) 130 for record := range response { 131 records = append(records, record) 132 } 133 134 if err := <-errors; err != nil { 135 return nil, err 136 } 137 138 return records, nil 139 } 140 141 // Streams ServiceUserConversation records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 142 func (c *ApiService) StreamServiceUserConversation(ChatServiceSid string, UserSid string, params *ListServiceUserConversationParams) (chan ConversationsV1ServiceUserConversation, chan error) { 143 if params == nil { 144 params = &ListServiceUserConversationParams{} 145 } 146 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 147 148 recordChannel := make(chan ConversationsV1ServiceUserConversation, 1) 149 errorChannel := make(chan error, 1) 150 151 response, err := c.PageServiceUserConversation(ChatServiceSid, UserSid, params, "", "") 152 if err != nil { 153 errorChannel <- err 154 close(recordChannel) 155 close(errorChannel) 156 } else { 157 go c.streamServiceUserConversation(response, params, recordChannel, errorChannel) 158 } 159 160 return recordChannel, errorChannel 161 } 162 163 func (c *ApiService) streamServiceUserConversation(response *ListServiceUserConversationResponse, params *ListServiceUserConversationParams, recordChannel chan ConversationsV1ServiceUserConversation, errorChannel chan error) { 164 curRecord := 1 165 166 for response != nil { 167 responseRecords := response.Conversations 168 for item := range responseRecords { 169 recordChannel <- responseRecords[item] 170 curRecord += 1 171 if params.Limit != nil && *params.Limit < curRecord { 172 close(recordChannel) 173 close(errorChannel) 174 return 175 } 176 } 177 178 record, err := client.GetNext(c.baseURL, response, c.getNextListServiceUserConversationResponse) 179 if err != nil { 180 errorChannel <- err 181 break 182 } else if record == nil { 183 break 184 } 185 186 response = record.(*ListServiceUserConversationResponse) 187 } 188 189 close(recordChannel) 190 close(errorChannel) 191 } 192 193 func (c *ApiService) getNextListServiceUserConversationResponse(nextPageUrl string) (interface{}, error) { 194 if nextPageUrl == "" { 195 return nil, nil 196 } 197 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 198 if err != nil { 199 return nil, err 200 } 201 202 defer resp.Body.Close() 203 204 ps := &ListServiceUserConversationResponse{} 205 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 206 return nil, err 207 } 208 return ps, nil 209 } 210 211 // Optional parameters for the method 'UpdateServiceUserConversation' 212 type UpdateServiceUserConversationParams struct { 213 // 214 NotificationLevel *string `json:"NotificationLevel,omitempty"` 215 // The date of the last message read in conversation by the user, given in ISO 8601 format. 216 LastReadTimestamp *time.Time `json:"LastReadTimestamp,omitempty"` 217 // The index of the last Message in the Conversation that the Participant has read. 218 LastReadMessageIndex *int `json:"LastReadMessageIndex,omitempty"` 219 } 220 221 func (params *UpdateServiceUserConversationParams) SetNotificationLevel(NotificationLevel string) *UpdateServiceUserConversationParams { 222 params.NotificationLevel = &NotificationLevel 223 return params 224 } 225 func (params *UpdateServiceUserConversationParams) SetLastReadTimestamp(LastReadTimestamp time.Time) *UpdateServiceUserConversationParams { 226 params.LastReadTimestamp = &LastReadTimestamp 227 return params 228 } 229 func (params *UpdateServiceUserConversationParams) SetLastReadMessageIndex(LastReadMessageIndex int) *UpdateServiceUserConversationParams { 230 params.LastReadMessageIndex = &LastReadMessageIndex 231 return params 232 } 233 234 // Update a specific User Conversation. 235 func (c *ApiService) UpdateServiceUserConversation(ChatServiceSid string, UserSid string, ConversationSid string, params *UpdateServiceUserConversationParams) (*ConversationsV1ServiceUserConversation, error) { 236 path := "/v1/Services/{ChatServiceSid}/Users/{UserSid}/Conversations/{ConversationSid}" 237 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 238 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 239 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 240 241 data := url.Values{} 242 headers := make(map[string]interface{}) 243 244 if params != nil && params.NotificationLevel != nil { 245 data.Set("NotificationLevel", *params.NotificationLevel) 246 } 247 if params != nil && params.LastReadTimestamp != nil { 248 data.Set("LastReadTimestamp", fmt.Sprint((*params.LastReadTimestamp).Format(time.RFC3339))) 249 } 250 if params != nil && params.LastReadMessageIndex != nil { 251 data.Set("LastReadMessageIndex", fmt.Sprint(*params.LastReadMessageIndex)) 252 } 253 254 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 255 if err != nil { 256 return nil, err 257 } 258 259 defer resp.Body.Close() 260 261 ps := &ConversationsV1ServiceUserConversation{} 262 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 263 return nil, err 264 } 265 266 return ps, err 267 }