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

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