github.com/twilio/twilio-go@v1.20.1/rest/serverless/v1/services_environments_logs.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Serverless
     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  // Retrieve a specific log.
    28  func (c *ApiService) FetchLog(ServiceSid string, EnvironmentSid string, Sid string) (*ServerlessV1Log, error) {
    29  	path := "/v1/Services/{ServiceSid}/Environments/{EnvironmentSid}/Logs/{Sid}"
    30  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    31  	path = strings.Replace(path, "{"+"EnvironmentSid"+"}", EnvironmentSid, -1)
    32  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    33  
    34  	data := url.Values{}
    35  	headers := make(map[string]interface{})
    36  
    37  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	defer resp.Body.Close()
    43  
    44  	ps := &ServerlessV1Log{}
    45  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	return ps, err
    50  }
    51  
    52  // Optional parameters for the method 'ListLog'
    53  type ListLogParams struct {
    54  	// The SID of the function whose invocation produced the Log resources to read.
    55  	FunctionSid *string `json:"FunctionSid,omitempty"`
    56  	// The date/time (in GMT, ISO 8601) after which the Log resources must have been created. Defaults to 1 day prior to current date/time.
    57  	StartDate *time.Time `json:"StartDate,omitempty"`
    58  	// The date/time (in GMT, ISO 8601) before which the Log resources must have been created. Defaults to current date/time.
    59  	EndDate *time.Time `json:"EndDate,omitempty"`
    60  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    61  	PageSize *int `json:"PageSize,omitempty"`
    62  	// Max number of records to return.
    63  	Limit *int `json:"limit,omitempty"`
    64  }
    65  
    66  func (params *ListLogParams) SetFunctionSid(FunctionSid string) *ListLogParams {
    67  	params.FunctionSid = &FunctionSid
    68  	return params
    69  }
    70  func (params *ListLogParams) SetStartDate(StartDate time.Time) *ListLogParams {
    71  	params.StartDate = &StartDate
    72  	return params
    73  }
    74  func (params *ListLogParams) SetEndDate(EndDate time.Time) *ListLogParams {
    75  	params.EndDate = &EndDate
    76  	return params
    77  }
    78  func (params *ListLogParams) SetPageSize(PageSize int) *ListLogParams {
    79  	params.PageSize = &PageSize
    80  	return params
    81  }
    82  func (params *ListLogParams) SetLimit(Limit int) *ListLogParams {
    83  	params.Limit = &Limit
    84  	return params
    85  }
    86  
    87  // Retrieve a single page of Log records from the API. Request is executed immediately.
    88  func (c *ApiService) PageLog(ServiceSid string, EnvironmentSid string, params *ListLogParams, pageToken, pageNumber string) (*ListLogResponse, error) {
    89  	path := "/v1/Services/{ServiceSid}/Environments/{EnvironmentSid}/Logs"
    90  
    91  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    92  	path = strings.Replace(path, "{"+"EnvironmentSid"+"}", EnvironmentSid, -1)
    93  
    94  	data := url.Values{}
    95  	headers := make(map[string]interface{})
    96  
    97  	if params != nil && params.FunctionSid != nil {
    98  		data.Set("FunctionSid", *params.FunctionSid)
    99  	}
   100  	if params != nil && params.StartDate != nil {
   101  		data.Set("StartDate", fmt.Sprint((*params.StartDate).Format(time.RFC3339)))
   102  	}
   103  	if params != nil && params.EndDate != nil {
   104  		data.Set("EndDate", fmt.Sprint((*params.EndDate).Format(time.RFC3339)))
   105  	}
   106  	if params != nil && params.PageSize != nil {
   107  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   108  	}
   109  
   110  	if pageToken != "" {
   111  		data.Set("PageToken", pageToken)
   112  	}
   113  	if pageNumber != "" {
   114  		data.Set("Page", pageNumber)
   115  	}
   116  
   117  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	defer resp.Body.Close()
   123  
   124  	ps := &ListLogResponse{}
   125  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	return ps, err
   130  }
   131  
   132  // Lists Log records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   133  func (c *ApiService) ListLog(ServiceSid string, EnvironmentSid string, params *ListLogParams) ([]ServerlessV1Log, error) {
   134  	response, errors := c.StreamLog(ServiceSid, EnvironmentSid, params)
   135  
   136  	records := make([]ServerlessV1Log, 0)
   137  	for record := range response {
   138  		records = append(records, record)
   139  	}
   140  
   141  	if err := <-errors; err != nil {
   142  		return nil, err
   143  	}
   144  
   145  	return records, nil
   146  }
   147  
   148  // Streams Log records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   149  func (c *ApiService) StreamLog(ServiceSid string, EnvironmentSid string, params *ListLogParams) (chan ServerlessV1Log, chan error) {
   150  	if params == nil {
   151  		params = &ListLogParams{}
   152  	}
   153  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   154  
   155  	recordChannel := make(chan ServerlessV1Log, 1)
   156  	errorChannel := make(chan error, 1)
   157  
   158  	response, err := c.PageLog(ServiceSid, EnvironmentSid, params, "", "")
   159  	if err != nil {
   160  		errorChannel <- err
   161  		close(recordChannel)
   162  		close(errorChannel)
   163  	} else {
   164  		go c.streamLog(response, params, recordChannel, errorChannel)
   165  	}
   166  
   167  	return recordChannel, errorChannel
   168  }
   169  
   170  func (c *ApiService) streamLog(response *ListLogResponse, params *ListLogParams, recordChannel chan ServerlessV1Log, errorChannel chan error) {
   171  	curRecord := 1
   172  
   173  	for response != nil {
   174  		responseRecords := response.Logs
   175  		for item := range responseRecords {
   176  			recordChannel <- responseRecords[item]
   177  			curRecord += 1
   178  			if params.Limit != nil && *params.Limit < curRecord {
   179  				close(recordChannel)
   180  				close(errorChannel)
   181  				return
   182  			}
   183  		}
   184  
   185  		record, err := client.GetNext(c.baseURL, response, c.getNextListLogResponse)
   186  		if err != nil {
   187  			errorChannel <- err
   188  			break
   189  		} else if record == nil {
   190  			break
   191  		}
   192  
   193  		response = record.(*ListLogResponse)
   194  	}
   195  
   196  	close(recordChannel)
   197  	close(errorChannel)
   198  }
   199  
   200  func (c *ApiService) getNextListLogResponse(nextPageUrl string) (interface{}, error) {
   201  	if nextPageUrl == "" {
   202  		return nil, nil
   203  	}
   204  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   205  	if err != nil {
   206  		return nil, err
   207  	}
   208  
   209  	defer resp.Body.Close()
   210  
   211  	ps := &ListLogResponse{}
   212  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   213  		return nil, err
   214  	}
   215  	return ps, nil
   216  }