github.com/twilio/twilio-go@v1.20.1/rest/events/v1/types.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Events
     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  // Fetch a specific Event Type.
    27  func (c *ApiService) FetchEventType(Type string) (*EventsV1EventType, error) {
    28  	path := "/v1/Types/{Type}"
    29  	path = strings.Replace(path, "{"+"Type"+"}", Type, -1)
    30  
    31  	data := url.Values{}
    32  	headers := make(map[string]interface{})
    33  
    34  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	defer resp.Body.Close()
    40  
    41  	ps := &EventsV1EventType{}
    42  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return ps, err
    47  }
    48  
    49  // Optional parameters for the method 'ListEventType'
    50  type ListEventTypeParams struct {
    51  	// A string parameter filtering the results to return only the Event Types using a given schema.
    52  	SchemaId *string `json:"SchemaId,omitempty"`
    53  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    54  	PageSize *int `json:"PageSize,omitempty"`
    55  	// Max number of records to return.
    56  	Limit *int `json:"limit,omitempty"`
    57  }
    58  
    59  func (params *ListEventTypeParams) SetSchemaId(SchemaId string) *ListEventTypeParams {
    60  	params.SchemaId = &SchemaId
    61  	return params
    62  }
    63  func (params *ListEventTypeParams) SetPageSize(PageSize int) *ListEventTypeParams {
    64  	params.PageSize = &PageSize
    65  	return params
    66  }
    67  func (params *ListEventTypeParams) SetLimit(Limit int) *ListEventTypeParams {
    68  	params.Limit = &Limit
    69  	return params
    70  }
    71  
    72  // Retrieve a single page of EventType records from the API. Request is executed immediately.
    73  func (c *ApiService) PageEventType(params *ListEventTypeParams, pageToken, pageNumber string) (*ListEventTypeResponse, error) {
    74  	path := "/v1/Types"
    75  
    76  	data := url.Values{}
    77  	headers := make(map[string]interface{})
    78  
    79  	if params != nil && params.SchemaId != nil {
    80  		data.Set("SchemaId", *params.SchemaId)
    81  	}
    82  	if params != nil && params.PageSize != nil {
    83  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
    84  	}
    85  
    86  	if pageToken != "" {
    87  		data.Set("PageToken", pageToken)
    88  	}
    89  	if pageNumber != "" {
    90  		data.Set("Page", pageNumber)
    91  	}
    92  
    93  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	defer resp.Body.Close()
    99  
   100  	ps := &ListEventTypeResponse{}
   101  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	return ps, err
   106  }
   107  
   108  // Lists EventType records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   109  func (c *ApiService) ListEventType(params *ListEventTypeParams) ([]EventsV1EventType, error) {
   110  	response, errors := c.StreamEventType(params)
   111  
   112  	records := make([]EventsV1EventType, 0)
   113  	for record := range response {
   114  		records = append(records, record)
   115  	}
   116  
   117  	if err := <-errors; err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	return records, nil
   122  }
   123  
   124  // Streams EventType records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   125  func (c *ApiService) StreamEventType(params *ListEventTypeParams) (chan EventsV1EventType, chan error) {
   126  	if params == nil {
   127  		params = &ListEventTypeParams{}
   128  	}
   129  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   130  
   131  	recordChannel := make(chan EventsV1EventType, 1)
   132  	errorChannel := make(chan error, 1)
   133  
   134  	response, err := c.PageEventType(params, "", "")
   135  	if err != nil {
   136  		errorChannel <- err
   137  		close(recordChannel)
   138  		close(errorChannel)
   139  	} else {
   140  		go c.streamEventType(response, params, recordChannel, errorChannel)
   141  	}
   142  
   143  	return recordChannel, errorChannel
   144  }
   145  
   146  func (c *ApiService) streamEventType(response *ListEventTypeResponse, params *ListEventTypeParams, recordChannel chan EventsV1EventType, errorChannel chan error) {
   147  	curRecord := 1
   148  
   149  	for response != nil {
   150  		responseRecords := response.Types
   151  		for item := range responseRecords {
   152  			recordChannel <- responseRecords[item]
   153  			curRecord += 1
   154  			if params.Limit != nil && *params.Limit < curRecord {
   155  				close(recordChannel)
   156  				close(errorChannel)
   157  				return
   158  			}
   159  		}
   160  
   161  		record, err := client.GetNext(c.baseURL, response, c.getNextListEventTypeResponse)
   162  		if err != nil {
   163  			errorChannel <- err
   164  			break
   165  		} else if record == nil {
   166  			break
   167  		}
   168  
   169  		response = record.(*ListEventTypeResponse)
   170  	}
   171  
   172  	close(recordChannel)
   173  	close(errorChannel)
   174  }
   175  
   176  func (c *ApiService) getNextListEventTypeResponse(nextPageUrl string) (interface{}, error) {
   177  	if nextPageUrl == "" {
   178  		return nil, nil
   179  	}
   180  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   181  	if err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	defer resp.Body.Close()
   186  
   187  	ps := &ListEventTypeResponse{}
   188  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   189  		return nil, err
   190  	}
   191  	return ps, nil
   192  }