github.com/twilio/twilio-go@v1.20.1/rest/verify/v2/services_rate_limits.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 'CreateRateLimit' 27 type CreateRateLimitParams struct { 28 // Provides a unique and addressable name to be assigned to this Rate Limit, assigned by the developer, to be optionally used in addition to SID. **This value should not contain PII.** 29 UniqueName *string `json:"UniqueName,omitempty"` 30 // Description of this Rate Limit 31 Description *string `json:"Description,omitempty"` 32 } 33 34 func (params *CreateRateLimitParams) SetUniqueName(UniqueName string) *CreateRateLimitParams { 35 params.UniqueName = &UniqueName 36 return params 37 } 38 func (params *CreateRateLimitParams) SetDescription(Description string) *CreateRateLimitParams { 39 params.Description = &Description 40 return params 41 } 42 43 // Create a new Rate Limit for a Service 44 func (c *ApiService) CreateRateLimit(ServiceSid string, params *CreateRateLimitParams) (*VerifyV2RateLimit, error) { 45 path := "/v2/Services/{ServiceSid}/RateLimits" 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.UniqueName != nil { 52 data.Set("UniqueName", *params.UniqueName) 53 } 54 if params != nil && params.Description != nil { 55 data.Set("Description", *params.Description) 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 := &VerifyV2RateLimit{} 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 Rate Limit. 74 func (c *ApiService) DeleteRateLimit(ServiceSid string, Sid string) error { 75 path := "/v2/Services/{ServiceSid}/RateLimits/{Sid}" 76 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 77 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -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 Rate Limit. 93 func (c *ApiService) FetchRateLimit(ServiceSid string, Sid string) (*VerifyV2RateLimit, error) { 94 path := "/v2/Services/{ServiceSid}/RateLimits/{Sid}" 95 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 96 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -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 := &VerifyV2RateLimit{} 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 'ListRateLimit' 117 type ListRateLimitParams 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 *ListRateLimitParams) SetPageSize(PageSize int) *ListRateLimitParams { 125 params.PageSize = &PageSize 126 return params 127 } 128 func (params *ListRateLimitParams) SetLimit(Limit int) *ListRateLimitParams { 129 params.Limit = &Limit 130 return params 131 } 132 133 // Retrieve a single page of RateLimit records from the API. Request is executed immediately. 134 func (c *ApiService) PageRateLimit(ServiceSid string, params *ListRateLimitParams, pageToken, pageNumber string) (*ListRateLimitResponse, error) { 135 path := "/v2/Services/{ServiceSid}/RateLimits" 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 := &ListRateLimitResponse{} 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 RateLimit 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) ListRateLimit(ServiceSid string, params *ListRateLimitParams) ([]VerifyV2RateLimit, error) { 170 response, errors := c.StreamRateLimit(ServiceSid, params) 171 172 records := make([]VerifyV2RateLimit, 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 RateLimit 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) StreamRateLimit(ServiceSid string, params *ListRateLimitParams) (chan VerifyV2RateLimit, chan error) { 186 if params == nil { 187 params = &ListRateLimitParams{} 188 } 189 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 190 191 recordChannel := make(chan VerifyV2RateLimit, 1) 192 errorChannel := make(chan error, 1) 193 194 response, err := c.PageRateLimit(ServiceSid, params, "", "") 195 if err != nil { 196 errorChannel <- err 197 close(recordChannel) 198 close(errorChannel) 199 } else { 200 go c.streamRateLimit(response, params, recordChannel, errorChannel) 201 } 202 203 return recordChannel, errorChannel 204 } 205 206 func (c *ApiService) streamRateLimit(response *ListRateLimitResponse, params *ListRateLimitParams, recordChannel chan VerifyV2RateLimit, errorChannel chan error) { 207 curRecord := 1 208 209 for response != nil { 210 responseRecords := response.RateLimits 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.getNextListRateLimitResponse) 222 if err != nil { 223 errorChannel <- err 224 break 225 } else if record == nil { 226 break 227 } 228 229 response = record.(*ListRateLimitResponse) 230 } 231 232 close(recordChannel) 233 close(errorChannel) 234 } 235 236 func (c *ApiService) getNextListRateLimitResponse(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 := &ListRateLimitResponse{} 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 'UpdateRateLimit' 255 type UpdateRateLimitParams struct { 256 // Description of this Rate Limit 257 Description *string `json:"Description,omitempty"` 258 } 259 260 func (params *UpdateRateLimitParams) SetDescription(Description string) *UpdateRateLimitParams { 261 params.Description = &Description 262 return params 263 } 264 265 // Update a specific Rate Limit. 266 func (c *ApiService) UpdateRateLimit(ServiceSid string, Sid string, params *UpdateRateLimitParams) (*VerifyV2RateLimit, error) { 267 path := "/v2/Services/{ServiceSid}/RateLimits/{Sid}" 268 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 269 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 270 271 data := url.Values{} 272 headers := make(map[string]interface{}) 273 274 if params != nil && params.Description != nil { 275 data.Set("Description", *params.Description) 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 := &VerifyV2RateLimit{} 286 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 287 return nil, err 288 } 289 290 return ps, err 291 }