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