github.com/twilio/twilio-go@v1.20.1/rest/messaging/v1/a2p_brand_registrations_vettings.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Messaging 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 'CreateBrandVetting' 27 type CreateBrandVettingParams struct { 28 // 29 VettingProvider *string `json:"VettingProvider,omitempty"` 30 // The unique ID of the vetting 31 VettingId *string `json:"VettingId,omitempty"` 32 } 33 34 func (params *CreateBrandVettingParams) SetVettingProvider(VettingProvider string) *CreateBrandVettingParams { 35 params.VettingProvider = &VettingProvider 36 return params 37 } 38 func (params *CreateBrandVettingParams) SetVettingId(VettingId string) *CreateBrandVettingParams { 39 params.VettingId = &VettingId 40 return params 41 } 42 43 // 44 func (c *ApiService) CreateBrandVetting(BrandSid string, params *CreateBrandVettingParams) (*MessagingV1BrandVetting, error) { 45 path := "/v1/a2p/BrandRegistrations/{BrandSid}/Vettings" 46 path = strings.Replace(path, "{"+"BrandSid"+"}", BrandSid, -1) 47 48 data := url.Values{} 49 headers := make(map[string]interface{}) 50 51 if params != nil && params.VettingProvider != nil { 52 data.Set("VettingProvider", *params.VettingProvider) 53 } 54 if params != nil && params.VettingId != nil { 55 data.Set("VettingId", *params.VettingId) 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 := &MessagingV1BrandVetting{} 66 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 67 return nil, err 68 } 69 70 return ps, err 71 } 72 73 // 74 func (c *ApiService) FetchBrandVetting(BrandSid string, BrandVettingSid string) (*MessagingV1BrandVetting, error) { 75 path := "/v1/a2p/BrandRegistrations/{BrandSid}/Vettings/{BrandVettingSid}" 76 path = strings.Replace(path, "{"+"BrandSid"+"}", BrandSid, -1) 77 path = strings.Replace(path, "{"+"BrandVettingSid"+"}", BrandVettingSid, -1) 78 79 data := url.Values{} 80 headers := make(map[string]interface{}) 81 82 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 83 if err != nil { 84 return nil, err 85 } 86 87 defer resp.Body.Close() 88 89 ps := &MessagingV1BrandVetting{} 90 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 91 return nil, err 92 } 93 94 return ps, err 95 } 96 97 // Optional parameters for the method 'ListBrandVetting' 98 type ListBrandVettingParams struct { 99 // The third-party provider of the vettings to read 100 VettingProvider *string `json:"VettingProvider,omitempty"` 101 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 102 PageSize *int `json:"PageSize,omitempty"` 103 // Max number of records to return. 104 Limit *int `json:"limit,omitempty"` 105 } 106 107 func (params *ListBrandVettingParams) SetVettingProvider(VettingProvider string) *ListBrandVettingParams { 108 params.VettingProvider = &VettingProvider 109 return params 110 } 111 func (params *ListBrandVettingParams) SetPageSize(PageSize int) *ListBrandVettingParams { 112 params.PageSize = &PageSize 113 return params 114 } 115 func (params *ListBrandVettingParams) SetLimit(Limit int) *ListBrandVettingParams { 116 params.Limit = &Limit 117 return params 118 } 119 120 // Retrieve a single page of BrandVetting records from the API. Request is executed immediately. 121 func (c *ApiService) PageBrandVetting(BrandSid string, params *ListBrandVettingParams, pageToken, pageNumber string) (*ListBrandVettingResponse, error) { 122 path := "/v1/a2p/BrandRegistrations/{BrandSid}/Vettings" 123 124 path = strings.Replace(path, "{"+"BrandSid"+"}", BrandSid, -1) 125 126 data := url.Values{} 127 headers := make(map[string]interface{}) 128 129 if params != nil && params.VettingProvider != nil { 130 data.Set("VettingProvider", *params.VettingProvider) 131 } 132 if params != nil && params.PageSize != nil { 133 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 134 } 135 136 if pageToken != "" { 137 data.Set("PageToken", pageToken) 138 } 139 if pageNumber != "" { 140 data.Set("Page", pageNumber) 141 } 142 143 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 144 if err != nil { 145 return nil, err 146 } 147 148 defer resp.Body.Close() 149 150 ps := &ListBrandVettingResponse{} 151 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 152 return nil, err 153 } 154 155 return ps, err 156 } 157 158 // Lists BrandVetting records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 159 func (c *ApiService) ListBrandVetting(BrandSid string, params *ListBrandVettingParams) ([]MessagingV1BrandVetting, error) { 160 response, errors := c.StreamBrandVetting(BrandSid, params) 161 162 records := make([]MessagingV1BrandVetting, 0) 163 for record := range response { 164 records = append(records, record) 165 } 166 167 if err := <-errors; err != nil { 168 return nil, err 169 } 170 171 return records, nil 172 } 173 174 // Streams BrandVetting records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 175 func (c *ApiService) StreamBrandVetting(BrandSid string, params *ListBrandVettingParams) (chan MessagingV1BrandVetting, chan error) { 176 if params == nil { 177 params = &ListBrandVettingParams{} 178 } 179 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 180 181 recordChannel := make(chan MessagingV1BrandVetting, 1) 182 errorChannel := make(chan error, 1) 183 184 response, err := c.PageBrandVetting(BrandSid, params, "", "") 185 if err != nil { 186 errorChannel <- err 187 close(recordChannel) 188 close(errorChannel) 189 } else { 190 go c.streamBrandVetting(response, params, recordChannel, errorChannel) 191 } 192 193 return recordChannel, errorChannel 194 } 195 196 func (c *ApiService) streamBrandVetting(response *ListBrandVettingResponse, params *ListBrandVettingParams, recordChannel chan MessagingV1BrandVetting, errorChannel chan error) { 197 curRecord := 1 198 199 for response != nil { 200 responseRecords := response.Data 201 for item := range responseRecords { 202 recordChannel <- responseRecords[item] 203 curRecord += 1 204 if params.Limit != nil && *params.Limit < curRecord { 205 close(recordChannel) 206 close(errorChannel) 207 return 208 } 209 } 210 211 record, err := client.GetNext(c.baseURL, response, c.getNextListBrandVettingResponse) 212 if err != nil { 213 errorChannel <- err 214 break 215 } else if record == nil { 216 break 217 } 218 219 response = record.(*ListBrandVettingResponse) 220 } 221 222 close(recordChannel) 223 close(errorChannel) 224 } 225 226 func (c *ApiService) getNextListBrandVettingResponse(nextPageUrl string) (interface{}, error) { 227 if nextPageUrl == "" { 228 return nil, nil 229 } 230 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 231 if err != nil { 232 return nil, err 233 } 234 235 defer resp.Body.Close() 236 237 ps := &ListBrandVettingResponse{} 238 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 239 return nil, err 240 } 241 return ps, nil 242 }