github.com/twilio/twilio-go@v1.20.1/rest/notify/v1/services.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Notify 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 'CreateService' 27 type CreateServiceParams struct { 28 // A descriptive string that you create to describe the resource. It can be up to 64 characters long. 29 FriendlyName *string `json:"FriendlyName,omitempty"` 30 // The SID of the [Credential](https://www.twilio.com/docs/notify/api/credential-resource) to use for APN Bindings. 31 ApnCredentialSid *string `json:"ApnCredentialSid,omitempty"` 32 // The SID of the [Credential](https://www.twilio.com/docs/notify/api/credential-resource) to use for GCM Bindings. 33 GcmCredentialSid *string `json:"GcmCredentialSid,omitempty"` 34 // The SID of the [Messaging Service](https://www.twilio.com/docs/sms/quickstart#messaging-services) to use for SMS Bindings. This parameter must be set in order to send SMS notifications. 35 MessagingServiceSid *string `json:"MessagingServiceSid,omitempty"` 36 // Deprecated. 37 FacebookMessengerPageId *string `json:"FacebookMessengerPageId,omitempty"` 38 // The protocol version to use for sending APNS notifications. Can be overridden on a Binding by Binding basis when creating a [Binding](https://www.twilio.com/docs/notify/api/binding-resource) resource. 39 DefaultApnNotificationProtocolVersion *string `json:"DefaultApnNotificationProtocolVersion,omitempty"` 40 // The protocol version to use for sending GCM notifications. Can be overridden on a Binding by Binding basis when creating a [Binding](https://www.twilio.com/docs/notify/api/binding-resource) resource. 41 DefaultGcmNotificationProtocolVersion *string `json:"DefaultGcmNotificationProtocolVersion,omitempty"` 42 // The SID of the [Credential](https://www.twilio.com/docs/notify/api/credential-resource) to use for FCM Bindings. 43 FcmCredentialSid *string `json:"FcmCredentialSid,omitempty"` 44 // The protocol version to use for sending FCM notifications. Can be overridden on a Binding by Binding basis when creating a [Binding](https://www.twilio.com/docs/notify/api/binding-resource) resource. 45 DefaultFcmNotificationProtocolVersion *string `json:"DefaultFcmNotificationProtocolVersion,omitempty"` 46 // Whether to log notifications. Can be: `true` or `false` and the default is `true`. 47 LogEnabled *bool `json:"LogEnabled,omitempty"` 48 // Deprecated. 49 AlexaSkillId *string `json:"AlexaSkillId,omitempty"` 50 // Deprecated. 51 DefaultAlexaNotificationProtocolVersion *string `json:"DefaultAlexaNotificationProtocolVersion,omitempty"` 52 // URL to send delivery status callback. 53 DeliveryCallbackUrl *string `json:"DeliveryCallbackUrl,omitempty"` 54 // Callback configuration that enables delivery callbacks, default false 55 DeliveryCallbackEnabled *bool `json:"DeliveryCallbackEnabled,omitempty"` 56 } 57 58 func (params *CreateServiceParams) SetFriendlyName(FriendlyName string) *CreateServiceParams { 59 params.FriendlyName = &FriendlyName 60 return params 61 } 62 func (params *CreateServiceParams) SetApnCredentialSid(ApnCredentialSid string) *CreateServiceParams { 63 params.ApnCredentialSid = &ApnCredentialSid 64 return params 65 } 66 func (params *CreateServiceParams) SetGcmCredentialSid(GcmCredentialSid string) *CreateServiceParams { 67 params.GcmCredentialSid = &GcmCredentialSid 68 return params 69 } 70 func (params *CreateServiceParams) SetMessagingServiceSid(MessagingServiceSid string) *CreateServiceParams { 71 params.MessagingServiceSid = &MessagingServiceSid 72 return params 73 } 74 func (params *CreateServiceParams) SetFacebookMessengerPageId(FacebookMessengerPageId string) *CreateServiceParams { 75 params.FacebookMessengerPageId = &FacebookMessengerPageId 76 return params 77 } 78 func (params *CreateServiceParams) SetDefaultApnNotificationProtocolVersion(DefaultApnNotificationProtocolVersion string) *CreateServiceParams { 79 params.DefaultApnNotificationProtocolVersion = &DefaultApnNotificationProtocolVersion 80 return params 81 } 82 func (params *CreateServiceParams) SetDefaultGcmNotificationProtocolVersion(DefaultGcmNotificationProtocolVersion string) *CreateServiceParams { 83 params.DefaultGcmNotificationProtocolVersion = &DefaultGcmNotificationProtocolVersion 84 return params 85 } 86 func (params *CreateServiceParams) SetFcmCredentialSid(FcmCredentialSid string) *CreateServiceParams { 87 params.FcmCredentialSid = &FcmCredentialSid 88 return params 89 } 90 func (params *CreateServiceParams) SetDefaultFcmNotificationProtocolVersion(DefaultFcmNotificationProtocolVersion string) *CreateServiceParams { 91 params.DefaultFcmNotificationProtocolVersion = &DefaultFcmNotificationProtocolVersion 92 return params 93 } 94 func (params *CreateServiceParams) SetLogEnabled(LogEnabled bool) *CreateServiceParams { 95 params.LogEnabled = &LogEnabled 96 return params 97 } 98 func (params *CreateServiceParams) SetAlexaSkillId(AlexaSkillId string) *CreateServiceParams { 99 params.AlexaSkillId = &AlexaSkillId 100 return params 101 } 102 func (params *CreateServiceParams) SetDefaultAlexaNotificationProtocolVersion(DefaultAlexaNotificationProtocolVersion string) *CreateServiceParams { 103 params.DefaultAlexaNotificationProtocolVersion = &DefaultAlexaNotificationProtocolVersion 104 return params 105 } 106 func (params *CreateServiceParams) SetDeliveryCallbackUrl(DeliveryCallbackUrl string) *CreateServiceParams { 107 params.DeliveryCallbackUrl = &DeliveryCallbackUrl 108 return params 109 } 110 func (params *CreateServiceParams) SetDeliveryCallbackEnabled(DeliveryCallbackEnabled bool) *CreateServiceParams { 111 params.DeliveryCallbackEnabled = &DeliveryCallbackEnabled 112 return params 113 } 114 115 // 116 func (c *ApiService) CreateService(params *CreateServiceParams) (*NotifyV1Service, error) { 117 path := "/v1/Services" 118 119 data := url.Values{} 120 headers := make(map[string]interface{}) 121 122 if params != nil && params.FriendlyName != nil { 123 data.Set("FriendlyName", *params.FriendlyName) 124 } 125 if params != nil && params.ApnCredentialSid != nil { 126 data.Set("ApnCredentialSid", *params.ApnCredentialSid) 127 } 128 if params != nil && params.GcmCredentialSid != nil { 129 data.Set("GcmCredentialSid", *params.GcmCredentialSid) 130 } 131 if params != nil && params.MessagingServiceSid != nil { 132 data.Set("MessagingServiceSid", *params.MessagingServiceSid) 133 } 134 if params != nil && params.FacebookMessengerPageId != nil { 135 data.Set("FacebookMessengerPageId", *params.FacebookMessengerPageId) 136 } 137 if params != nil && params.DefaultApnNotificationProtocolVersion != nil { 138 data.Set("DefaultApnNotificationProtocolVersion", *params.DefaultApnNotificationProtocolVersion) 139 } 140 if params != nil && params.DefaultGcmNotificationProtocolVersion != nil { 141 data.Set("DefaultGcmNotificationProtocolVersion", *params.DefaultGcmNotificationProtocolVersion) 142 } 143 if params != nil && params.FcmCredentialSid != nil { 144 data.Set("FcmCredentialSid", *params.FcmCredentialSid) 145 } 146 if params != nil && params.DefaultFcmNotificationProtocolVersion != nil { 147 data.Set("DefaultFcmNotificationProtocolVersion", *params.DefaultFcmNotificationProtocolVersion) 148 } 149 if params != nil && params.LogEnabled != nil { 150 data.Set("LogEnabled", fmt.Sprint(*params.LogEnabled)) 151 } 152 if params != nil && params.AlexaSkillId != nil { 153 data.Set("AlexaSkillId", *params.AlexaSkillId) 154 } 155 if params != nil && params.DefaultAlexaNotificationProtocolVersion != nil { 156 data.Set("DefaultAlexaNotificationProtocolVersion", *params.DefaultAlexaNotificationProtocolVersion) 157 } 158 if params != nil && params.DeliveryCallbackUrl != nil { 159 data.Set("DeliveryCallbackUrl", *params.DeliveryCallbackUrl) 160 } 161 if params != nil && params.DeliveryCallbackEnabled != nil { 162 data.Set("DeliveryCallbackEnabled", fmt.Sprint(*params.DeliveryCallbackEnabled)) 163 } 164 165 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 166 if err != nil { 167 return nil, err 168 } 169 170 defer resp.Body.Close() 171 172 ps := &NotifyV1Service{} 173 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 174 return nil, err 175 } 176 177 return ps, err 178 } 179 180 // 181 func (c *ApiService) DeleteService(Sid string) error { 182 path := "/v1/Services/{Sid}" 183 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 184 185 data := url.Values{} 186 headers := make(map[string]interface{}) 187 188 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 189 if err != nil { 190 return err 191 } 192 193 defer resp.Body.Close() 194 195 return nil 196 } 197 198 // 199 func (c *ApiService) FetchService(Sid string) (*NotifyV1Service, error) { 200 path := "/v1/Services/{Sid}" 201 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 202 203 data := url.Values{} 204 headers := make(map[string]interface{}) 205 206 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 207 if err != nil { 208 return nil, err 209 } 210 211 defer resp.Body.Close() 212 213 ps := &NotifyV1Service{} 214 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 215 return nil, err 216 } 217 218 return ps, err 219 } 220 221 // Optional parameters for the method 'ListService' 222 type ListServiceParams struct { 223 // The string that identifies the Service resources to read. 224 FriendlyName *string `json:"FriendlyName,omitempty"` 225 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 226 PageSize *int `json:"PageSize,omitempty"` 227 // Max number of records to return. 228 Limit *int `json:"limit,omitempty"` 229 } 230 231 func (params *ListServiceParams) SetFriendlyName(FriendlyName string) *ListServiceParams { 232 params.FriendlyName = &FriendlyName 233 return params 234 } 235 func (params *ListServiceParams) SetPageSize(PageSize int) *ListServiceParams { 236 params.PageSize = &PageSize 237 return params 238 } 239 func (params *ListServiceParams) SetLimit(Limit int) *ListServiceParams { 240 params.Limit = &Limit 241 return params 242 } 243 244 // Retrieve a single page of Service records from the API. Request is executed immediately. 245 func (c *ApiService) PageService(params *ListServiceParams, pageToken, pageNumber string) (*ListServiceResponse, error) { 246 path := "/v1/Services" 247 248 data := url.Values{} 249 headers := make(map[string]interface{}) 250 251 if params != nil && params.FriendlyName != nil { 252 data.Set("FriendlyName", *params.FriendlyName) 253 } 254 if params != nil && params.PageSize != nil { 255 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 256 } 257 258 if pageToken != "" { 259 data.Set("PageToken", pageToken) 260 } 261 if pageNumber != "" { 262 data.Set("Page", pageNumber) 263 } 264 265 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 266 if err != nil { 267 return nil, err 268 } 269 270 defer resp.Body.Close() 271 272 ps := &ListServiceResponse{} 273 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 274 return nil, err 275 } 276 277 return ps, err 278 } 279 280 // Lists Service records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 281 func (c *ApiService) ListService(params *ListServiceParams) ([]NotifyV1Service, error) { 282 response, errors := c.StreamService(params) 283 284 records := make([]NotifyV1Service, 0) 285 for record := range response { 286 records = append(records, record) 287 } 288 289 if err := <-errors; err != nil { 290 return nil, err 291 } 292 293 return records, nil 294 } 295 296 // Streams Service records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 297 func (c *ApiService) StreamService(params *ListServiceParams) (chan NotifyV1Service, chan error) { 298 if params == nil { 299 params = &ListServiceParams{} 300 } 301 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 302 303 recordChannel := make(chan NotifyV1Service, 1) 304 errorChannel := make(chan error, 1) 305 306 response, err := c.PageService(params, "", "") 307 if err != nil { 308 errorChannel <- err 309 close(recordChannel) 310 close(errorChannel) 311 } else { 312 go c.streamService(response, params, recordChannel, errorChannel) 313 } 314 315 return recordChannel, errorChannel 316 } 317 318 func (c *ApiService) streamService(response *ListServiceResponse, params *ListServiceParams, recordChannel chan NotifyV1Service, errorChannel chan error) { 319 curRecord := 1 320 321 for response != nil { 322 responseRecords := response.Services 323 for item := range responseRecords { 324 recordChannel <- responseRecords[item] 325 curRecord += 1 326 if params.Limit != nil && *params.Limit < curRecord { 327 close(recordChannel) 328 close(errorChannel) 329 return 330 } 331 } 332 333 record, err := client.GetNext(c.baseURL, response, c.getNextListServiceResponse) 334 if err != nil { 335 errorChannel <- err 336 break 337 } else if record == nil { 338 break 339 } 340 341 response = record.(*ListServiceResponse) 342 } 343 344 close(recordChannel) 345 close(errorChannel) 346 } 347 348 func (c *ApiService) getNextListServiceResponse(nextPageUrl string) (interface{}, error) { 349 if nextPageUrl == "" { 350 return nil, nil 351 } 352 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 353 if err != nil { 354 return nil, err 355 } 356 357 defer resp.Body.Close() 358 359 ps := &ListServiceResponse{} 360 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 361 return nil, err 362 } 363 return ps, nil 364 } 365 366 // Optional parameters for the method 'UpdateService' 367 type UpdateServiceParams struct { 368 // A descriptive string that you create to describe the resource. It can be up to 64 characters long. 369 FriendlyName *string `json:"FriendlyName,omitempty"` 370 // The SID of the [Credential](https://www.twilio.com/docs/notify/api/credential-resource) to use for APN Bindings. 371 ApnCredentialSid *string `json:"ApnCredentialSid,omitempty"` 372 // The SID of the [Credential](https://www.twilio.com/docs/notify/api/credential-resource) to use for GCM Bindings. 373 GcmCredentialSid *string `json:"GcmCredentialSid,omitempty"` 374 // The SID of the [Messaging Service](https://www.twilio.com/docs/sms/quickstart#messaging-services) to use for SMS Bindings. This parameter must be set in order to send SMS notifications. 375 MessagingServiceSid *string `json:"MessagingServiceSid,omitempty"` 376 // Deprecated. 377 FacebookMessengerPageId *string `json:"FacebookMessengerPageId,omitempty"` 378 // The protocol version to use for sending APNS notifications. Can be overridden on a Binding by Binding basis when creating a [Binding](https://www.twilio.com/docs/notify/api/binding-resource) resource. 379 DefaultApnNotificationProtocolVersion *string `json:"DefaultApnNotificationProtocolVersion,omitempty"` 380 // The protocol version to use for sending GCM notifications. Can be overridden on a Binding by Binding basis when creating a [Binding](https://www.twilio.com/docs/notify/api/binding-resource) resource. 381 DefaultGcmNotificationProtocolVersion *string `json:"DefaultGcmNotificationProtocolVersion,omitempty"` 382 // The SID of the [Credential](https://www.twilio.com/docs/notify/api/credential-resource) to use for FCM Bindings. 383 FcmCredentialSid *string `json:"FcmCredentialSid,omitempty"` 384 // The protocol version to use for sending FCM notifications. Can be overridden on a Binding by Binding basis when creating a [Binding](https://www.twilio.com/docs/notify/api/binding-resource) resource. 385 DefaultFcmNotificationProtocolVersion *string `json:"DefaultFcmNotificationProtocolVersion,omitempty"` 386 // Whether to log notifications. Can be: `true` or `false` and the default is `true`. 387 LogEnabled *bool `json:"LogEnabled,omitempty"` 388 // Deprecated. 389 AlexaSkillId *string `json:"AlexaSkillId,omitempty"` 390 // Deprecated. 391 DefaultAlexaNotificationProtocolVersion *string `json:"DefaultAlexaNotificationProtocolVersion,omitempty"` 392 // URL to send delivery status callback. 393 DeliveryCallbackUrl *string `json:"DeliveryCallbackUrl,omitempty"` 394 // Callback configuration that enables delivery callbacks, default false 395 DeliveryCallbackEnabled *bool `json:"DeliveryCallbackEnabled,omitempty"` 396 } 397 398 func (params *UpdateServiceParams) SetFriendlyName(FriendlyName string) *UpdateServiceParams { 399 params.FriendlyName = &FriendlyName 400 return params 401 } 402 func (params *UpdateServiceParams) SetApnCredentialSid(ApnCredentialSid string) *UpdateServiceParams { 403 params.ApnCredentialSid = &ApnCredentialSid 404 return params 405 } 406 func (params *UpdateServiceParams) SetGcmCredentialSid(GcmCredentialSid string) *UpdateServiceParams { 407 params.GcmCredentialSid = &GcmCredentialSid 408 return params 409 } 410 func (params *UpdateServiceParams) SetMessagingServiceSid(MessagingServiceSid string) *UpdateServiceParams { 411 params.MessagingServiceSid = &MessagingServiceSid 412 return params 413 } 414 func (params *UpdateServiceParams) SetFacebookMessengerPageId(FacebookMessengerPageId string) *UpdateServiceParams { 415 params.FacebookMessengerPageId = &FacebookMessengerPageId 416 return params 417 } 418 func (params *UpdateServiceParams) SetDefaultApnNotificationProtocolVersion(DefaultApnNotificationProtocolVersion string) *UpdateServiceParams { 419 params.DefaultApnNotificationProtocolVersion = &DefaultApnNotificationProtocolVersion 420 return params 421 } 422 func (params *UpdateServiceParams) SetDefaultGcmNotificationProtocolVersion(DefaultGcmNotificationProtocolVersion string) *UpdateServiceParams { 423 params.DefaultGcmNotificationProtocolVersion = &DefaultGcmNotificationProtocolVersion 424 return params 425 } 426 func (params *UpdateServiceParams) SetFcmCredentialSid(FcmCredentialSid string) *UpdateServiceParams { 427 params.FcmCredentialSid = &FcmCredentialSid 428 return params 429 } 430 func (params *UpdateServiceParams) SetDefaultFcmNotificationProtocolVersion(DefaultFcmNotificationProtocolVersion string) *UpdateServiceParams { 431 params.DefaultFcmNotificationProtocolVersion = &DefaultFcmNotificationProtocolVersion 432 return params 433 } 434 func (params *UpdateServiceParams) SetLogEnabled(LogEnabled bool) *UpdateServiceParams { 435 params.LogEnabled = &LogEnabled 436 return params 437 } 438 func (params *UpdateServiceParams) SetAlexaSkillId(AlexaSkillId string) *UpdateServiceParams { 439 params.AlexaSkillId = &AlexaSkillId 440 return params 441 } 442 func (params *UpdateServiceParams) SetDefaultAlexaNotificationProtocolVersion(DefaultAlexaNotificationProtocolVersion string) *UpdateServiceParams { 443 params.DefaultAlexaNotificationProtocolVersion = &DefaultAlexaNotificationProtocolVersion 444 return params 445 } 446 func (params *UpdateServiceParams) SetDeliveryCallbackUrl(DeliveryCallbackUrl string) *UpdateServiceParams { 447 params.DeliveryCallbackUrl = &DeliveryCallbackUrl 448 return params 449 } 450 func (params *UpdateServiceParams) SetDeliveryCallbackEnabled(DeliveryCallbackEnabled bool) *UpdateServiceParams { 451 params.DeliveryCallbackEnabled = &DeliveryCallbackEnabled 452 return params 453 } 454 455 // 456 func (c *ApiService) UpdateService(Sid string, params *UpdateServiceParams) (*NotifyV1Service, error) { 457 path := "/v1/Services/{Sid}" 458 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 459 460 data := url.Values{} 461 headers := make(map[string]interface{}) 462 463 if params != nil && params.FriendlyName != nil { 464 data.Set("FriendlyName", *params.FriendlyName) 465 } 466 if params != nil && params.ApnCredentialSid != nil { 467 data.Set("ApnCredentialSid", *params.ApnCredentialSid) 468 } 469 if params != nil && params.GcmCredentialSid != nil { 470 data.Set("GcmCredentialSid", *params.GcmCredentialSid) 471 } 472 if params != nil && params.MessagingServiceSid != nil { 473 data.Set("MessagingServiceSid", *params.MessagingServiceSid) 474 } 475 if params != nil && params.FacebookMessengerPageId != nil { 476 data.Set("FacebookMessengerPageId", *params.FacebookMessengerPageId) 477 } 478 if params != nil && params.DefaultApnNotificationProtocolVersion != nil { 479 data.Set("DefaultApnNotificationProtocolVersion", *params.DefaultApnNotificationProtocolVersion) 480 } 481 if params != nil && params.DefaultGcmNotificationProtocolVersion != nil { 482 data.Set("DefaultGcmNotificationProtocolVersion", *params.DefaultGcmNotificationProtocolVersion) 483 } 484 if params != nil && params.FcmCredentialSid != nil { 485 data.Set("FcmCredentialSid", *params.FcmCredentialSid) 486 } 487 if params != nil && params.DefaultFcmNotificationProtocolVersion != nil { 488 data.Set("DefaultFcmNotificationProtocolVersion", *params.DefaultFcmNotificationProtocolVersion) 489 } 490 if params != nil && params.LogEnabled != nil { 491 data.Set("LogEnabled", fmt.Sprint(*params.LogEnabled)) 492 } 493 if params != nil && params.AlexaSkillId != nil { 494 data.Set("AlexaSkillId", *params.AlexaSkillId) 495 } 496 if params != nil && params.DefaultAlexaNotificationProtocolVersion != nil { 497 data.Set("DefaultAlexaNotificationProtocolVersion", *params.DefaultAlexaNotificationProtocolVersion) 498 } 499 if params != nil && params.DeliveryCallbackUrl != nil { 500 data.Set("DeliveryCallbackUrl", *params.DeliveryCallbackUrl) 501 } 502 if params != nil && params.DeliveryCallbackEnabled != nil { 503 data.Set("DeliveryCallbackEnabled", fmt.Sprint(*params.DeliveryCallbackEnabled)) 504 } 505 506 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 507 if err != nil { 508 return nil, err 509 } 510 511 defer resp.Body.Close() 512 513 ps := &NotifyV1Service{} 514 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 515 return nil, err 516 } 517 518 return ps, err 519 }