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