github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/users_conversations.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  	"time"
    23  
    24  	"github.com/twilio/twilio-go/client"
    25  )
    26  
    27  // Delete a specific User Conversation.
    28  func (c *ApiService) DeleteUserConversation(UserSid string, ConversationSid string) error {
    29  	path := "/v1/Users/{UserSid}/Conversations/{ConversationSid}"
    30  	path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1)
    31  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
    32  
    33  	data := url.Values{}
    34  	headers := make(map[string]interface{})
    35  
    36  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	defer resp.Body.Close()
    42  
    43  	return nil
    44  }
    45  
    46  // Fetch a specific User Conversation.
    47  func (c *ApiService) FetchUserConversation(UserSid string, ConversationSid string) (*ConversationsV1UserConversation, error) {
    48  	path := "/v1/Users/{UserSid}/Conversations/{ConversationSid}"
    49  	path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1)
    50  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
    51  
    52  	data := url.Values{}
    53  	headers := make(map[string]interface{})
    54  
    55  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	defer resp.Body.Close()
    61  
    62  	ps := &ConversationsV1UserConversation{}
    63  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	return ps, err
    68  }
    69  
    70  // Optional parameters for the method 'ListUserConversation'
    71  type ListUserConversationParams struct {
    72  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    73  	PageSize *int `json:"PageSize,omitempty"`
    74  	// Max number of records to return.
    75  	Limit *int `json:"limit,omitempty"`
    76  }
    77  
    78  func (params *ListUserConversationParams) SetPageSize(PageSize int) *ListUserConversationParams {
    79  	params.PageSize = &PageSize
    80  	return params
    81  }
    82  func (params *ListUserConversationParams) SetLimit(Limit int) *ListUserConversationParams {
    83  	params.Limit = &Limit
    84  	return params
    85  }
    86  
    87  // Retrieve a single page of UserConversation records from the API. Request is executed immediately.
    88  func (c *ApiService) PageUserConversation(UserSid string, params *ListUserConversationParams, pageToken, pageNumber string) (*ListUserConversationResponse, error) {
    89  	path := "/v1/Users/{UserSid}/Conversations"
    90  
    91  	path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1)
    92  
    93  	data := url.Values{}
    94  	headers := make(map[string]interface{})
    95  
    96  	if params != nil && params.PageSize != nil {
    97  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
    98  	}
    99  
   100  	if pageToken != "" {
   101  		data.Set("PageToken", pageToken)
   102  	}
   103  	if pageNumber != "" {
   104  		data.Set("Page", pageNumber)
   105  	}
   106  
   107  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	defer resp.Body.Close()
   113  
   114  	ps := &ListUserConversationResponse{}
   115  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	return ps, err
   120  }
   121  
   122  // Lists UserConversation records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   123  func (c *ApiService) ListUserConversation(UserSid string, params *ListUserConversationParams) ([]ConversationsV1UserConversation, error) {
   124  	response, errors := c.StreamUserConversation(UserSid, params)
   125  
   126  	records := make([]ConversationsV1UserConversation, 0)
   127  	for record := range response {
   128  		records = append(records, record)
   129  	}
   130  
   131  	if err := <-errors; err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	return records, nil
   136  }
   137  
   138  // Streams UserConversation records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   139  func (c *ApiService) StreamUserConversation(UserSid string, params *ListUserConversationParams) (chan ConversationsV1UserConversation, chan error) {
   140  	if params == nil {
   141  		params = &ListUserConversationParams{}
   142  	}
   143  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   144  
   145  	recordChannel := make(chan ConversationsV1UserConversation, 1)
   146  	errorChannel := make(chan error, 1)
   147  
   148  	response, err := c.PageUserConversation(UserSid, params, "", "")
   149  	if err != nil {
   150  		errorChannel <- err
   151  		close(recordChannel)
   152  		close(errorChannel)
   153  	} else {
   154  		go c.streamUserConversation(response, params, recordChannel, errorChannel)
   155  	}
   156  
   157  	return recordChannel, errorChannel
   158  }
   159  
   160  func (c *ApiService) streamUserConversation(response *ListUserConversationResponse, params *ListUserConversationParams, recordChannel chan ConversationsV1UserConversation, errorChannel chan error) {
   161  	curRecord := 1
   162  
   163  	for response != nil {
   164  		responseRecords := response.Conversations
   165  		for item := range responseRecords {
   166  			recordChannel <- responseRecords[item]
   167  			curRecord += 1
   168  			if params.Limit != nil && *params.Limit < curRecord {
   169  				close(recordChannel)
   170  				close(errorChannel)
   171  				return
   172  			}
   173  		}
   174  
   175  		record, err := client.GetNext(c.baseURL, response, c.getNextListUserConversationResponse)
   176  		if err != nil {
   177  			errorChannel <- err
   178  			break
   179  		} else if record == nil {
   180  			break
   181  		}
   182  
   183  		response = record.(*ListUserConversationResponse)
   184  	}
   185  
   186  	close(recordChannel)
   187  	close(errorChannel)
   188  }
   189  
   190  func (c *ApiService) getNextListUserConversationResponse(nextPageUrl string) (interface{}, error) {
   191  	if nextPageUrl == "" {
   192  		return nil, nil
   193  	}
   194  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   195  	if err != nil {
   196  		return nil, err
   197  	}
   198  
   199  	defer resp.Body.Close()
   200  
   201  	ps := &ListUserConversationResponse{}
   202  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   203  		return nil, err
   204  	}
   205  	return ps, nil
   206  }
   207  
   208  // Optional parameters for the method 'UpdateUserConversation'
   209  type UpdateUserConversationParams struct {
   210  	//
   211  	NotificationLevel *string `json:"NotificationLevel,omitempty"`
   212  	// The date of the last message read in conversation by the user, given in ISO 8601 format.
   213  	LastReadTimestamp *time.Time `json:"LastReadTimestamp,omitempty"`
   214  	// The index of the last Message in the Conversation that the Participant has read.
   215  	LastReadMessageIndex *int `json:"LastReadMessageIndex,omitempty"`
   216  }
   217  
   218  func (params *UpdateUserConversationParams) SetNotificationLevel(NotificationLevel string) *UpdateUserConversationParams {
   219  	params.NotificationLevel = &NotificationLevel
   220  	return params
   221  }
   222  func (params *UpdateUserConversationParams) SetLastReadTimestamp(LastReadTimestamp time.Time) *UpdateUserConversationParams {
   223  	params.LastReadTimestamp = &LastReadTimestamp
   224  	return params
   225  }
   226  func (params *UpdateUserConversationParams) SetLastReadMessageIndex(LastReadMessageIndex int) *UpdateUserConversationParams {
   227  	params.LastReadMessageIndex = &LastReadMessageIndex
   228  	return params
   229  }
   230  
   231  // Update a specific User Conversation.
   232  func (c *ApiService) UpdateUserConversation(UserSid string, ConversationSid string, params *UpdateUserConversationParams) (*ConversationsV1UserConversation, error) {
   233  	path := "/v1/Users/{UserSid}/Conversations/{ConversationSid}"
   234  	path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1)
   235  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
   236  
   237  	data := url.Values{}
   238  	headers := make(map[string]interface{})
   239  
   240  	if params != nil && params.NotificationLevel != nil {
   241  		data.Set("NotificationLevel", *params.NotificationLevel)
   242  	}
   243  	if params != nil && params.LastReadTimestamp != nil {
   244  		data.Set("LastReadTimestamp", fmt.Sprint((*params.LastReadTimestamp).Format(time.RFC3339)))
   245  	}
   246  	if params != nil && params.LastReadMessageIndex != nil {
   247  		data.Set("LastReadMessageIndex", fmt.Sprint(*params.LastReadMessageIndex))
   248  	}
   249  
   250  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   251  	if err != nil {
   252  		return nil, err
   253  	}
   254  
   255  	defer resp.Body.Close()
   256  
   257  	ps := &ConversationsV1UserConversation{}
   258  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   259  		return nil, err
   260  	}
   261  
   262  	return ps, err
   263  }