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