github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/participant_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 22 "github.com/twilio/twilio-go/client" 23 ) 24 25 // Optional parameters for the method 'ListParticipantConversation' 26 type ListParticipantConversationParams struct { 27 // A unique string identifier for the conversation participant as [Conversation User](https://www.twilio.com/docs/conversations/api/user-resource). This parameter is non-null if (and only if) the participant is using the Conversations SDK to communicate. Limited to 256 characters. 28 Identity *string `json:"Identity,omitempty"` 29 // A unique string identifier for the conversation participant who's not a Conversation User. This parameter could be found in messaging_binding.address field of Participant resource. It should be url-encoded. 30 Address *string `json:"Address,omitempty"` 31 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 32 PageSize *int `json:"PageSize,omitempty"` 33 // Max number of records to return. 34 Limit *int `json:"limit,omitempty"` 35 } 36 37 func (params *ListParticipantConversationParams) SetIdentity(Identity string) *ListParticipantConversationParams { 38 params.Identity = &Identity 39 return params 40 } 41 func (params *ListParticipantConversationParams) SetAddress(Address string) *ListParticipantConversationParams { 42 params.Address = &Address 43 return params 44 } 45 func (params *ListParticipantConversationParams) SetPageSize(PageSize int) *ListParticipantConversationParams { 46 params.PageSize = &PageSize 47 return params 48 } 49 func (params *ListParticipantConversationParams) SetLimit(Limit int) *ListParticipantConversationParams { 50 params.Limit = &Limit 51 return params 52 } 53 54 // Retrieve a single page of ParticipantConversation records from the API. Request is executed immediately. 55 func (c *ApiService) PageParticipantConversation(params *ListParticipantConversationParams, pageToken, pageNumber string) (*ListParticipantConversationResponse, error) { 56 path := "/v1/ParticipantConversations" 57 58 data := url.Values{} 59 headers := make(map[string]interface{}) 60 61 if params != nil && params.Identity != nil { 62 data.Set("Identity", *params.Identity) 63 } 64 if params != nil && params.Address != nil { 65 data.Set("Address", *params.Address) 66 } 67 if params != nil && params.PageSize != nil { 68 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 69 } 70 71 if pageToken != "" { 72 data.Set("PageToken", pageToken) 73 } 74 if pageNumber != "" { 75 data.Set("Page", pageNumber) 76 } 77 78 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 79 if err != nil { 80 return nil, err 81 } 82 83 defer resp.Body.Close() 84 85 ps := &ListParticipantConversationResponse{} 86 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 87 return nil, err 88 } 89 90 return ps, err 91 } 92 93 // Lists ParticipantConversation records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 94 func (c *ApiService) ListParticipantConversation(params *ListParticipantConversationParams) ([]ConversationsV1ParticipantConversation, error) { 95 response, errors := c.StreamParticipantConversation(params) 96 97 records := make([]ConversationsV1ParticipantConversation, 0) 98 for record := range response { 99 records = append(records, record) 100 } 101 102 if err := <-errors; err != nil { 103 return nil, err 104 } 105 106 return records, nil 107 } 108 109 // Streams ParticipantConversation records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 110 func (c *ApiService) StreamParticipantConversation(params *ListParticipantConversationParams) (chan ConversationsV1ParticipantConversation, chan error) { 111 if params == nil { 112 params = &ListParticipantConversationParams{} 113 } 114 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 115 116 recordChannel := make(chan ConversationsV1ParticipantConversation, 1) 117 errorChannel := make(chan error, 1) 118 119 response, err := c.PageParticipantConversation(params, "", "") 120 if err != nil { 121 errorChannel <- err 122 close(recordChannel) 123 close(errorChannel) 124 } else { 125 go c.streamParticipantConversation(response, params, recordChannel, errorChannel) 126 } 127 128 return recordChannel, errorChannel 129 } 130 131 func (c *ApiService) streamParticipantConversation(response *ListParticipantConversationResponse, params *ListParticipantConversationParams, recordChannel chan ConversationsV1ParticipantConversation, errorChannel chan error) { 132 curRecord := 1 133 134 for response != nil { 135 responseRecords := response.Conversations 136 for item := range responseRecords { 137 recordChannel <- responseRecords[item] 138 curRecord += 1 139 if params.Limit != nil && *params.Limit < curRecord { 140 close(recordChannel) 141 close(errorChannel) 142 return 143 } 144 } 145 146 record, err := client.GetNext(c.baseURL, response, c.getNextListParticipantConversationResponse) 147 if err != nil { 148 errorChannel <- err 149 break 150 } else if record == nil { 151 break 152 } 153 154 response = record.(*ListParticipantConversationResponse) 155 } 156 157 close(recordChannel) 158 close(errorChannel) 159 } 160 161 func (c *ApiService) getNextListParticipantConversationResponse(nextPageUrl string) (interface{}, error) { 162 if nextPageUrl == "" { 163 return nil, nil 164 } 165 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 166 if err != nil { 167 return nil, err 168 } 169 170 defer resp.Body.Close() 171 172 ps := &ListParticipantConversationResponse{} 173 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 174 return nil, err 175 } 176 return ps, nil 177 }