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