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

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