github.com/twilio/twilio-go@v1.20.1/rest/verify/v2/services_messaging_configurations.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Verify 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 'CreateMessagingConfiguration' 27 type CreateMessagingConfigurationParams struct { 28 // The [ISO-3166-1](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) country code of the country this configuration will be applied to. If this is a global configuration, Country will take the value `all`. 29 Country *string `json:"Country,omitempty"` 30 // The SID of the [Messaging Service](https://www.twilio.com/docs/messaging/api/service-resource) to be used to send SMS to the country of this configuration. 31 MessagingServiceSid *string `json:"MessagingServiceSid,omitempty"` 32 } 33 34 func (params *CreateMessagingConfigurationParams) SetCountry(Country string) *CreateMessagingConfigurationParams { 35 params.Country = &Country 36 return params 37 } 38 func (params *CreateMessagingConfigurationParams) SetMessagingServiceSid(MessagingServiceSid string) *CreateMessagingConfigurationParams { 39 params.MessagingServiceSid = &MessagingServiceSid 40 return params 41 } 42 43 // Create a new MessagingConfiguration for a service. 44 func (c *ApiService) CreateMessagingConfiguration(ServiceSid string, params *CreateMessagingConfigurationParams) (*VerifyV2MessagingConfiguration, error) { 45 path := "/v2/Services/{ServiceSid}/MessagingConfigurations" 46 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 47 48 data := url.Values{} 49 headers := make(map[string]interface{}) 50 51 if params != nil && params.Country != nil { 52 data.Set("Country", *params.Country) 53 } 54 if params != nil && params.MessagingServiceSid != nil { 55 data.Set("MessagingServiceSid", *params.MessagingServiceSid) 56 } 57 58 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 59 if err != nil { 60 return nil, err 61 } 62 63 defer resp.Body.Close() 64 65 ps := &VerifyV2MessagingConfiguration{} 66 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 67 return nil, err 68 } 69 70 return ps, err 71 } 72 73 // Delete a specific MessagingConfiguration. 74 func (c *ApiService) DeleteMessagingConfiguration(ServiceSid string, Country string) error { 75 path := "/v2/Services/{ServiceSid}/MessagingConfigurations/{Country}" 76 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 77 path = strings.Replace(path, "{"+"Country"+"}", Country, -1) 78 79 data := url.Values{} 80 headers := make(map[string]interface{}) 81 82 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 83 if err != nil { 84 return err 85 } 86 87 defer resp.Body.Close() 88 89 return nil 90 } 91 92 // Fetch a specific MessagingConfiguration. 93 func (c *ApiService) FetchMessagingConfiguration(ServiceSid string, Country string) (*VerifyV2MessagingConfiguration, error) { 94 path := "/v2/Services/{ServiceSid}/MessagingConfigurations/{Country}" 95 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 96 path = strings.Replace(path, "{"+"Country"+"}", Country, -1) 97 98 data := url.Values{} 99 headers := make(map[string]interface{}) 100 101 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 102 if err != nil { 103 return nil, err 104 } 105 106 defer resp.Body.Close() 107 108 ps := &VerifyV2MessagingConfiguration{} 109 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 110 return nil, err 111 } 112 113 return ps, err 114 } 115 116 // Optional parameters for the method 'ListMessagingConfiguration' 117 type ListMessagingConfigurationParams struct { 118 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 119 PageSize *int `json:"PageSize,omitempty"` 120 // Max number of records to return. 121 Limit *int `json:"limit,omitempty"` 122 } 123 124 func (params *ListMessagingConfigurationParams) SetPageSize(PageSize int) *ListMessagingConfigurationParams { 125 params.PageSize = &PageSize 126 return params 127 } 128 func (params *ListMessagingConfigurationParams) SetLimit(Limit int) *ListMessagingConfigurationParams { 129 params.Limit = &Limit 130 return params 131 } 132 133 // Retrieve a single page of MessagingConfiguration records from the API. Request is executed immediately. 134 func (c *ApiService) PageMessagingConfiguration(ServiceSid string, params *ListMessagingConfigurationParams, pageToken, pageNumber string) (*ListMessagingConfigurationResponse, error) { 135 path := "/v2/Services/{ServiceSid}/MessagingConfigurations" 136 137 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 138 139 data := url.Values{} 140 headers := make(map[string]interface{}) 141 142 if params != nil && params.PageSize != nil { 143 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 144 } 145 146 if pageToken != "" { 147 data.Set("PageToken", pageToken) 148 } 149 if pageNumber != "" { 150 data.Set("Page", pageNumber) 151 } 152 153 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 154 if err != nil { 155 return nil, err 156 } 157 158 defer resp.Body.Close() 159 160 ps := &ListMessagingConfigurationResponse{} 161 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 162 return nil, err 163 } 164 165 return ps, err 166 } 167 168 // Lists MessagingConfiguration records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 169 func (c *ApiService) ListMessagingConfiguration(ServiceSid string, params *ListMessagingConfigurationParams) ([]VerifyV2MessagingConfiguration, error) { 170 response, errors := c.StreamMessagingConfiguration(ServiceSid, params) 171 172 records := make([]VerifyV2MessagingConfiguration, 0) 173 for record := range response { 174 records = append(records, record) 175 } 176 177 if err := <-errors; err != nil { 178 return nil, err 179 } 180 181 return records, nil 182 } 183 184 // Streams MessagingConfiguration records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 185 func (c *ApiService) StreamMessagingConfiguration(ServiceSid string, params *ListMessagingConfigurationParams) (chan VerifyV2MessagingConfiguration, chan error) { 186 if params == nil { 187 params = &ListMessagingConfigurationParams{} 188 } 189 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 190 191 recordChannel := make(chan VerifyV2MessagingConfiguration, 1) 192 errorChannel := make(chan error, 1) 193 194 response, err := c.PageMessagingConfiguration(ServiceSid, params, "", "") 195 if err != nil { 196 errorChannel <- err 197 close(recordChannel) 198 close(errorChannel) 199 } else { 200 go c.streamMessagingConfiguration(response, params, recordChannel, errorChannel) 201 } 202 203 return recordChannel, errorChannel 204 } 205 206 func (c *ApiService) streamMessagingConfiguration(response *ListMessagingConfigurationResponse, params *ListMessagingConfigurationParams, recordChannel chan VerifyV2MessagingConfiguration, errorChannel chan error) { 207 curRecord := 1 208 209 for response != nil { 210 responseRecords := response.MessagingConfigurations 211 for item := range responseRecords { 212 recordChannel <- responseRecords[item] 213 curRecord += 1 214 if params.Limit != nil && *params.Limit < curRecord { 215 close(recordChannel) 216 close(errorChannel) 217 return 218 } 219 } 220 221 record, err := client.GetNext(c.baseURL, response, c.getNextListMessagingConfigurationResponse) 222 if err != nil { 223 errorChannel <- err 224 break 225 } else if record == nil { 226 break 227 } 228 229 response = record.(*ListMessagingConfigurationResponse) 230 } 231 232 close(recordChannel) 233 close(errorChannel) 234 } 235 236 func (c *ApiService) getNextListMessagingConfigurationResponse(nextPageUrl string) (interface{}, error) { 237 if nextPageUrl == "" { 238 return nil, nil 239 } 240 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 241 if err != nil { 242 return nil, err 243 } 244 245 defer resp.Body.Close() 246 247 ps := &ListMessagingConfigurationResponse{} 248 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 249 return nil, err 250 } 251 return ps, nil 252 } 253 254 // Optional parameters for the method 'UpdateMessagingConfiguration' 255 type UpdateMessagingConfigurationParams struct { 256 // The SID of the [Messaging Service](https://www.twilio.com/docs/messaging/api/service-resource) to be used to send SMS to the country of this configuration. 257 MessagingServiceSid *string `json:"MessagingServiceSid,omitempty"` 258 } 259 260 func (params *UpdateMessagingConfigurationParams) SetMessagingServiceSid(MessagingServiceSid string) *UpdateMessagingConfigurationParams { 261 params.MessagingServiceSid = &MessagingServiceSid 262 return params 263 } 264 265 // Update a specific MessagingConfiguration 266 func (c *ApiService) UpdateMessagingConfiguration(ServiceSid string, Country string, params *UpdateMessagingConfigurationParams) (*VerifyV2MessagingConfiguration, error) { 267 path := "/v2/Services/{ServiceSid}/MessagingConfigurations/{Country}" 268 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 269 path = strings.Replace(path, "{"+"Country"+"}", Country, -1) 270 271 data := url.Values{} 272 headers := make(map[string]interface{}) 273 274 if params != nil && params.MessagingServiceSid != nil { 275 data.Set("MessagingServiceSid", *params.MessagingServiceSid) 276 } 277 278 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 279 if err != nil { 280 return nil, err 281 } 282 283 defer resp.Body.Close() 284 285 ps := &VerifyV2MessagingConfiguration{} 286 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 287 return nil, err 288 } 289 290 return ps, err 291 }