github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_transcriptions.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 'DeleteTranscription'
    27  type DeleteTranscriptionParams struct {
    28  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Transcription resources to delete.
    29  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    30  }
    31  
    32  func (params *DeleteTranscriptionParams) SetPathAccountSid(PathAccountSid string) *DeleteTranscriptionParams {
    33  	params.PathAccountSid = &PathAccountSid
    34  	return params
    35  }
    36  
    37  // Delete a transcription from the account used to make the request
    38  func (c *ApiService) DeleteTranscription(Sid string, params *DeleteTranscriptionParams) error {
    39  	path := "/2010-04-01/Accounts/{AccountSid}/Transcriptions/{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.Delete(c.baseURL+path, data, headers)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	defer resp.Body.Close()
    56  
    57  	return nil
    58  }
    59  
    60  // Optional parameters for the method 'FetchTranscription'
    61  type FetchTranscriptionParams struct {
    62  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Transcription resource to fetch.
    63  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    64  }
    65  
    66  func (params *FetchTranscriptionParams) SetPathAccountSid(PathAccountSid string) *FetchTranscriptionParams {
    67  	params.PathAccountSid = &PathAccountSid
    68  	return params
    69  }
    70  
    71  // Fetch an instance of a Transcription
    72  func (c *ApiService) FetchTranscription(Sid string, params *FetchTranscriptionParams) (*ApiV2010Transcription, error) {
    73  	path := "/2010-04-01/Accounts/{AccountSid}/Transcriptions/{Sid}.json"
    74  	if params != nil && params.PathAccountSid != nil {
    75  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    76  	} else {
    77  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    78  	}
    79  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    80  
    81  	data := url.Values{}
    82  	headers := make(map[string]interface{})
    83  
    84  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	defer resp.Body.Close()
    90  
    91  	ps := &ApiV2010Transcription{}
    92  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	return ps, err
    97  }
    98  
    99  // Optional parameters for the method 'ListTranscription'
   100  type ListTranscriptionParams struct {
   101  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Transcription resources to read.
   102  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   103  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   104  	PageSize *int `json:"PageSize,omitempty"`
   105  	// Max number of records to return.
   106  	Limit *int `json:"limit,omitempty"`
   107  }
   108  
   109  func (params *ListTranscriptionParams) SetPathAccountSid(PathAccountSid string) *ListTranscriptionParams {
   110  	params.PathAccountSid = &PathAccountSid
   111  	return params
   112  }
   113  func (params *ListTranscriptionParams) SetPageSize(PageSize int) *ListTranscriptionParams {
   114  	params.PageSize = &PageSize
   115  	return params
   116  }
   117  func (params *ListTranscriptionParams) SetLimit(Limit int) *ListTranscriptionParams {
   118  	params.Limit = &Limit
   119  	return params
   120  }
   121  
   122  // Retrieve a single page of Transcription records from the API. Request is executed immediately.
   123  func (c *ApiService) PageTranscription(params *ListTranscriptionParams, pageToken, pageNumber string) (*ListTranscriptionResponse, error) {
   124  	path := "/2010-04-01/Accounts/{AccountSid}/Transcriptions.json"
   125  
   126  	if params != nil && params.PathAccountSid != nil {
   127  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   128  	} else {
   129  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   130  	}
   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 := &ListTranscriptionResponse{}
   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 Transcription 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) ListTranscription(params *ListTranscriptionParams) ([]ApiV2010Transcription, error) {
   163  	response, errors := c.StreamTranscription(params)
   164  
   165  	records := make([]ApiV2010Transcription, 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 Transcription 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) StreamTranscription(params *ListTranscriptionParams) (chan ApiV2010Transcription, chan error) {
   179  	if params == nil {
   180  		params = &ListTranscriptionParams{}
   181  	}
   182  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   183  
   184  	recordChannel := make(chan ApiV2010Transcription, 1)
   185  	errorChannel := make(chan error, 1)
   186  
   187  	response, err := c.PageTranscription(params, "", "")
   188  	if err != nil {
   189  		errorChannel <- err
   190  		close(recordChannel)
   191  		close(errorChannel)
   192  	} else {
   193  		go c.streamTranscription(response, params, recordChannel, errorChannel)
   194  	}
   195  
   196  	return recordChannel, errorChannel
   197  }
   198  
   199  func (c *ApiService) streamTranscription(response *ListTranscriptionResponse, params *ListTranscriptionParams, recordChannel chan ApiV2010Transcription, errorChannel chan error) {
   200  	curRecord := 1
   201  
   202  	for response != nil {
   203  		responseRecords := response.Transcriptions
   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.getNextListTranscriptionResponse)
   215  		if err != nil {
   216  			errorChannel <- err
   217  			break
   218  		} else if record == nil {
   219  			break
   220  		}
   221  
   222  		response = record.(*ListTranscriptionResponse)
   223  	}
   224  
   225  	close(recordChannel)
   226  	close(errorChannel)
   227  }
   228  
   229  func (c *ApiService) getNextListTranscriptionResponse(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 := &ListTranscriptionResponse{}
   241  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   242  		return nil, err
   243  	}
   244  	return ps, nil
   245  }