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

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Content
     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 'CreateContent'
    27  type CreateContentParams struct {
    28  	//
    29  	ContentCreateRequest *ContentCreateRequest `json:"ContentCreateRequest,omitempty"`
    30  }
    31  
    32  func (params *CreateContentParams) SetContentCreateRequest(ContentCreateRequest ContentCreateRequest) *CreateContentParams {
    33  	params.ContentCreateRequest = &ContentCreateRequest
    34  	return params
    35  }
    36  
    37  // Create a Content resource
    38  func (c *ApiService) CreateContent(params *CreateContentParams) (*ContentV1Content, error) {
    39  	path := "/v1/Content"
    40  
    41  	data := url.Values{}
    42  	headers := map[string]interface{}{
    43  		"Content-Type": "application/json",
    44  	}
    45  
    46  	body := []byte{}
    47  	if params != nil && params.ContentCreateRequest != nil {
    48  		b, err := json.Marshal(*params.ContentCreateRequest)
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  		body = b
    53  	}
    54  
    55  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers, body...)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	defer resp.Body.Close()
    61  
    62  	ps := &ContentV1Content{}
    63  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	return ps, err
    68  }
    69  
    70  // Deletes a Content resource
    71  func (c *ApiService) DeleteContent(Sid string) error {
    72  	path := "/v1/Content/{Sid}"
    73  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    74  
    75  	data := url.Values{}
    76  	headers := make(map[string]interface{})
    77  
    78  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	defer resp.Body.Close()
    84  
    85  	return nil
    86  }
    87  
    88  // Fetch a Content resource by its unique Content Sid
    89  func (c *ApiService) FetchContent(Sid string) (*ContentV1Content, error) {
    90  	path := "/v1/Content/{Sid}"
    91  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    92  
    93  	data := url.Values{}
    94  	headers := make(map[string]interface{})
    95  
    96  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  
   101  	defer resp.Body.Close()
   102  
   103  	ps := &ContentV1Content{}
   104  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	return ps, err
   109  }
   110  
   111  // Optional parameters for the method 'ListContent'
   112  type ListContentParams struct {
   113  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   114  	PageSize *int `json:"PageSize,omitempty"`
   115  	// Max number of records to return.
   116  	Limit *int `json:"limit,omitempty"`
   117  }
   118  
   119  func (params *ListContentParams) SetPageSize(PageSize int) *ListContentParams {
   120  	params.PageSize = &PageSize
   121  	return params
   122  }
   123  func (params *ListContentParams) SetLimit(Limit int) *ListContentParams {
   124  	params.Limit = &Limit
   125  	return params
   126  }
   127  
   128  // Retrieve a single page of Content records from the API. Request is executed immediately.
   129  func (c *ApiService) PageContent(params *ListContentParams, pageToken, pageNumber string) (*ListContentResponse, error) {
   130  	path := "/v1/Content"
   131  
   132  	data := url.Values{}
   133  	headers := make(map[string]interface{})
   134  
   135  	if params != nil && params.PageSize != nil {
   136  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   137  	}
   138  
   139  	if pageToken != "" {
   140  		data.Set("PageToken", pageToken)
   141  	}
   142  	if pageNumber != "" {
   143  		data.Set("Page", pageNumber)
   144  	}
   145  
   146  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	defer resp.Body.Close()
   152  
   153  	ps := &ListContentResponse{}
   154  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   155  		return nil, err
   156  	}
   157  
   158  	return ps, err
   159  }
   160  
   161  // Lists Content records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   162  func (c *ApiService) ListContent(params *ListContentParams) ([]ContentV1Content, error) {
   163  	response, errors := c.StreamContent(params)
   164  
   165  	records := make([]ContentV1Content, 0)
   166  	for record := range response {
   167  		records = append(records, record)
   168  	}
   169  
   170  	if err := <-errors; err != nil {
   171  		return nil, err
   172  	}
   173  
   174  	return records, nil
   175  }
   176  
   177  // Streams Content records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   178  func (c *ApiService) StreamContent(params *ListContentParams) (chan ContentV1Content, chan error) {
   179  	if params == nil {
   180  		params = &ListContentParams{}
   181  	}
   182  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   183  
   184  	recordChannel := make(chan ContentV1Content, 1)
   185  	errorChannel := make(chan error, 1)
   186  
   187  	response, err := c.PageContent(params, "", "")
   188  	if err != nil {
   189  		errorChannel <- err
   190  		close(recordChannel)
   191  		close(errorChannel)
   192  	} else {
   193  		go c.streamContent(response, params, recordChannel, errorChannel)
   194  	}
   195  
   196  	return recordChannel, errorChannel
   197  }
   198  
   199  func (c *ApiService) streamContent(response *ListContentResponse, params *ListContentParams, recordChannel chan ContentV1Content, errorChannel chan error) {
   200  	curRecord := 1
   201  
   202  	for response != nil {
   203  		responseRecords := response.Contents
   204  		for item := range responseRecords {
   205  			recordChannel <- responseRecords[item]
   206  			curRecord += 1
   207  			if params.Limit != nil && *params.Limit < curRecord {
   208  				close(recordChannel)
   209  				close(errorChannel)
   210  				return
   211  			}
   212  		}
   213  
   214  		record, err := client.GetNext(c.baseURL, response, c.getNextListContentResponse)
   215  		if err != nil {
   216  			errorChannel <- err
   217  			break
   218  		} else if record == nil {
   219  			break
   220  		}
   221  
   222  		response = record.(*ListContentResponse)
   223  	}
   224  
   225  	close(recordChannel)
   226  	close(errorChannel)
   227  }
   228  
   229  func (c *ApiService) getNextListContentResponse(nextPageUrl string) (interface{}, error) {
   230  	if nextPageUrl == "" {
   231  		return nil, nil
   232  	}
   233  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   234  	if err != nil {
   235  		return nil, err
   236  	}
   237  
   238  	defer resp.Body.Close()
   239  
   240  	ps := &ListContentResponse{}
   241  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   242  		return nil, err
   243  	}
   244  	return ps, nil
   245  }