github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_messages_media.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  	"time"
    23  
    24  	"github.com/twilio/twilio-go/client"
    25  )
    26  
    27  // Optional parameters for the method 'DeleteMedia'
    28  type DeleteMediaParams struct {
    29  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that is associated with the Media resource.
    30  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    31  }
    32  
    33  func (params *DeleteMediaParams) SetPathAccountSid(PathAccountSid string) *DeleteMediaParams {
    34  	params.PathAccountSid = &PathAccountSid
    35  	return params
    36  }
    37  
    38  // Delete the Media resource.
    39  func (c *ApiService) DeleteMedia(MessageSid string, Sid string, params *DeleteMediaParams) error {
    40  	path := "/2010-04-01/Accounts/{AccountSid}/Messages/{MessageSid}/Media/{Sid}.json"
    41  	if params != nil && params.PathAccountSid != nil {
    42  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    43  	} else {
    44  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    45  	}
    46  	path = strings.Replace(path, "{"+"MessageSid"+"}", MessageSid, -1)
    47  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    48  
    49  	data := url.Values{}
    50  	headers := make(map[string]interface{})
    51  
    52  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	defer resp.Body.Close()
    58  
    59  	return nil
    60  }
    61  
    62  // Optional parameters for the method 'FetchMedia'
    63  type FetchMediaParams struct {
    64  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) associated with the Media resource.
    65  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    66  }
    67  
    68  func (params *FetchMediaParams) SetPathAccountSid(PathAccountSid string) *FetchMediaParams {
    69  	params.PathAccountSid = &PathAccountSid
    70  	return params
    71  }
    72  
    73  // Fetch a single Media resource associated with a specific Message resource
    74  func (c *ApiService) FetchMedia(MessageSid string, Sid string, params *FetchMediaParams) (*ApiV2010Media, error) {
    75  	path := "/2010-04-01/Accounts/{AccountSid}/Messages/{MessageSid}/Media/{Sid}.json"
    76  	if params != nil && params.PathAccountSid != nil {
    77  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    78  	} else {
    79  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    80  	}
    81  	path = strings.Replace(path, "{"+"MessageSid"+"}", MessageSid, -1)
    82  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    83  
    84  	data := url.Values{}
    85  	headers := make(map[string]interface{})
    86  
    87  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	defer resp.Body.Close()
    93  
    94  	ps := &ApiV2010Media{}
    95  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	return ps, err
   100  }
   101  
   102  // Optional parameters for the method 'ListMedia'
   103  type ListMediaParams struct {
   104  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that is associated with the Media resources.
   105  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   106  	// Only include Media resources that were created on this date. Specify a date as `YYYY-MM-DD` in GMT, for example: `2009-07-06`, to read Media that were created on this date. You can also specify an inequality, such as `StartTime<=YYYY-MM-DD`, to read Media that were created on or before midnight of this date, and `StartTime>=YYYY-MM-DD` to read Media that were created on or after midnight of this date.
   107  	DateCreated *time.Time `json:"DateCreated,omitempty"`
   108  	// Only include Media resources that were created on this date. Specify a date as `YYYY-MM-DD` in GMT, for example: `2009-07-06`, to read Media that were created on this date. You can also specify an inequality, such as `StartTime<=YYYY-MM-DD`, to read Media that were created on or before midnight of this date, and `StartTime>=YYYY-MM-DD` to read Media that were created on or after midnight of this date.
   109  	DateCreatedBefore *time.Time `json:"DateCreated&lt;,omitempty"`
   110  	// Only include Media resources that were created on this date. Specify a date as `YYYY-MM-DD` in GMT, for example: `2009-07-06`, to read Media that were created on this date. You can also specify an inequality, such as `StartTime<=YYYY-MM-DD`, to read Media that were created on or before midnight of this date, and `StartTime>=YYYY-MM-DD` to read Media that were created on or after midnight of this date.
   111  	DateCreatedAfter *time.Time `json:"DateCreated&gt;,omitempty"`
   112  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   113  	PageSize *int `json:"PageSize,omitempty"`
   114  	// Max number of records to return.
   115  	Limit *int `json:"limit,omitempty"`
   116  }
   117  
   118  func (params *ListMediaParams) SetPathAccountSid(PathAccountSid string) *ListMediaParams {
   119  	params.PathAccountSid = &PathAccountSid
   120  	return params
   121  }
   122  func (params *ListMediaParams) SetDateCreated(DateCreated time.Time) *ListMediaParams {
   123  	params.DateCreated = &DateCreated
   124  	return params
   125  }
   126  func (params *ListMediaParams) SetDateCreatedBefore(DateCreatedBefore time.Time) *ListMediaParams {
   127  	params.DateCreatedBefore = &DateCreatedBefore
   128  	return params
   129  }
   130  func (params *ListMediaParams) SetDateCreatedAfter(DateCreatedAfter time.Time) *ListMediaParams {
   131  	params.DateCreatedAfter = &DateCreatedAfter
   132  	return params
   133  }
   134  func (params *ListMediaParams) SetPageSize(PageSize int) *ListMediaParams {
   135  	params.PageSize = &PageSize
   136  	return params
   137  }
   138  func (params *ListMediaParams) SetLimit(Limit int) *ListMediaParams {
   139  	params.Limit = &Limit
   140  	return params
   141  }
   142  
   143  // Retrieve a single page of Media records from the API. Request is executed immediately.
   144  func (c *ApiService) PageMedia(MessageSid string, params *ListMediaParams, pageToken, pageNumber string) (*ListMediaResponse, error) {
   145  	path := "/2010-04-01/Accounts/{AccountSid}/Messages/{MessageSid}/Media.json"
   146  
   147  	if params != nil && params.PathAccountSid != nil {
   148  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   149  	} else {
   150  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   151  	}
   152  	path = strings.Replace(path, "{"+"MessageSid"+"}", MessageSid, -1)
   153  
   154  	data := url.Values{}
   155  	headers := make(map[string]interface{})
   156  
   157  	if params != nil && params.DateCreated != nil {
   158  		data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339)))
   159  	}
   160  	if params != nil && params.DateCreatedBefore != nil {
   161  		data.Set("DateCreated<", fmt.Sprint((*params.DateCreatedBefore).Format(time.RFC3339)))
   162  	}
   163  	if params != nil && params.DateCreatedAfter != nil {
   164  		data.Set("DateCreated>", fmt.Sprint((*params.DateCreatedAfter).Format(time.RFC3339)))
   165  	}
   166  	if params != nil && params.PageSize != nil {
   167  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   168  	}
   169  
   170  	if pageToken != "" {
   171  		data.Set("PageToken", pageToken)
   172  	}
   173  	if pageNumber != "" {
   174  		data.Set("Page", pageNumber)
   175  	}
   176  
   177  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   178  	if err != nil {
   179  		return nil, err
   180  	}
   181  
   182  	defer resp.Body.Close()
   183  
   184  	ps := &ListMediaResponse{}
   185  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   186  		return nil, err
   187  	}
   188  
   189  	return ps, err
   190  }
   191  
   192  // Lists Media records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   193  func (c *ApiService) ListMedia(MessageSid string, params *ListMediaParams) ([]ApiV2010Media, error) {
   194  	response, errors := c.StreamMedia(MessageSid, params)
   195  
   196  	records := make([]ApiV2010Media, 0)
   197  	for record := range response {
   198  		records = append(records, record)
   199  	}
   200  
   201  	if err := <-errors; err != nil {
   202  		return nil, err
   203  	}
   204  
   205  	return records, nil
   206  }
   207  
   208  // Streams Media records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   209  func (c *ApiService) StreamMedia(MessageSid string, params *ListMediaParams) (chan ApiV2010Media, chan error) {
   210  	if params == nil {
   211  		params = &ListMediaParams{}
   212  	}
   213  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   214  
   215  	recordChannel := make(chan ApiV2010Media, 1)
   216  	errorChannel := make(chan error, 1)
   217  
   218  	response, err := c.PageMedia(MessageSid, params, "", "")
   219  	if err != nil {
   220  		errorChannel <- err
   221  		close(recordChannel)
   222  		close(errorChannel)
   223  	} else {
   224  		go c.streamMedia(response, params, recordChannel, errorChannel)
   225  	}
   226  
   227  	return recordChannel, errorChannel
   228  }
   229  
   230  func (c *ApiService) streamMedia(response *ListMediaResponse, params *ListMediaParams, recordChannel chan ApiV2010Media, errorChannel chan error) {
   231  	curRecord := 1
   232  
   233  	for response != nil {
   234  		responseRecords := response.MediaList
   235  		for item := range responseRecords {
   236  			recordChannel <- responseRecords[item]
   237  			curRecord += 1
   238  			if params.Limit != nil && *params.Limit < curRecord {
   239  				close(recordChannel)
   240  				close(errorChannel)
   241  				return
   242  			}
   243  		}
   244  
   245  		record, err := client.GetNext(c.baseURL, response, c.getNextListMediaResponse)
   246  		if err != nil {
   247  			errorChannel <- err
   248  			break
   249  		} else if record == nil {
   250  			break
   251  		}
   252  
   253  		response = record.(*ListMediaResponse)
   254  	}
   255  
   256  	close(recordChannel)
   257  	close(errorChannel)
   258  }
   259  
   260  func (c *ApiService) getNextListMediaResponse(nextPageUrl string) (interface{}, error) {
   261  	if nextPageUrl == "" {
   262  		return nil, nil
   263  	}
   264  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   265  	if err != nil {
   266  		return nil, err
   267  	}
   268  
   269  	defer resp.Body.Close()
   270  
   271  	ps := &ListMediaResponse{}
   272  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   273  		return nil, err
   274  	}
   275  	return ps, nil
   276  }