github.com/twilio/twilio-go@v1.20.1/rest/bulkexports/v1/exports_jobs.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Bulkexports
     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 'CreateExportCustomJob'
    27  type CreateExportCustomJobParams struct {
    28  	// The start day for the custom export specified as a string in the format of yyyy-mm-dd
    29  	StartDay *string `json:"StartDay,omitempty"`
    30  	// The end day for the custom export specified as a string in the format of yyyy-mm-dd. End day is inclusive and must be 2 days earlier than the current UTC day.
    31  	EndDay *string `json:"EndDay,omitempty"`
    32  	// The friendly name specified when creating the job
    33  	FriendlyName *string `json:"FriendlyName,omitempty"`
    34  	// The optional webhook url called on completion of the job. If this is supplied, `WebhookMethod` must also be supplied. If you set neither webhook nor email, you will have to check your job's status manually.
    35  	WebhookUrl *string `json:"WebhookUrl,omitempty"`
    36  	// This is the method used to call the webhook on completion of the job. If this is supplied, `WebhookUrl` must also be supplied.
    37  	WebhookMethod *string `json:"WebhookMethod,omitempty"`
    38  	// The optional email to send the completion notification to. You can set both webhook, and email, or one or the other. If you set neither, the job will run but you will have to query to determine your job's status.
    39  	Email *string `json:"Email,omitempty"`
    40  }
    41  
    42  func (params *CreateExportCustomJobParams) SetStartDay(StartDay string) *CreateExportCustomJobParams {
    43  	params.StartDay = &StartDay
    44  	return params
    45  }
    46  func (params *CreateExportCustomJobParams) SetEndDay(EndDay string) *CreateExportCustomJobParams {
    47  	params.EndDay = &EndDay
    48  	return params
    49  }
    50  func (params *CreateExportCustomJobParams) SetFriendlyName(FriendlyName string) *CreateExportCustomJobParams {
    51  	params.FriendlyName = &FriendlyName
    52  	return params
    53  }
    54  func (params *CreateExportCustomJobParams) SetWebhookUrl(WebhookUrl string) *CreateExportCustomJobParams {
    55  	params.WebhookUrl = &WebhookUrl
    56  	return params
    57  }
    58  func (params *CreateExportCustomJobParams) SetWebhookMethod(WebhookMethod string) *CreateExportCustomJobParams {
    59  	params.WebhookMethod = &WebhookMethod
    60  	return params
    61  }
    62  func (params *CreateExportCustomJobParams) SetEmail(Email string) *CreateExportCustomJobParams {
    63  	params.Email = &Email
    64  	return params
    65  }
    66  
    67  //
    68  func (c *ApiService) CreateExportCustomJob(ResourceType string, params *CreateExportCustomJobParams) (*BulkexportsV1ExportCustomJob, error) {
    69  	path := "/v1/Exports/{ResourceType}/Jobs"
    70  	path = strings.Replace(path, "{"+"ResourceType"+"}", ResourceType, -1)
    71  
    72  	data := url.Values{}
    73  	headers := make(map[string]interface{})
    74  
    75  	if params != nil && params.StartDay != nil {
    76  		data.Set("StartDay", *params.StartDay)
    77  	}
    78  	if params != nil && params.EndDay != nil {
    79  		data.Set("EndDay", *params.EndDay)
    80  	}
    81  	if params != nil && params.FriendlyName != nil {
    82  		data.Set("FriendlyName", *params.FriendlyName)
    83  	}
    84  	if params != nil && params.WebhookUrl != nil {
    85  		data.Set("WebhookUrl", *params.WebhookUrl)
    86  	}
    87  	if params != nil && params.WebhookMethod != nil {
    88  		data.Set("WebhookMethod", *params.WebhookMethod)
    89  	}
    90  	if params != nil && params.Email != nil {
    91  		data.Set("Email", *params.Email)
    92  	}
    93  
    94  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	defer resp.Body.Close()
   100  
   101  	ps := &BulkexportsV1ExportCustomJob{}
   102  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	return ps, err
   107  }
   108  
   109  //
   110  func (c *ApiService) DeleteJob(JobSid string) error {
   111  	path := "/v1/Exports/Jobs/{JobSid}"
   112  	path = strings.Replace(path, "{"+"JobSid"+"}", JobSid, -1)
   113  
   114  	data := url.Values{}
   115  	headers := make(map[string]interface{})
   116  
   117  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   118  	if err != nil {
   119  		return err
   120  	}
   121  
   122  	defer resp.Body.Close()
   123  
   124  	return nil
   125  }
   126  
   127  //
   128  func (c *ApiService) FetchJob(JobSid string) (*BulkexportsV1Job, error) {
   129  	path := "/v1/Exports/Jobs/{JobSid}"
   130  	path = strings.Replace(path, "{"+"JobSid"+"}", JobSid, -1)
   131  
   132  	data := url.Values{}
   133  	headers := make(map[string]interface{})
   134  
   135  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   136  	if err != nil {
   137  		return nil, err
   138  	}
   139  
   140  	defer resp.Body.Close()
   141  
   142  	ps := &BulkexportsV1Job{}
   143  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	return ps, err
   148  }
   149  
   150  // Optional parameters for the method 'ListExportCustomJob'
   151  type ListExportCustomJobParams struct {
   152  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   153  	PageSize *int `json:"PageSize,omitempty"`
   154  	// Max number of records to return.
   155  	Limit *int `json:"limit,omitempty"`
   156  }
   157  
   158  func (params *ListExportCustomJobParams) SetPageSize(PageSize int) *ListExportCustomJobParams {
   159  	params.PageSize = &PageSize
   160  	return params
   161  }
   162  func (params *ListExportCustomJobParams) SetLimit(Limit int) *ListExportCustomJobParams {
   163  	params.Limit = &Limit
   164  	return params
   165  }
   166  
   167  // Retrieve a single page of ExportCustomJob records from the API. Request is executed immediately.
   168  func (c *ApiService) PageExportCustomJob(ResourceType string, params *ListExportCustomJobParams, pageToken, pageNumber string) (*ListExportCustomJobResponse, error) {
   169  	path := "/v1/Exports/{ResourceType}/Jobs"
   170  
   171  	path = strings.Replace(path, "{"+"ResourceType"+"}", ResourceType, -1)
   172  
   173  	data := url.Values{}
   174  	headers := make(map[string]interface{})
   175  
   176  	if params != nil && params.PageSize != nil {
   177  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   178  	}
   179  
   180  	if pageToken != "" {
   181  		data.Set("PageToken", pageToken)
   182  	}
   183  	if pageNumber != "" {
   184  		data.Set("Page", pageNumber)
   185  	}
   186  
   187  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   188  	if err != nil {
   189  		return nil, err
   190  	}
   191  
   192  	defer resp.Body.Close()
   193  
   194  	ps := &ListExportCustomJobResponse{}
   195  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   196  		return nil, err
   197  	}
   198  
   199  	return ps, err
   200  }
   201  
   202  // Lists ExportCustomJob records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   203  func (c *ApiService) ListExportCustomJob(ResourceType string, params *ListExportCustomJobParams) ([]BulkexportsV1ExportCustomJob, error) {
   204  	response, errors := c.StreamExportCustomJob(ResourceType, params)
   205  
   206  	records := make([]BulkexportsV1ExportCustomJob, 0)
   207  	for record := range response {
   208  		records = append(records, record)
   209  	}
   210  
   211  	if err := <-errors; err != nil {
   212  		return nil, err
   213  	}
   214  
   215  	return records, nil
   216  }
   217  
   218  // Streams ExportCustomJob records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   219  func (c *ApiService) StreamExportCustomJob(ResourceType string, params *ListExportCustomJobParams) (chan BulkexportsV1ExportCustomJob, chan error) {
   220  	if params == nil {
   221  		params = &ListExportCustomJobParams{}
   222  	}
   223  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   224  
   225  	recordChannel := make(chan BulkexportsV1ExportCustomJob, 1)
   226  	errorChannel := make(chan error, 1)
   227  
   228  	response, err := c.PageExportCustomJob(ResourceType, params, "", "")
   229  	if err != nil {
   230  		errorChannel <- err
   231  		close(recordChannel)
   232  		close(errorChannel)
   233  	} else {
   234  		go c.streamExportCustomJob(response, params, recordChannel, errorChannel)
   235  	}
   236  
   237  	return recordChannel, errorChannel
   238  }
   239  
   240  func (c *ApiService) streamExportCustomJob(response *ListExportCustomJobResponse, params *ListExportCustomJobParams, recordChannel chan BulkexportsV1ExportCustomJob, errorChannel chan error) {
   241  	curRecord := 1
   242  
   243  	for response != nil {
   244  		responseRecords := response.Jobs
   245  		for item := range responseRecords {
   246  			recordChannel <- responseRecords[item]
   247  			curRecord += 1
   248  			if params.Limit != nil && *params.Limit < curRecord {
   249  				close(recordChannel)
   250  				close(errorChannel)
   251  				return
   252  			}
   253  		}
   254  
   255  		record, err := client.GetNext(c.baseURL, response, c.getNextListExportCustomJobResponse)
   256  		if err != nil {
   257  			errorChannel <- err
   258  			break
   259  		} else if record == nil {
   260  			break
   261  		}
   262  
   263  		response = record.(*ListExportCustomJobResponse)
   264  	}
   265  
   266  	close(recordChannel)
   267  	close(errorChannel)
   268  }
   269  
   270  func (c *ApiService) getNextListExportCustomJobResponse(nextPageUrl string) (interface{}, error) {
   271  	if nextPageUrl == "" {
   272  		return nil, nil
   273  	}
   274  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   275  	if err != nil {
   276  		return nil, err
   277  	}
   278  
   279  	defer resp.Body.Close()
   280  
   281  	ps := &ListExportCustomJobResponse{}
   282  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   283  		return nil, err
   284  	}
   285  	return ps, nil
   286  }