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