github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/interactions_channels_invites.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Flex 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 'CreateInteractionChannelInvite' 27 type CreateInteractionChannelInviteParams struct { 28 // The Interaction's routing logic. 29 Routing *interface{} `json:"Routing,omitempty"` 30 } 31 32 func (params *CreateInteractionChannelInviteParams) SetRouting(Routing interface{}) *CreateInteractionChannelInviteParams { 33 params.Routing = &Routing 34 return params 35 } 36 37 // Invite an Agent or a TaskQueue to a Channel. 38 func (c *ApiService) CreateInteractionChannelInvite(InteractionSid string, ChannelSid string, params *CreateInteractionChannelInviteParams) (*FlexV1InteractionChannelInvite, error) { 39 path := "/v1/Interactions/{InteractionSid}/Channels/{ChannelSid}/Invites" 40 path = strings.Replace(path, "{"+"InteractionSid"+"}", InteractionSid, -1) 41 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 42 43 data := url.Values{} 44 headers := make(map[string]interface{}) 45 46 if params != nil && params.Routing != nil { 47 v, err := json.Marshal(params.Routing) 48 49 if err != nil { 50 return nil, err 51 } 52 53 data.Set("Routing", string(v)) 54 } 55 56 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 57 if err != nil { 58 return nil, err 59 } 60 61 defer resp.Body.Close() 62 63 ps := &FlexV1InteractionChannelInvite{} 64 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 65 return nil, err 66 } 67 68 return ps, err 69 } 70 71 // Optional parameters for the method 'ListInteractionChannelInvite' 72 type ListInteractionChannelInviteParams struct { 73 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 74 PageSize *int `json:"PageSize,omitempty"` 75 // Max number of records to return. 76 Limit *int `json:"limit,omitempty"` 77 } 78 79 func (params *ListInteractionChannelInviteParams) SetPageSize(PageSize int) *ListInteractionChannelInviteParams { 80 params.PageSize = &PageSize 81 return params 82 } 83 func (params *ListInteractionChannelInviteParams) SetLimit(Limit int) *ListInteractionChannelInviteParams { 84 params.Limit = &Limit 85 return params 86 } 87 88 // Retrieve a single page of InteractionChannelInvite records from the API. Request is executed immediately. 89 func (c *ApiService) PageInteractionChannelInvite(InteractionSid string, ChannelSid string, params *ListInteractionChannelInviteParams, pageToken, pageNumber string) (*ListInteractionChannelInviteResponse, error) { 90 path := "/v1/Interactions/{InteractionSid}/Channels/{ChannelSid}/Invites" 91 92 path = strings.Replace(path, "{"+"InteractionSid"+"}", InteractionSid, -1) 93 path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1) 94 95 data := url.Values{} 96 headers := make(map[string]interface{}) 97 98 if params != nil && params.PageSize != nil { 99 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 100 } 101 102 if pageToken != "" { 103 data.Set("PageToken", pageToken) 104 } 105 if pageNumber != "" { 106 data.Set("Page", pageNumber) 107 } 108 109 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 110 if err != nil { 111 return nil, err 112 } 113 114 defer resp.Body.Close() 115 116 ps := &ListInteractionChannelInviteResponse{} 117 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 118 return nil, err 119 } 120 121 return ps, err 122 } 123 124 // Lists InteractionChannelInvite records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 125 func (c *ApiService) ListInteractionChannelInvite(InteractionSid string, ChannelSid string, params *ListInteractionChannelInviteParams) ([]FlexV1InteractionChannelInvite, error) { 126 response, errors := c.StreamInteractionChannelInvite(InteractionSid, ChannelSid, params) 127 128 records := make([]FlexV1InteractionChannelInvite, 0) 129 for record := range response { 130 records = append(records, record) 131 } 132 133 if err := <-errors; err != nil { 134 return nil, err 135 } 136 137 return records, nil 138 } 139 140 // Streams InteractionChannelInvite records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 141 func (c *ApiService) StreamInteractionChannelInvite(InteractionSid string, ChannelSid string, params *ListInteractionChannelInviteParams) (chan FlexV1InteractionChannelInvite, chan error) { 142 if params == nil { 143 params = &ListInteractionChannelInviteParams{} 144 } 145 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 146 147 recordChannel := make(chan FlexV1InteractionChannelInvite, 1) 148 errorChannel := make(chan error, 1) 149 150 response, err := c.PageInteractionChannelInvite(InteractionSid, ChannelSid, params, "", "") 151 if err != nil { 152 errorChannel <- err 153 close(recordChannel) 154 close(errorChannel) 155 } else { 156 go c.streamInteractionChannelInvite(response, params, recordChannel, errorChannel) 157 } 158 159 return recordChannel, errorChannel 160 } 161 162 func (c *ApiService) streamInteractionChannelInvite(response *ListInteractionChannelInviteResponse, params *ListInteractionChannelInviteParams, recordChannel chan FlexV1InteractionChannelInvite, errorChannel chan error) { 163 curRecord := 1 164 165 for response != nil { 166 responseRecords := response.Invites 167 for item := range responseRecords { 168 recordChannel <- responseRecords[item] 169 curRecord += 1 170 if params.Limit != nil && *params.Limit < curRecord { 171 close(recordChannel) 172 close(errorChannel) 173 return 174 } 175 } 176 177 record, err := client.GetNext(c.baseURL, response, c.getNextListInteractionChannelInviteResponse) 178 if err != nil { 179 errorChannel <- err 180 break 181 } else if record == nil { 182 break 183 } 184 185 response = record.(*ListInteractionChannelInviteResponse) 186 } 187 188 close(recordChannel) 189 close(errorChannel) 190 } 191 192 func (c *ApiService) getNextListInteractionChannelInviteResponse(nextPageUrl string) (interface{}, error) { 193 if nextPageUrl == "" { 194 return nil, nil 195 } 196 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 197 if err != nil { 198 return nil, err 199 } 200 201 defer resp.Body.Close() 202 203 ps := &ListInteractionChannelInviteResponse{} 204 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 205 return nil, err 206 } 207 return ps, nil 208 }