github.com/twilio/twilio-go@v1.20.1/rest/verify/v2/attempts.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  	"time"
    23  
    24  	"github.com/twilio/twilio-go/client"
    25  )
    26  
    27  // Fetch a specific verification attempt.
    28  func (c *ApiService) FetchVerificationAttempt(Sid string) (*VerifyV2VerificationAttempt, error) {
    29  	path := "/v2/Attempts/{Sid}"
    30  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    31  
    32  	data := url.Values{}
    33  	headers := make(map[string]interface{})
    34  
    35  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	defer resp.Body.Close()
    41  
    42  	ps := &VerifyV2VerificationAttempt{}
    43  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return ps, err
    48  }
    49  
    50  // Optional parameters for the method 'ListVerificationAttempt'
    51  type ListVerificationAttemptParams struct {
    52  	// Datetime filter used to consider only Verification Attempts created after this datetime on the summary aggregation. Given as GMT in ISO 8601 formatted datetime string: yyyy-MM-dd'T'HH:mm:ss'Z.
    53  	DateCreatedAfter *time.Time `json:"DateCreatedAfter,omitempty"`
    54  	// Datetime filter used to consider only Verification Attempts created before this datetime on the summary aggregation. Given as GMT in ISO 8601 formatted datetime string: yyyy-MM-dd'T'HH:mm:ss'Z.
    55  	DateCreatedBefore *time.Time `json:"DateCreatedBefore,omitempty"`
    56  	// Destination of a verification. It is phone number in E.164 format.
    57  	ChannelDataTo *string `json:"ChannelData.To,omitempty"`
    58  	// Filter used to query Verification Attempts sent to the specified destination country.
    59  	Country *string `json:"Country,omitempty"`
    60  	// Filter used to query Verification Attempts by communication channel. Valid values are `SMS` and `CALL`
    61  	Channel *string `json:"Channel,omitempty"`
    62  	// Filter used to query Verification Attempts by verify service. Only attempts of the provided SID will be returned.
    63  	VerifyServiceSid *string `json:"VerifyServiceSid,omitempty"`
    64  	// Filter used to return all the Verification Attempts of a single verification. Only attempts of the provided verification SID will be returned.
    65  	VerificationSid *string `json:"VerificationSid,omitempty"`
    66  	// Filter used to query Verification Attempts by conversion status. Valid values are `UNCONVERTED`, for attempts that were not converted, and `CONVERTED`, for attempts that were confirmed.
    67  	Status *string `json:"Status,omitempty"`
    68  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    69  	PageSize *int `json:"PageSize,omitempty"`
    70  	// Max number of records to return.
    71  	Limit *int `json:"limit,omitempty"`
    72  }
    73  
    74  func (params *ListVerificationAttemptParams) SetDateCreatedAfter(DateCreatedAfter time.Time) *ListVerificationAttemptParams {
    75  	params.DateCreatedAfter = &DateCreatedAfter
    76  	return params
    77  }
    78  func (params *ListVerificationAttemptParams) SetDateCreatedBefore(DateCreatedBefore time.Time) *ListVerificationAttemptParams {
    79  	params.DateCreatedBefore = &DateCreatedBefore
    80  	return params
    81  }
    82  func (params *ListVerificationAttemptParams) SetChannelDataTo(ChannelDataTo string) *ListVerificationAttemptParams {
    83  	params.ChannelDataTo = &ChannelDataTo
    84  	return params
    85  }
    86  func (params *ListVerificationAttemptParams) SetCountry(Country string) *ListVerificationAttemptParams {
    87  	params.Country = &Country
    88  	return params
    89  }
    90  func (params *ListVerificationAttemptParams) SetChannel(Channel string) *ListVerificationAttemptParams {
    91  	params.Channel = &Channel
    92  	return params
    93  }
    94  func (params *ListVerificationAttemptParams) SetVerifyServiceSid(VerifyServiceSid string) *ListVerificationAttemptParams {
    95  	params.VerifyServiceSid = &VerifyServiceSid
    96  	return params
    97  }
    98  func (params *ListVerificationAttemptParams) SetVerificationSid(VerificationSid string) *ListVerificationAttemptParams {
    99  	params.VerificationSid = &VerificationSid
   100  	return params
   101  }
   102  func (params *ListVerificationAttemptParams) SetStatus(Status string) *ListVerificationAttemptParams {
   103  	params.Status = &Status
   104  	return params
   105  }
   106  func (params *ListVerificationAttemptParams) SetPageSize(PageSize int) *ListVerificationAttemptParams {
   107  	params.PageSize = &PageSize
   108  	return params
   109  }
   110  func (params *ListVerificationAttemptParams) SetLimit(Limit int) *ListVerificationAttemptParams {
   111  	params.Limit = &Limit
   112  	return params
   113  }
   114  
   115  // Retrieve a single page of VerificationAttempt records from the API. Request is executed immediately.
   116  func (c *ApiService) PageVerificationAttempt(params *ListVerificationAttemptParams, pageToken, pageNumber string) (*ListVerificationAttemptResponse, error) {
   117  	path := "/v2/Attempts"
   118  
   119  	data := url.Values{}
   120  	headers := make(map[string]interface{})
   121  
   122  	if params != nil && params.DateCreatedAfter != nil {
   123  		data.Set("DateCreatedAfter", fmt.Sprint((*params.DateCreatedAfter).Format(time.RFC3339)))
   124  	}
   125  	if params != nil && params.DateCreatedBefore != nil {
   126  		data.Set("DateCreatedBefore", fmt.Sprint((*params.DateCreatedBefore).Format(time.RFC3339)))
   127  	}
   128  	if params != nil && params.ChannelDataTo != nil {
   129  		data.Set("ChannelData.To", *params.ChannelDataTo)
   130  	}
   131  	if params != nil && params.Country != nil {
   132  		data.Set("Country", *params.Country)
   133  	}
   134  	if params != nil && params.Channel != nil {
   135  		data.Set("Channel", *params.Channel)
   136  	}
   137  	if params != nil && params.VerifyServiceSid != nil {
   138  		data.Set("VerifyServiceSid", *params.VerifyServiceSid)
   139  	}
   140  	if params != nil && params.VerificationSid != nil {
   141  		data.Set("VerificationSid", *params.VerificationSid)
   142  	}
   143  	if params != nil && params.Status != nil {
   144  		data.Set("Status", *params.Status)
   145  	}
   146  	if params != nil && params.PageSize != nil {
   147  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   148  	}
   149  
   150  	if pageToken != "" {
   151  		data.Set("PageToken", pageToken)
   152  	}
   153  	if pageNumber != "" {
   154  		data.Set("Page", pageNumber)
   155  	}
   156  
   157  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  
   162  	defer resp.Body.Close()
   163  
   164  	ps := &ListVerificationAttemptResponse{}
   165  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	return ps, err
   170  }
   171  
   172  // Lists VerificationAttempt records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   173  func (c *ApiService) ListVerificationAttempt(params *ListVerificationAttemptParams) ([]VerifyV2VerificationAttempt, error) {
   174  	response, errors := c.StreamVerificationAttempt(params)
   175  
   176  	records := make([]VerifyV2VerificationAttempt, 0)
   177  	for record := range response {
   178  		records = append(records, record)
   179  	}
   180  
   181  	if err := <-errors; err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	return records, nil
   186  }
   187  
   188  // Streams VerificationAttempt records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   189  func (c *ApiService) StreamVerificationAttempt(params *ListVerificationAttemptParams) (chan VerifyV2VerificationAttempt, chan error) {
   190  	if params == nil {
   191  		params = &ListVerificationAttemptParams{}
   192  	}
   193  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   194  
   195  	recordChannel := make(chan VerifyV2VerificationAttempt, 1)
   196  	errorChannel := make(chan error, 1)
   197  
   198  	response, err := c.PageVerificationAttempt(params, "", "")
   199  	if err != nil {
   200  		errorChannel <- err
   201  		close(recordChannel)
   202  		close(errorChannel)
   203  	} else {
   204  		go c.streamVerificationAttempt(response, params, recordChannel, errorChannel)
   205  	}
   206  
   207  	return recordChannel, errorChannel
   208  }
   209  
   210  func (c *ApiService) streamVerificationAttempt(response *ListVerificationAttemptResponse, params *ListVerificationAttemptParams, recordChannel chan VerifyV2VerificationAttempt, errorChannel chan error) {
   211  	curRecord := 1
   212  
   213  	for response != nil {
   214  		responseRecords := response.Attempts
   215  		for item := range responseRecords {
   216  			recordChannel <- responseRecords[item]
   217  			curRecord += 1
   218  			if params.Limit != nil && *params.Limit < curRecord {
   219  				close(recordChannel)
   220  				close(errorChannel)
   221  				return
   222  			}
   223  		}
   224  
   225  		record, err := client.GetNext(c.baseURL, response, c.getNextListVerificationAttemptResponse)
   226  		if err != nil {
   227  			errorChannel <- err
   228  			break
   229  		} else if record == nil {
   230  			break
   231  		}
   232  
   233  		response = record.(*ListVerificationAttemptResponse)
   234  	}
   235  
   236  	close(recordChannel)
   237  	close(errorChannel)
   238  }
   239  
   240  func (c *ApiService) getNextListVerificationAttemptResponse(nextPageUrl string) (interface{}, error) {
   241  	if nextPageUrl == "" {
   242  		return nil, nil
   243  	}
   244  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   245  	if err != nil {
   246  		return nil, err
   247  	}
   248  
   249  	defer resp.Body.Close()
   250  
   251  	ps := &ListVerificationAttemptResponse{}
   252  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   253  		return nil, err
   254  	}
   255  	return ps, nil
   256  }