github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/conversations_messages_receipts.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 23 "github.com/twilio/twilio-go/client" 24 ) 25 26 // Fetch the delivery and read receipts of the conversation message 27 func (c *ApiService) FetchConversationMessageReceipt(ConversationSid string, MessageSid string, Sid string) (*ConversationsV1ConversationMessageReceipt, error) { 28 path := "/v1/Conversations/{ConversationSid}/Messages/{MessageSid}/Receipts/{Sid}" 29 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 30 path = strings.Replace(path, "{"+"MessageSid"+"}", MessageSid, -1) 31 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 32 33 data := url.Values{} 34 headers := make(map[string]interface{}) 35 36 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 37 if err != nil { 38 return nil, err 39 } 40 41 defer resp.Body.Close() 42 43 ps := &ConversationsV1ConversationMessageReceipt{} 44 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 45 return nil, err 46 } 47 48 return ps, err 49 } 50 51 // Optional parameters for the method 'ListConversationMessageReceipt' 52 type ListConversationMessageReceiptParams struct { 53 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 54 PageSize *int `json:"PageSize,omitempty"` 55 // Max number of records to return. 56 Limit *int `json:"limit,omitempty"` 57 } 58 59 func (params *ListConversationMessageReceiptParams) SetPageSize(PageSize int) *ListConversationMessageReceiptParams { 60 params.PageSize = &PageSize 61 return params 62 } 63 func (params *ListConversationMessageReceiptParams) SetLimit(Limit int) *ListConversationMessageReceiptParams { 64 params.Limit = &Limit 65 return params 66 } 67 68 // Retrieve a single page of ConversationMessageReceipt records from the API. Request is executed immediately. 69 func (c *ApiService) PageConversationMessageReceipt(ConversationSid string, MessageSid string, params *ListConversationMessageReceiptParams, pageToken, pageNumber string) (*ListConversationMessageReceiptResponse, error) { 70 path := "/v1/Conversations/{ConversationSid}/Messages/{MessageSid}/Receipts" 71 72 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 73 path = strings.Replace(path, "{"+"MessageSid"+"}", MessageSid, -1) 74 75 data := url.Values{} 76 headers := make(map[string]interface{}) 77 78 if params != nil && params.PageSize != nil { 79 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 80 } 81 82 if pageToken != "" { 83 data.Set("PageToken", pageToken) 84 } 85 if pageNumber != "" { 86 data.Set("Page", pageNumber) 87 } 88 89 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 90 if err != nil { 91 return nil, err 92 } 93 94 defer resp.Body.Close() 95 96 ps := &ListConversationMessageReceiptResponse{} 97 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 98 return nil, err 99 } 100 101 return ps, err 102 } 103 104 // Lists ConversationMessageReceipt records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 105 func (c *ApiService) ListConversationMessageReceipt(ConversationSid string, MessageSid string, params *ListConversationMessageReceiptParams) ([]ConversationsV1ConversationMessageReceipt, error) { 106 response, errors := c.StreamConversationMessageReceipt(ConversationSid, MessageSid, params) 107 108 records := make([]ConversationsV1ConversationMessageReceipt, 0) 109 for record := range response { 110 records = append(records, record) 111 } 112 113 if err := <-errors; err != nil { 114 return nil, err 115 } 116 117 return records, nil 118 } 119 120 // Streams ConversationMessageReceipt records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 121 func (c *ApiService) StreamConversationMessageReceipt(ConversationSid string, MessageSid string, params *ListConversationMessageReceiptParams) (chan ConversationsV1ConversationMessageReceipt, chan error) { 122 if params == nil { 123 params = &ListConversationMessageReceiptParams{} 124 } 125 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 126 127 recordChannel := make(chan ConversationsV1ConversationMessageReceipt, 1) 128 errorChannel := make(chan error, 1) 129 130 response, err := c.PageConversationMessageReceipt(ConversationSid, MessageSid, params, "", "") 131 if err != nil { 132 errorChannel <- err 133 close(recordChannel) 134 close(errorChannel) 135 } else { 136 go c.streamConversationMessageReceipt(response, params, recordChannel, errorChannel) 137 } 138 139 return recordChannel, errorChannel 140 } 141 142 func (c *ApiService) streamConversationMessageReceipt(response *ListConversationMessageReceiptResponse, params *ListConversationMessageReceiptParams, recordChannel chan ConversationsV1ConversationMessageReceipt, errorChannel chan error) { 143 curRecord := 1 144 145 for response != nil { 146 responseRecords := response.DeliveryReceipts 147 for item := range responseRecords { 148 recordChannel <- responseRecords[item] 149 curRecord += 1 150 if params.Limit != nil && *params.Limit < curRecord { 151 close(recordChannel) 152 close(errorChannel) 153 return 154 } 155 } 156 157 record, err := client.GetNext(c.baseURL, response, c.getNextListConversationMessageReceiptResponse) 158 if err != nil { 159 errorChannel <- err 160 break 161 } else if record == nil { 162 break 163 } 164 165 response = record.(*ListConversationMessageReceiptResponse) 166 } 167 168 close(recordChannel) 169 close(errorChannel) 170 } 171 172 func (c *ApiService) getNextListConversationMessageReceiptResponse(nextPageUrl string) (interface{}, error) { 173 if nextPageUrl == "" { 174 return nil, nil 175 } 176 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 177 if err != nil { 178 return nil, err 179 } 180 181 defer resp.Body.Close() 182 183 ps := &ListConversationMessageReceiptResponse{} 184 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 185 return nil, err 186 } 187 return ps, nil 188 }