github.com/twilio/twilio-go@v1.20.1/rest/trusthub/v1/trust_products.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Trusthub 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 'CreateTrustProduct' 27 type CreateTrustProductParams struct { 28 // The string that you assigned to describe the resource. 29 FriendlyName *string `json:"FriendlyName,omitempty"` 30 // The email address that will receive updates when the Trust Product resource changes status. 31 Email *string `json:"Email,omitempty"` 32 // The unique string of a policy that is associated to the Trust Product resource. 33 PolicySid *string `json:"PolicySid,omitempty"` 34 // The URL we call to inform your application of status changes. 35 StatusCallback *string `json:"StatusCallback,omitempty"` 36 } 37 38 func (params *CreateTrustProductParams) SetFriendlyName(FriendlyName string) *CreateTrustProductParams { 39 params.FriendlyName = &FriendlyName 40 return params 41 } 42 func (params *CreateTrustProductParams) SetEmail(Email string) *CreateTrustProductParams { 43 params.Email = &Email 44 return params 45 } 46 func (params *CreateTrustProductParams) SetPolicySid(PolicySid string) *CreateTrustProductParams { 47 params.PolicySid = &PolicySid 48 return params 49 } 50 func (params *CreateTrustProductParams) SetStatusCallback(StatusCallback string) *CreateTrustProductParams { 51 params.StatusCallback = &StatusCallback 52 return params 53 } 54 55 // Create a new Trust Product. 56 func (c *ApiService) CreateTrustProduct(params *CreateTrustProductParams) (*TrusthubV1TrustProduct, error) { 57 path := "/v1/TrustProducts" 58 59 data := url.Values{} 60 headers := make(map[string]interface{}) 61 62 if params != nil && params.FriendlyName != nil { 63 data.Set("FriendlyName", *params.FriendlyName) 64 } 65 if params != nil && params.Email != nil { 66 data.Set("Email", *params.Email) 67 } 68 if params != nil && params.PolicySid != nil { 69 data.Set("PolicySid", *params.PolicySid) 70 } 71 if params != nil && params.StatusCallback != nil { 72 data.Set("StatusCallback", *params.StatusCallback) 73 } 74 75 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 76 if err != nil { 77 return nil, err 78 } 79 80 defer resp.Body.Close() 81 82 ps := &TrusthubV1TrustProduct{} 83 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 84 return nil, err 85 } 86 87 return ps, err 88 } 89 90 // Delete a specific Trust Product. 91 func (c *ApiService) DeleteTrustProduct(Sid string) error { 92 path := "/v1/TrustProducts/{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.Delete(c.baseURL+path, data, headers) 99 if err != nil { 100 return err 101 } 102 103 defer resp.Body.Close() 104 105 return nil 106 } 107 108 // Fetch a specific Trust Product instance. 109 func (c *ApiService) FetchTrustProduct(Sid string) (*TrusthubV1TrustProduct, error) { 110 path := "/v1/TrustProducts/{Sid}" 111 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 112 113 data := url.Values{} 114 headers := make(map[string]interface{}) 115 116 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 117 if err != nil { 118 return nil, err 119 } 120 121 defer resp.Body.Close() 122 123 ps := &TrusthubV1TrustProduct{} 124 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 125 return nil, err 126 } 127 128 return ps, err 129 } 130 131 // Optional parameters for the method 'ListTrustProduct' 132 type ListTrustProductParams struct { 133 // The verification status of the Trust Product resource. 134 Status *string `json:"Status,omitempty"` 135 // The string that you assigned to describe the resource. 136 FriendlyName *string `json:"FriendlyName,omitempty"` 137 // The unique string of a policy that is associated to the Trust Product resource. 138 PolicySid *string `json:"PolicySid,omitempty"` 139 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 140 PageSize *int `json:"PageSize,omitempty"` 141 // Max number of records to return. 142 Limit *int `json:"limit,omitempty"` 143 } 144 145 func (params *ListTrustProductParams) SetStatus(Status string) *ListTrustProductParams { 146 params.Status = &Status 147 return params 148 } 149 func (params *ListTrustProductParams) SetFriendlyName(FriendlyName string) *ListTrustProductParams { 150 params.FriendlyName = &FriendlyName 151 return params 152 } 153 func (params *ListTrustProductParams) SetPolicySid(PolicySid string) *ListTrustProductParams { 154 params.PolicySid = &PolicySid 155 return params 156 } 157 func (params *ListTrustProductParams) SetPageSize(PageSize int) *ListTrustProductParams { 158 params.PageSize = &PageSize 159 return params 160 } 161 func (params *ListTrustProductParams) SetLimit(Limit int) *ListTrustProductParams { 162 params.Limit = &Limit 163 return params 164 } 165 166 // Retrieve a single page of TrustProduct records from the API. Request is executed immediately. 167 func (c *ApiService) PageTrustProduct(params *ListTrustProductParams, pageToken, pageNumber string) (*ListTrustProductResponse, error) { 168 path := "/v1/TrustProducts" 169 170 data := url.Values{} 171 headers := make(map[string]interface{}) 172 173 if params != nil && params.Status != nil { 174 data.Set("Status", *params.Status) 175 } 176 if params != nil && params.FriendlyName != nil { 177 data.Set("FriendlyName", *params.FriendlyName) 178 } 179 if params != nil && params.PolicySid != nil { 180 data.Set("PolicySid", *params.PolicySid) 181 } 182 if params != nil && params.PageSize != nil { 183 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 184 } 185 186 if pageToken != "" { 187 data.Set("PageToken", pageToken) 188 } 189 if pageNumber != "" { 190 data.Set("Page", pageNumber) 191 } 192 193 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 194 if err != nil { 195 return nil, err 196 } 197 198 defer resp.Body.Close() 199 200 ps := &ListTrustProductResponse{} 201 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 202 return nil, err 203 } 204 205 return ps, err 206 } 207 208 // Lists TrustProduct records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 209 func (c *ApiService) ListTrustProduct(params *ListTrustProductParams) ([]TrusthubV1TrustProduct, error) { 210 response, errors := c.StreamTrustProduct(params) 211 212 records := make([]TrusthubV1TrustProduct, 0) 213 for record := range response { 214 records = append(records, record) 215 } 216 217 if err := <-errors; err != nil { 218 return nil, err 219 } 220 221 return records, nil 222 } 223 224 // Streams TrustProduct records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 225 func (c *ApiService) StreamTrustProduct(params *ListTrustProductParams) (chan TrusthubV1TrustProduct, chan error) { 226 if params == nil { 227 params = &ListTrustProductParams{} 228 } 229 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 230 231 recordChannel := make(chan TrusthubV1TrustProduct, 1) 232 errorChannel := make(chan error, 1) 233 234 response, err := c.PageTrustProduct(params, "", "") 235 if err != nil { 236 errorChannel <- err 237 close(recordChannel) 238 close(errorChannel) 239 } else { 240 go c.streamTrustProduct(response, params, recordChannel, errorChannel) 241 } 242 243 return recordChannel, errorChannel 244 } 245 246 func (c *ApiService) streamTrustProduct(response *ListTrustProductResponse, params *ListTrustProductParams, recordChannel chan TrusthubV1TrustProduct, errorChannel chan error) { 247 curRecord := 1 248 249 for response != nil { 250 responseRecords := response.Results 251 for item := range responseRecords { 252 recordChannel <- responseRecords[item] 253 curRecord += 1 254 if params.Limit != nil && *params.Limit < curRecord { 255 close(recordChannel) 256 close(errorChannel) 257 return 258 } 259 } 260 261 record, err := client.GetNext(c.baseURL, response, c.getNextListTrustProductResponse) 262 if err != nil { 263 errorChannel <- err 264 break 265 } else if record == nil { 266 break 267 } 268 269 response = record.(*ListTrustProductResponse) 270 } 271 272 close(recordChannel) 273 close(errorChannel) 274 } 275 276 func (c *ApiService) getNextListTrustProductResponse(nextPageUrl string) (interface{}, error) { 277 if nextPageUrl == "" { 278 return nil, nil 279 } 280 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 281 if err != nil { 282 return nil, err 283 } 284 285 defer resp.Body.Close() 286 287 ps := &ListTrustProductResponse{} 288 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 289 return nil, err 290 } 291 return ps, nil 292 } 293 294 // Optional parameters for the method 'UpdateTrustProduct' 295 type UpdateTrustProductParams struct { 296 // 297 Status *string `json:"Status,omitempty"` 298 // The URL we call to inform your application of status changes. 299 StatusCallback *string `json:"StatusCallback,omitempty"` 300 // The string that you assigned to describe the resource. 301 FriendlyName *string `json:"FriendlyName,omitempty"` 302 // The email address that will receive updates when the Trust Product resource changes status. 303 Email *string `json:"Email,omitempty"` 304 } 305 306 func (params *UpdateTrustProductParams) SetStatus(Status string) *UpdateTrustProductParams { 307 params.Status = &Status 308 return params 309 } 310 func (params *UpdateTrustProductParams) SetStatusCallback(StatusCallback string) *UpdateTrustProductParams { 311 params.StatusCallback = &StatusCallback 312 return params 313 } 314 func (params *UpdateTrustProductParams) SetFriendlyName(FriendlyName string) *UpdateTrustProductParams { 315 params.FriendlyName = &FriendlyName 316 return params 317 } 318 func (params *UpdateTrustProductParams) SetEmail(Email string) *UpdateTrustProductParams { 319 params.Email = &Email 320 return params 321 } 322 323 // Updates a Trust Product in an account. 324 func (c *ApiService) UpdateTrustProduct(Sid string, params *UpdateTrustProductParams) (*TrusthubV1TrustProduct, error) { 325 path := "/v1/TrustProducts/{Sid}" 326 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 327 328 data := url.Values{} 329 headers := make(map[string]interface{}) 330 331 if params != nil && params.Status != nil { 332 data.Set("Status", *params.Status) 333 } 334 if params != nil && params.StatusCallback != nil { 335 data.Set("StatusCallback", *params.StatusCallback) 336 } 337 if params != nil && params.FriendlyName != nil { 338 data.Set("FriendlyName", *params.FriendlyName) 339 } 340 if params != nil && params.Email != nil { 341 data.Set("Email", *params.Email) 342 } 343 344 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 345 if err != nil { 346 return nil, err 347 } 348 349 defer resp.Body.Close() 350 351 ps := &TrusthubV1TrustProduct{} 352 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 353 return nil, err 354 } 355 356 return ps, err 357 }