github.com/twilio/twilio-go@v1.20.1/rest/proxy/v1/services_sessions_participants_message_interactions.go (about)

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