github.com/twilio/twilio-go@v1.20.1/rest/sync/v1/services_maps.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Sync 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 'CreateSyncMap' 27 type CreateSyncMapParams struct { 28 // An application-defined string that uniquely identifies the resource. It can be used as an alternative to the `sid` in the URL path to address the resource. 29 UniqueName *string `json:"UniqueName,omitempty"` 30 // An alias for `collection_ttl`. If both parameters are provided, this value is ignored. 31 Ttl *int `json:"Ttl,omitempty"` 32 // How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the Sync Map expires (time-to-live) and is deleted. 33 CollectionTtl *int `json:"CollectionTtl,omitempty"` 34 } 35 36 func (params *CreateSyncMapParams) SetUniqueName(UniqueName string) *CreateSyncMapParams { 37 params.UniqueName = &UniqueName 38 return params 39 } 40 func (params *CreateSyncMapParams) SetTtl(Ttl int) *CreateSyncMapParams { 41 params.Ttl = &Ttl 42 return params 43 } 44 func (params *CreateSyncMapParams) SetCollectionTtl(CollectionTtl int) *CreateSyncMapParams { 45 params.CollectionTtl = &CollectionTtl 46 return params 47 } 48 49 // 50 func (c *ApiService) CreateSyncMap(ServiceSid string, params *CreateSyncMapParams) (*SyncV1SyncMap, error) { 51 path := "/v1/Services/{ServiceSid}/Maps" 52 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 53 54 data := url.Values{} 55 headers := make(map[string]interface{}) 56 57 if params != nil && params.UniqueName != nil { 58 data.Set("UniqueName", *params.UniqueName) 59 } 60 if params != nil && params.Ttl != nil { 61 data.Set("Ttl", fmt.Sprint(*params.Ttl)) 62 } 63 if params != nil && params.CollectionTtl != nil { 64 data.Set("CollectionTtl", fmt.Sprint(*params.CollectionTtl)) 65 } 66 67 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 68 if err != nil { 69 return nil, err 70 } 71 72 defer resp.Body.Close() 73 74 ps := &SyncV1SyncMap{} 75 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 76 return nil, err 77 } 78 79 return ps, err 80 } 81 82 // 83 func (c *ApiService) DeleteSyncMap(ServiceSid string, Sid string) error { 84 path := "/v1/Services/{ServiceSid}/Maps/{Sid}" 85 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 86 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 87 88 data := url.Values{} 89 headers := make(map[string]interface{}) 90 91 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 92 if err != nil { 93 return err 94 } 95 96 defer resp.Body.Close() 97 98 return nil 99 } 100 101 // 102 func (c *ApiService) FetchSyncMap(ServiceSid string, Sid string) (*SyncV1SyncMap, error) { 103 path := "/v1/Services/{ServiceSid}/Maps/{Sid}" 104 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 105 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 106 107 data := url.Values{} 108 headers := make(map[string]interface{}) 109 110 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 111 if err != nil { 112 return nil, err 113 } 114 115 defer resp.Body.Close() 116 117 ps := &SyncV1SyncMap{} 118 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 119 return nil, err 120 } 121 122 return ps, err 123 } 124 125 // Optional parameters for the method 'ListSyncMap' 126 type ListSyncMapParams struct { 127 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 128 PageSize *int `json:"PageSize,omitempty"` 129 // Max number of records to return. 130 Limit *int `json:"limit,omitempty"` 131 } 132 133 func (params *ListSyncMapParams) SetPageSize(PageSize int) *ListSyncMapParams { 134 params.PageSize = &PageSize 135 return params 136 } 137 func (params *ListSyncMapParams) SetLimit(Limit int) *ListSyncMapParams { 138 params.Limit = &Limit 139 return params 140 } 141 142 // Retrieve a single page of SyncMap records from the API. Request is executed immediately. 143 func (c *ApiService) PageSyncMap(ServiceSid string, params *ListSyncMapParams, pageToken, pageNumber string) (*ListSyncMapResponse, error) { 144 path := "/v1/Services/{ServiceSid}/Maps" 145 146 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 147 148 data := url.Values{} 149 headers := make(map[string]interface{}) 150 151 if params != nil && params.PageSize != nil { 152 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 153 } 154 155 if pageToken != "" { 156 data.Set("PageToken", pageToken) 157 } 158 if pageNumber != "" { 159 data.Set("Page", pageNumber) 160 } 161 162 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 163 if err != nil { 164 return nil, err 165 } 166 167 defer resp.Body.Close() 168 169 ps := &ListSyncMapResponse{} 170 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 171 return nil, err 172 } 173 174 return ps, err 175 } 176 177 // Lists SyncMap records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 178 func (c *ApiService) ListSyncMap(ServiceSid string, params *ListSyncMapParams) ([]SyncV1SyncMap, error) { 179 response, errors := c.StreamSyncMap(ServiceSid, params) 180 181 records := make([]SyncV1SyncMap, 0) 182 for record := range response { 183 records = append(records, record) 184 } 185 186 if err := <-errors; err != nil { 187 return nil, err 188 } 189 190 return records, nil 191 } 192 193 // Streams SyncMap records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 194 func (c *ApiService) StreamSyncMap(ServiceSid string, params *ListSyncMapParams) (chan SyncV1SyncMap, chan error) { 195 if params == nil { 196 params = &ListSyncMapParams{} 197 } 198 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 199 200 recordChannel := make(chan SyncV1SyncMap, 1) 201 errorChannel := make(chan error, 1) 202 203 response, err := c.PageSyncMap(ServiceSid, params, "", "") 204 if err != nil { 205 errorChannel <- err 206 close(recordChannel) 207 close(errorChannel) 208 } else { 209 go c.streamSyncMap(response, params, recordChannel, errorChannel) 210 } 211 212 return recordChannel, errorChannel 213 } 214 215 func (c *ApiService) streamSyncMap(response *ListSyncMapResponse, params *ListSyncMapParams, recordChannel chan SyncV1SyncMap, errorChannel chan error) { 216 curRecord := 1 217 218 for response != nil { 219 responseRecords := response.Maps 220 for item := range responseRecords { 221 recordChannel <- responseRecords[item] 222 curRecord += 1 223 if params.Limit != nil && *params.Limit < curRecord { 224 close(recordChannel) 225 close(errorChannel) 226 return 227 } 228 } 229 230 record, err := client.GetNext(c.baseURL, response, c.getNextListSyncMapResponse) 231 if err != nil { 232 errorChannel <- err 233 break 234 } else if record == nil { 235 break 236 } 237 238 response = record.(*ListSyncMapResponse) 239 } 240 241 close(recordChannel) 242 close(errorChannel) 243 } 244 245 func (c *ApiService) getNextListSyncMapResponse(nextPageUrl string) (interface{}, error) { 246 if nextPageUrl == "" { 247 return nil, nil 248 } 249 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 250 if err != nil { 251 return nil, err 252 } 253 254 defer resp.Body.Close() 255 256 ps := &ListSyncMapResponse{} 257 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 258 return nil, err 259 } 260 return ps, nil 261 } 262 263 // Optional parameters for the method 'UpdateSyncMap' 264 type UpdateSyncMapParams struct { 265 // An alias for `collection_ttl`. If both parameters are provided, this value is ignored. 266 Ttl *int `json:"Ttl,omitempty"` 267 // How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the Sync Map expires (time-to-live) and is deleted. 268 CollectionTtl *int `json:"CollectionTtl,omitempty"` 269 } 270 271 func (params *UpdateSyncMapParams) SetTtl(Ttl int) *UpdateSyncMapParams { 272 params.Ttl = &Ttl 273 return params 274 } 275 func (params *UpdateSyncMapParams) SetCollectionTtl(CollectionTtl int) *UpdateSyncMapParams { 276 params.CollectionTtl = &CollectionTtl 277 return params 278 } 279 280 // 281 func (c *ApiService) UpdateSyncMap(ServiceSid string, Sid string, params *UpdateSyncMapParams) (*SyncV1SyncMap, error) { 282 path := "/v1/Services/{ServiceSid}/Maps/{Sid}" 283 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 284 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 285 286 data := url.Values{} 287 headers := make(map[string]interface{}) 288 289 if params != nil && params.Ttl != nil { 290 data.Set("Ttl", fmt.Sprint(*params.Ttl)) 291 } 292 if params != nil && params.CollectionTtl != nil { 293 data.Set("CollectionTtl", fmt.Sprint(*params.CollectionTtl)) 294 } 295 296 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 297 if err != nil { 298 return nil, err 299 } 300 301 defer resp.Body.Close() 302 303 ps := &SyncV1SyncMap{} 304 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 305 return nil, err 306 } 307 308 return ps, err 309 }