github.com/twilio/twilio-go@v1.20.1/rest/chat/v2/services_bindings.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Chat 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 // 27 func (c *ApiService) DeleteBinding(ServiceSid string, Sid string) error { 28 path := "/v2/Services/{ServiceSid}/Bindings/{Sid}" 29 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 30 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 31 32 data := url.Values{} 33 headers := make(map[string]interface{}) 34 35 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 36 if err != nil { 37 return err 38 } 39 40 defer resp.Body.Close() 41 42 return nil 43 } 44 45 // 46 func (c *ApiService) FetchBinding(ServiceSid string, Sid string) (*ChatV2Binding, error) { 47 path := "/v2/Services/{ServiceSid}/Bindings/{Sid}" 48 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 49 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 50 51 data := url.Values{} 52 headers := make(map[string]interface{}) 53 54 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 55 if err != nil { 56 return nil, err 57 } 58 59 defer resp.Body.Close() 60 61 ps := &ChatV2Binding{} 62 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 63 return nil, err 64 } 65 66 return ps, err 67 } 68 69 // Optional parameters for the method 'ListBinding' 70 type ListBindingParams struct { 71 // The push technology used by the Binding resources to read. Can be: `apn`, `gcm`, or `fcm`. See [push notification configuration](https://www.twilio.com/docs/chat/push-notification-configuration) for more info. 72 BindingType *[]string `json:"BindingType,omitempty"` 73 // The [User](https://www.twilio.com/docs/chat/rest/user-resource)'s `identity` value of the resources to read. See [access tokens](https://www.twilio.com/docs/chat/create-tokens) for more details. 74 Identity *[]string `json:"Identity,omitempty"` 75 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 76 PageSize *int `json:"PageSize,omitempty"` 77 // Max number of records to return. 78 Limit *int `json:"limit,omitempty"` 79 } 80 81 func (params *ListBindingParams) SetBindingType(BindingType []string) *ListBindingParams { 82 params.BindingType = &BindingType 83 return params 84 } 85 func (params *ListBindingParams) SetIdentity(Identity []string) *ListBindingParams { 86 params.Identity = &Identity 87 return params 88 } 89 func (params *ListBindingParams) SetPageSize(PageSize int) *ListBindingParams { 90 params.PageSize = &PageSize 91 return params 92 } 93 func (params *ListBindingParams) SetLimit(Limit int) *ListBindingParams { 94 params.Limit = &Limit 95 return params 96 } 97 98 // Retrieve a single page of Binding records from the API. Request is executed immediately. 99 func (c *ApiService) PageBinding(ServiceSid string, params *ListBindingParams, pageToken, pageNumber string) (*ListBindingResponse, error) { 100 path := "/v2/Services/{ServiceSid}/Bindings" 101 102 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 103 104 data := url.Values{} 105 headers := make(map[string]interface{}) 106 107 if params != nil && params.BindingType != nil { 108 for _, item := range *params.BindingType { 109 data.Add("BindingType", item) 110 } 111 } 112 if params != nil && params.Identity != nil { 113 for _, item := range *params.Identity { 114 data.Add("Identity", item) 115 } 116 } 117 if params != nil && params.PageSize != nil { 118 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 119 } 120 121 if pageToken != "" { 122 data.Set("PageToken", pageToken) 123 } 124 if pageNumber != "" { 125 data.Set("Page", pageNumber) 126 } 127 128 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 129 if err != nil { 130 return nil, err 131 } 132 133 defer resp.Body.Close() 134 135 ps := &ListBindingResponse{} 136 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 137 return nil, err 138 } 139 140 return ps, err 141 } 142 143 // Lists Binding records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 144 func (c *ApiService) ListBinding(ServiceSid string, params *ListBindingParams) ([]ChatV2Binding, error) { 145 response, errors := c.StreamBinding(ServiceSid, params) 146 147 records := make([]ChatV2Binding, 0) 148 for record := range response { 149 records = append(records, record) 150 } 151 152 if err := <-errors; err != nil { 153 return nil, err 154 } 155 156 return records, nil 157 } 158 159 // Streams Binding records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 160 func (c *ApiService) StreamBinding(ServiceSid string, params *ListBindingParams) (chan ChatV2Binding, chan error) { 161 if params == nil { 162 params = &ListBindingParams{} 163 } 164 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 165 166 recordChannel := make(chan ChatV2Binding, 1) 167 errorChannel := make(chan error, 1) 168 169 response, err := c.PageBinding(ServiceSid, params, "", "") 170 if err != nil { 171 errorChannel <- err 172 close(recordChannel) 173 close(errorChannel) 174 } else { 175 go c.streamBinding(response, params, recordChannel, errorChannel) 176 } 177 178 return recordChannel, errorChannel 179 } 180 181 func (c *ApiService) streamBinding(response *ListBindingResponse, params *ListBindingParams, recordChannel chan ChatV2Binding, errorChannel chan error) { 182 curRecord := 1 183 184 for response != nil { 185 responseRecords := response.Bindings 186 for item := range responseRecords { 187 recordChannel <- responseRecords[item] 188 curRecord += 1 189 if params.Limit != nil && *params.Limit < curRecord { 190 close(recordChannel) 191 close(errorChannel) 192 return 193 } 194 } 195 196 record, err := client.GetNext(c.baseURL, response, c.getNextListBindingResponse) 197 if err != nil { 198 errorChannel <- err 199 break 200 } else if record == nil { 201 break 202 } 203 204 response = record.(*ListBindingResponse) 205 } 206 207 close(recordChannel) 208 close(errorChannel) 209 } 210 211 func (c *ApiService) getNextListBindingResponse(nextPageUrl string) (interface{}, error) { 212 if nextPageUrl == "" { 213 return nil, nil 214 } 215 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 216 if err != nil { 217 return nil, err 218 } 219 220 defer resp.Body.Close() 221 222 ps := &ListBindingResponse{} 223 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 224 return nil, err 225 } 226 return ps, nil 227 }