github.com/twilio/twilio-go@v1.20.1/rest/sync/v1/services_maps_items.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 'CreateSyncMapItem' 27 type CreateSyncMapItemParams struct { 28 // The unique, user-defined key for the Map Item. Can be up to 320 characters long. 29 Key *string `json:"Key,omitempty"` 30 // A JSON string that represents an arbitrary, schema-less object that the Map Item stores. Can be up to 16 KiB in length. 31 Data *interface{} `json:"Data,omitempty"` 32 // An alias for `item_ttl`. If both parameters are provided, this value is ignored. 33 Ttl *int `json:"Ttl,omitempty"` 34 // How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the Map Item expires (time-to-live) and is deleted. 35 ItemTtl *int `json:"ItemTtl,omitempty"` 36 // How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the Map Item's parent Sync Map expires (time-to-live) and is deleted. 37 CollectionTtl *int `json:"CollectionTtl,omitempty"` 38 } 39 40 func (params *CreateSyncMapItemParams) SetKey(Key string) *CreateSyncMapItemParams { 41 params.Key = &Key 42 return params 43 } 44 func (params *CreateSyncMapItemParams) SetData(Data interface{}) *CreateSyncMapItemParams { 45 params.Data = &Data 46 return params 47 } 48 func (params *CreateSyncMapItemParams) SetTtl(Ttl int) *CreateSyncMapItemParams { 49 params.Ttl = &Ttl 50 return params 51 } 52 func (params *CreateSyncMapItemParams) SetItemTtl(ItemTtl int) *CreateSyncMapItemParams { 53 params.ItemTtl = &ItemTtl 54 return params 55 } 56 func (params *CreateSyncMapItemParams) SetCollectionTtl(CollectionTtl int) *CreateSyncMapItemParams { 57 params.CollectionTtl = &CollectionTtl 58 return params 59 } 60 61 // 62 func (c *ApiService) CreateSyncMapItem(ServiceSid string, MapSid string, params *CreateSyncMapItemParams) (*SyncV1SyncMapItem, error) { 63 path := "/v1/Services/{ServiceSid}/Maps/{MapSid}/Items" 64 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 65 path = strings.Replace(path, "{"+"MapSid"+"}", MapSid, -1) 66 67 data := url.Values{} 68 headers := make(map[string]interface{}) 69 70 if params != nil && params.Key != nil { 71 data.Set("Key", *params.Key) 72 } 73 if params != nil && params.Data != nil { 74 v, err := json.Marshal(params.Data) 75 76 if err != nil { 77 return nil, err 78 } 79 80 data.Set("Data", string(v)) 81 } 82 if params != nil && params.Ttl != nil { 83 data.Set("Ttl", fmt.Sprint(*params.Ttl)) 84 } 85 if params != nil && params.ItemTtl != nil { 86 data.Set("ItemTtl", fmt.Sprint(*params.ItemTtl)) 87 } 88 if params != nil && params.CollectionTtl != nil { 89 data.Set("CollectionTtl", fmt.Sprint(*params.CollectionTtl)) 90 } 91 92 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 93 if err != nil { 94 return nil, err 95 } 96 97 defer resp.Body.Close() 98 99 ps := &SyncV1SyncMapItem{} 100 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 101 return nil, err 102 } 103 104 return ps, err 105 } 106 107 // Optional parameters for the method 'DeleteSyncMapItem' 108 type DeleteSyncMapItemParams struct { 109 // If provided, applies this mutation if (and only if) the “revision” field of this [map item] matches the provided value. This matches the semantics of (and is implemented with) the HTTP [If-Match header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match). 110 IfMatch *string `json:"If-Match,omitempty"` 111 } 112 113 func (params *DeleteSyncMapItemParams) SetIfMatch(IfMatch string) *DeleteSyncMapItemParams { 114 params.IfMatch = &IfMatch 115 return params 116 } 117 118 // 119 func (c *ApiService) DeleteSyncMapItem(ServiceSid string, MapSid string, Key string, params *DeleteSyncMapItemParams) error { 120 path := "/v1/Services/{ServiceSid}/Maps/{MapSid}/Items/{Key}" 121 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 122 path = strings.Replace(path, "{"+"MapSid"+"}", MapSid, -1) 123 path = strings.Replace(path, "{"+"Key"+"}", Key, -1) 124 125 data := url.Values{} 126 headers := make(map[string]interface{}) 127 128 if params != nil && params.IfMatch != nil { 129 headers["If-Match"] = *params.IfMatch 130 } 131 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 132 if err != nil { 133 return err 134 } 135 136 defer resp.Body.Close() 137 138 return nil 139 } 140 141 // 142 func (c *ApiService) FetchSyncMapItem(ServiceSid string, MapSid string, Key string) (*SyncV1SyncMapItem, error) { 143 path := "/v1/Services/{ServiceSid}/Maps/{MapSid}/Items/{Key}" 144 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 145 path = strings.Replace(path, "{"+"MapSid"+"}", MapSid, -1) 146 path = strings.Replace(path, "{"+"Key"+"}", Key, -1) 147 148 data := url.Values{} 149 headers := make(map[string]interface{}) 150 151 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 152 if err != nil { 153 return nil, err 154 } 155 156 defer resp.Body.Close() 157 158 ps := &SyncV1SyncMapItem{} 159 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 160 return nil, err 161 } 162 163 return ps, err 164 } 165 166 // Optional parameters for the method 'ListSyncMapItem' 167 type ListSyncMapItemParams struct { 168 // How to order the Map Items returned by their `key` value. Can be: `asc` (ascending) or `desc` (descending) and the default is ascending. Map Items are [ordered lexicographically](https://en.wikipedia.org/wiki/Lexicographical_order) by Item key. 169 Order *string `json:"Order,omitempty"` 170 // The `key` of the first Sync Map Item resource to read. See also `bounds`. 171 From *string `json:"From,omitempty"` 172 // Whether to include the Map Item referenced by the `from` parameter. Can be: `inclusive` to include the Map Item referenced by the `from` parameter or `exclusive` to start with the next Map Item. The default value is `inclusive`. 173 Bounds *string `json:"Bounds,omitempty"` 174 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 175 PageSize *int `json:"PageSize,omitempty"` 176 // Max number of records to return. 177 Limit *int `json:"limit,omitempty"` 178 } 179 180 func (params *ListSyncMapItemParams) SetOrder(Order string) *ListSyncMapItemParams { 181 params.Order = &Order 182 return params 183 } 184 func (params *ListSyncMapItemParams) SetFrom(From string) *ListSyncMapItemParams { 185 params.From = &From 186 return params 187 } 188 func (params *ListSyncMapItemParams) SetBounds(Bounds string) *ListSyncMapItemParams { 189 params.Bounds = &Bounds 190 return params 191 } 192 func (params *ListSyncMapItemParams) SetPageSize(PageSize int) *ListSyncMapItemParams { 193 params.PageSize = &PageSize 194 return params 195 } 196 func (params *ListSyncMapItemParams) SetLimit(Limit int) *ListSyncMapItemParams { 197 params.Limit = &Limit 198 return params 199 } 200 201 // Retrieve a single page of SyncMapItem records from the API. Request is executed immediately. 202 func (c *ApiService) PageSyncMapItem(ServiceSid string, MapSid string, params *ListSyncMapItemParams, pageToken, pageNumber string) (*ListSyncMapItemResponse, error) { 203 path := "/v1/Services/{ServiceSid}/Maps/{MapSid}/Items" 204 205 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 206 path = strings.Replace(path, "{"+"MapSid"+"}", MapSid, -1) 207 208 data := url.Values{} 209 headers := make(map[string]interface{}) 210 211 if params != nil && params.Order != nil { 212 data.Set("Order", *params.Order) 213 } 214 if params != nil && params.From != nil { 215 data.Set("From", *params.From) 216 } 217 if params != nil && params.Bounds != nil { 218 data.Set("Bounds", *params.Bounds) 219 } 220 if params != nil && params.PageSize != nil { 221 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 222 } 223 224 if pageToken != "" { 225 data.Set("PageToken", pageToken) 226 } 227 if pageNumber != "" { 228 data.Set("Page", pageNumber) 229 } 230 231 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 232 if err != nil { 233 return nil, err 234 } 235 236 defer resp.Body.Close() 237 238 ps := &ListSyncMapItemResponse{} 239 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 240 return nil, err 241 } 242 243 return ps, err 244 } 245 246 // Lists SyncMapItem records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 247 func (c *ApiService) ListSyncMapItem(ServiceSid string, MapSid string, params *ListSyncMapItemParams) ([]SyncV1SyncMapItem, error) { 248 response, errors := c.StreamSyncMapItem(ServiceSid, MapSid, params) 249 250 records := make([]SyncV1SyncMapItem, 0) 251 for record := range response { 252 records = append(records, record) 253 } 254 255 if err := <-errors; err != nil { 256 return nil, err 257 } 258 259 return records, nil 260 } 261 262 // Streams SyncMapItem records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 263 func (c *ApiService) StreamSyncMapItem(ServiceSid string, MapSid string, params *ListSyncMapItemParams) (chan SyncV1SyncMapItem, chan error) { 264 if params == nil { 265 params = &ListSyncMapItemParams{} 266 } 267 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 268 269 recordChannel := make(chan SyncV1SyncMapItem, 1) 270 errorChannel := make(chan error, 1) 271 272 response, err := c.PageSyncMapItem(ServiceSid, MapSid, params, "", "") 273 if err != nil { 274 errorChannel <- err 275 close(recordChannel) 276 close(errorChannel) 277 } else { 278 go c.streamSyncMapItem(response, params, recordChannel, errorChannel) 279 } 280 281 return recordChannel, errorChannel 282 } 283 284 func (c *ApiService) streamSyncMapItem(response *ListSyncMapItemResponse, params *ListSyncMapItemParams, recordChannel chan SyncV1SyncMapItem, errorChannel chan error) { 285 curRecord := 1 286 287 for response != nil { 288 responseRecords := response.Items 289 for item := range responseRecords { 290 recordChannel <- responseRecords[item] 291 curRecord += 1 292 if params.Limit != nil && *params.Limit < curRecord { 293 close(recordChannel) 294 close(errorChannel) 295 return 296 } 297 } 298 299 record, err := client.GetNext(c.baseURL, response, c.getNextListSyncMapItemResponse) 300 if err != nil { 301 errorChannel <- err 302 break 303 } else if record == nil { 304 break 305 } 306 307 response = record.(*ListSyncMapItemResponse) 308 } 309 310 close(recordChannel) 311 close(errorChannel) 312 } 313 314 func (c *ApiService) getNextListSyncMapItemResponse(nextPageUrl string) (interface{}, error) { 315 if nextPageUrl == "" { 316 return nil, nil 317 } 318 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 319 if err != nil { 320 return nil, err 321 } 322 323 defer resp.Body.Close() 324 325 ps := &ListSyncMapItemResponse{} 326 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 327 return nil, err 328 } 329 return ps, nil 330 } 331 332 // Optional parameters for the method 'UpdateSyncMapItem' 333 type UpdateSyncMapItemParams struct { 334 // If provided, applies this mutation if (and only if) the “revision” field of this [map item] matches the provided value. This matches the semantics of (and is implemented with) the HTTP [If-Match header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match). 335 IfMatch *string `json:"If-Match,omitempty"` 336 // A JSON string that represents an arbitrary, schema-less object that the Map Item stores. Can be up to 16 KiB in length. 337 Data *interface{} `json:"Data,omitempty"` 338 // An alias for `item_ttl`. If both parameters are provided, this value is ignored. 339 Ttl *int `json:"Ttl,omitempty"` 340 // How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the Map Item expires (time-to-live) and is deleted. 341 ItemTtl *int `json:"ItemTtl,omitempty"` 342 // How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the Map Item's parent Sync Map expires (time-to-live) and is deleted. This parameter can only be used when the Map Item's `data` or `ttl` is updated in the same request. 343 CollectionTtl *int `json:"CollectionTtl,omitempty"` 344 } 345 346 func (params *UpdateSyncMapItemParams) SetIfMatch(IfMatch string) *UpdateSyncMapItemParams { 347 params.IfMatch = &IfMatch 348 return params 349 } 350 func (params *UpdateSyncMapItemParams) SetData(Data interface{}) *UpdateSyncMapItemParams { 351 params.Data = &Data 352 return params 353 } 354 func (params *UpdateSyncMapItemParams) SetTtl(Ttl int) *UpdateSyncMapItemParams { 355 params.Ttl = &Ttl 356 return params 357 } 358 func (params *UpdateSyncMapItemParams) SetItemTtl(ItemTtl int) *UpdateSyncMapItemParams { 359 params.ItemTtl = &ItemTtl 360 return params 361 } 362 func (params *UpdateSyncMapItemParams) SetCollectionTtl(CollectionTtl int) *UpdateSyncMapItemParams { 363 params.CollectionTtl = &CollectionTtl 364 return params 365 } 366 367 // 368 func (c *ApiService) UpdateSyncMapItem(ServiceSid string, MapSid string, Key string, params *UpdateSyncMapItemParams) (*SyncV1SyncMapItem, error) { 369 path := "/v1/Services/{ServiceSid}/Maps/{MapSid}/Items/{Key}" 370 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 371 path = strings.Replace(path, "{"+"MapSid"+"}", MapSid, -1) 372 path = strings.Replace(path, "{"+"Key"+"}", Key, -1) 373 374 data := url.Values{} 375 headers := make(map[string]interface{}) 376 377 if params != nil && params.Data != nil { 378 v, err := json.Marshal(params.Data) 379 380 if err != nil { 381 return nil, err 382 } 383 384 data.Set("Data", string(v)) 385 } 386 if params != nil && params.Ttl != nil { 387 data.Set("Ttl", fmt.Sprint(*params.Ttl)) 388 } 389 if params != nil && params.ItemTtl != nil { 390 data.Set("ItemTtl", fmt.Sprint(*params.ItemTtl)) 391 } 392 if params != nil && params.CollectionTtl != nil { 393 data.Set("CollectionTtl", fmt.Sprint(*params.CollectionTtl)) 394 } 395 396 if params != nil && params.IfMatch != nil { 397 headers["If-Match"] = *params.IfMatch 398 } 399 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 400 if err != nil { 401 return nil, err 402 } 403 404 defer resp.Body.Close() 405 406 ps := &SyncV1SyncMapItem{} 407 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 408 return nil, err 409 } 410 411 return ps, err 412 }