github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_calls_notifications.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Api
     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 'FetchCallNotification'
    27  type FetchCallNotificationParams struct {
    28  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Call Notification resource to fetch.
    29  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    30  }
    31  
    32  func (params *FetchCallNotificationParams) SetPathAccountSid(PathAccountSid string) *FetchCallNotificationParams {
    33  	params.PathAccountSid = &PathAccountSid
    34  	return params
    35  }
    36  
    37  //
    38  func (c *ApiService) FetchCallNotification(CallSid string, Sid string, params *FetchCallNotificationParams) (*ApiV2010CallNotificationInstance, error) {
    39  	path := "/2010-04-01/Accounts/{AccountSid}/Calls/{CallSid}/Notifications/{Sid}.json"
    40  	if params != nil && params.PathAccountSid != nil {
    41  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    42  	} else {
    43  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    44  	}
    45  	path = strings.Replace(path, "{"+"CallSid"+"}", CallSid, -1)
    46  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    47  
    48  	data := url.Values{}
    49  	headers := make(map[string]interface{})
    50  
    51  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	defer resp.Body.Close()
    57  
    58  	ps := &ApiV2010CallNotificationInstance{}
    59  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	return ps, err
    64  }
    65  
    66  // Optional parameters for the method 'ListCallNotification'
    67  type ListCallNotificationParams struct {
    68  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Call Notification resources to read.
    69  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    70  	// Only read notifications of the specified log level. Can be:  `0` to read only ERROR notifications or `1` to read only WARNING notifications. By default, all notifications are read.
    71  	Log *int `json:"Log,omitempty"`
    72  	// Only show notifications for the specified date, formatted as `YYYY-MM-DD`. You can also specify an inequality, such as `<=YYYY-MM-DD` for messages logged at or before midnight on a date, or `>=YYYY-MM-DD` for messages logged at or after midnight on a date.
    73  	MessageDate *string `json:"MessageDate,omitempty"`
    74  	// Only show notifications for the specified date, formatted as `YYYY-MM-DD`. You can also specify an inequality, such as `<=YYYY-MM-DD` for messages logged at or before midnight on a date, or `>=YYYY-MM-DD` for messages logged at or after midnight on a date.
    75  	MessageDateBefore *string `json:"MessageDate&lt;,omitempty"`
    76  	// Only show notifications for the specified date, formatted as `YYYY-MM-DD`. You can also specify an inequality, such as `<=YYYY-MM-DD` for messages logged at or before midnight on a date, or `>=YYYY-MM-DD` for messages logged at or after midnight on a date.
    77  	MessageDateAfter *string `json:"MessageDate&gt;,omitempty"`
    78  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    79  	PageSize *int `json:"PageSize,omitempty"`
    80  	// Max number of records to return.
    81  	Limit *int `json:"limit,omitempty"`
    82  }
    83  
    84  func (params *ListCallNotificationParams) SetPathAccountSid(PathAccountSid string) *ListCallNotificationParams {
    85  	params.PathAccountSid = &PathAccountSid
    86  	return params
    87  }
    88  func (params *ListCallNotificationParams) SetLog(Log int) *ListCallNotificationParams {
    89  	params.Log = &Log
    90  	return params
    91  }
    92  func (params *ListCallNotificationParams) SetMessageDate(MessageDate string) *ListCallNotificationParams {
    93  	params.MessageDate = &MessageDate
    94  	return params
    95  }
    96  func (params *ListCallNotificationParams) SetMessageDateBefore(MessageDateBefore string) *ListCallNotificationParams {
    97  	params.MessageDateBefore = &MessageDateBefore
    98  	return params
    99  }
   100  func (params *ListCallNotificationParams) SetMessageDateAfter(MessageDateAfter string) *ListCallNotificationParams {
   101  	params.MessageDateAfter = &MessageDateAfter
   102  	return params
   103  }
   104  func (params *ListCallNotificationParams) SetPageSize(PageSize int) *ListCallNotificationParams {
   105  	params.PageSize = &PageSize
   106  	return params
   107  }
   108  func (params *ListCallNotificationParams) SetLimit(Limit int) *ListCallNotificationParams {
   109  	params.Limit = &Limit
   110  	return params
   111  }
   112  
   113  // Retrieve a single page of CallNotification records from the API. Request is executed immediately.
   114  func (c *ApiService) PageCallNotification(CallSid string, params *ListCallNotificationParams, pageToken, pageNumber string) (*ListCallNotificationResponse, error) {
   115  	path := "/2010-04-01/Accounts/{AccountSid}/Calls/{CallSid}/Notifications.json"
   116  
   117  	if params != nil && params.PathAccountSid != nil {
   118  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   119  	} else {
   120  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   121  	}
   122  	path = strings.Replace(path, "{"+"CallSid"+"}", CallSid, -1)
   123  
   124  	data := url.Values{}
   125  	headers := make(map[string]interface{})
   126  
   127  	if params != nil && params.Log != nil {
   128  		data.Set("Log", fmt.Sprint(*params.Log))
   129  	}
   130  	if params != nil && params.MessageDate != nil {
   131  		data.Set("MessageDate", fmt.Sprint(*params.MessageDate))
   132  	}
   133  	if params != nil && params.MessageDateBefore != nil {
   134  		data.Set("MessageDate<", fmt.Sprint(*params.MessageDateBefore))
   135  	}
   136  	if params != nil && params.MessageDateAfter != nil {
   137  		data.Set("MessageDate>", fmt.Sprint(*params.MessageDateAfter))
   138  	}
   139  	if params != nil && params.PageSize != nil {
   140  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   141  	}
   142  
   143  	if pageToken != "" {
   144  		data.Set("PageToken", pageToken)
   145  	}
   146  	if pageNumber != "" {
   147  		data.Set("Page", pageNumber)
   148  	}
   149  
   150  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	defer resp.Body.Close()
   156  
   157  	ps := &ListCallNotificationResponse{}
   158  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   159  		return nil, err
   160  	}
   161  
   162  	return ps, err
   163  }
   164  
   165  // Lists CallNotification records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   166  func (c *ApiService) ListCallNotification(CallSid string, params *ListCallNotificationParams) ([]ApiV2010CallNotification, error) {
   167  	response, errors := c.StreamCallNotification(CallSid, params)
   168  
   169  	records := make([]ApiV2010CallNotification, 0)
   170  	for record := range response {
   171  		records = append(records, record)
   172  	}
   173  
   174  	if err := <-errors; err != nil {
   175  		return nil, err
   176  	}
   177  
   178  	return records, nil
   179  }
   180  
   181  // Streams CallNotification records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   182  func (c *ApiService) StreamCallNotification(CallSid string, params *ListCallNotificationParams) (chan ApiV2010CallNotification, chan error) {
   183  	if params == nil {
   184  		params = &ListCallNotificationParams{}
   185  	}
   186  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   187  
   188  	recordChannel := make(chan ApiV2010CallNotification, 1)
   189  	errorChannel := make(chan error, 1)
   190  
   191  	response, err := c.PageCallNotification(CallSid, params, "", "")
   192  	if err != nil {
   193  		errorChannel <- err
   194  		close(recordChannel)
   195  		close(errorChannel)
   196  	} else {
   197  		go c.streamCallNotification(response, params, recordChannel, errorChannel)
   198  	}
   199  
   200  	return recordChannel, errorChannel
   201  }
   202  
   203  func (c *ApiService) streamCallNotification(response *ListCallNotificationResponse, params *ListCallNotificationParams, recordChannel chan ApiV2010CallNotification, errorChannel chan error) {
   204  	curRecord := 1
   205  
   206  	for response != nil {
   207  		responseRecords := response.Notifications
   208  		for item := range responseRecords {
   209  			recordChannel <- responseRecords[item]
   210  			curRecord += 1
   211  			if params.Limit != nil && *params.Limit < curRecord {
   212  				close(recordChannel)
   213  				close(errorChannel)
   214  				return
   215  			}
   216  		}
   217  
   218  		record, err := client.GetNext(c.baseURL, response, c.getNextListCallNotificationResponse)
   219  		if err != nil {
   220  			errorChannel <- err
   221  			break
   222  		} else if record == nil {
   223  			break
   224  		}
   225  
   226  		response = record.(*ListCallNotificationResponse)
   227  	}
   228  
   229  	close(recordChannel)
   230  	close(errorChannel)
   231  }
   232  
   233  func (c *ApiService) getNextListCallNotificationResponse(nextPageUrl string) (interface{}, error) {
   234  	if nextPageUrl == "" {
   235  		return nil, nil
   236  	}
   237  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   238  	if err != nil {
   239  		return nil, err
   240  	}
   241  
   242  	defer resp.Body.Close()
   243  
   244  	ps := &ListCallNotificationResponse{}
   245  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   246  		return nil, err
   247  	}
   248  	return ps, nil
   249  }