github.com/twilio/twilio-go@v1.20.1/rest/proxy/v1/services_sessions.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  	"time"
    23  
    24  	"github.com/twilio/twilio-go/client"
    25  )
    26  
    27  // Optional parameters for the method 'CreateSession'
    28  type CreateSessionParams struct {
    29  	// An application-defined string that uniquely identifies the resource. This value must be 191 characters or fewer in length and be unique. **This value should not have PII.**
    30  	UniqueName *string `json:"UniqueName,omitempty"`
    31  	// The [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date when the Session should expire. If this is value is present, it overrides the `ttl` value.
    32  	DateExpiry *time.Time `json:"DateExpiry,omitempty"`
    33  	// The time, in seconds, when the session will expire. The time is measured from the last Session create or the Session's last Interaction.
    34  	Ttl *int `json:"Ttl,omitempty"`
    35  	//
    36  	Mode *string `json:"Mode,omitempty"`
    37  	//
    38  	Status *string `json:"Status,omitempty"`
    39  	// The Participant objects to include in the new session.
    40  	Participants *[]interface{} `json:"Participants,omitempty"`
    41  }
    42  
    43  func (params *CreateSessionParams) SetUniqueName(UniqueName string) *CreateSessionParams {
    44  	params.UniqueName = &UniqueName
    45  	return params
    46  }
    47  func (params *CreateSessionParams) SetDateExpiry(DateExpiry time.Time) *CreateSessionParams {
    48  	params.DateExpiry = &DateExpiry
    49  	return params
    50  }
    51  func (params *CreateSessionParams) SetTtl(Ttl int) *CreateSessionParams {
    52  	params.Ttl = &Ttl
    53  	return params
    54  }
    55  func (params *CreateSessionParams) SetMode(Mode string) *CreateSessionParams {
    56  	params.Mode = &Mode
    57  	return params
    58  }
    59  func (params *CreateSessionParams) SetStatus(Status string) *CreateSessionParams {
    60  	params.Status = &Status
    61  	return params
    62  }
    63  func (params *CreateSessionParams) SetParticipants(Participants []interface{}) *CreateSessionParams {
    64  	params.Participants = &Participants
    65  	return params
    66  }
    67  
    68  // Create a new Session
    69  func (c *ApiService) CreateSession(ServiceSid string, params *CreateSessionParams) (*ProxyV1Session, error) {
    70  	path := "/v1/Services/{ServiceSid}/Sessions"
    71  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    72  
    73  	data := url.Values{}
    74  	headers := make(map[string]interface{})
    75  
    76  	if params != nil && params.UniqueName != nil {
    77  		data.Set("UniqueName", *params.UniqueName)
    78  	}
    79  	if params != nil && params.DateExpiry != nil {
    80  		data.Set("DateExpiry", fmt.Sprint((*params.DateExpiry).Format(time.RFC3339)))
    81  	}
    82  	if params != nil && params.Ttl != nil {
    83  		data.Set("Ttl", fmt.Sprint(*params.Ttl))
    84  	}
    85  	if params != nil && params.Mode != nil {
    86  		data.Set("Mode", *params.Mode)
    87  	}
    88  	if params != nil && params.Status != nil {
    89  		data.Set("Status", *params.Status)
    90  	}
    91  	if params != nil && params.Participants != nil {
    92  		for _, item := range *params.Participants {
    93  			v, err := json.Marshal(item)
    94  
    95  			if err != nil {
    96  				return nil, err
    97  			}
    98  
    99  			data.Add("Participants", string(v))
   100  		}
   101  	}
   102  
   103  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	defer resp.Body.Close()
   109  
   110  	ps := &ProxyV1Session{}
   111  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	return ps, err
   116  }
   117  
   118  // Delete a specific Session.
   119  func (c *ApiService) DeleteSession(ServiceSid string, Sid string) error {
   120  	path := "/v1/Services/{ServiceSid}/Sessions/{Sid}"
   121  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   122  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   123  
   124  	data := url.Values{}
   125  	headers := make(map[string]interface{})
   126  
   127  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	defer resp.Body.Close()
   133  
   134  	return nil
   135  }
   136  
   137  // Fetch a specific Session.
   138  func (c *ApiService) FetchSession(ServiceSid string, Sid string) (*ProxyV1Session, error) {
   139  	path := "/v1/Services/{ServiceSid}/Sessions/{Sid}"
   140  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   141  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   142  
   143  	data := url.Values{}
   144  	headers := make(map[string]interface{})
   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 := &ProxyV1Session{}
   154  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   155  		return nil, err
   156  	}
   157  
   158  	return ps, err
   159  }
   160  
   161  // Optional parameters for the method 'ListSession'
   162  type ListSessionParams struct {
   163  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   164  	PageSize *int `json:"PageSize,omitempty"`
   165  	// Max number of records to return.
   166  	Limit *int `json:"limit,omitempty"`
   167  }
   168  
   169  func (params *ListSessionParams) SetPageSize(PageSize int) *ListSessionParams {
   170  	params.PageSize = &PageSize
   171  	return params
   172  }
   173  func (params *ListSessionParams) SetLimit(Limit int) *ListSessionParams {
   174  	params.Limit = &Limit
   175  	return params
   176  }
   177  
   178  // Retrieve a single page of Session records from the API. Request is executed immediately.
   179  func (c *ApiService) PageSession(ServiceSid string, params *ListSessionParams, pageToken, pageNumber string) (*ListSessionResponse, error) {
   180  	path := "/v1/Services/{ServiceSid}/Sessions"
   181  
   182  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   183  
   184  	data := url.Values{}
   185  	headers := make(map[string]interface{})
   186  
   187  	if params != nil && params.PageSize != nil {
   188  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   189  	}
   190  
   191  	if pageToken != "" {
   192  		data.Set("PageToken", pageToken)
   193  	}
   194  	if pageNumber != "" {
   195  		data.Set("Page", pageNumber)
   196  	}
   197  
   198  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   199  	if err != nil {
   200  		return nil, err
   201  	}
   202  
   203  	defer resp.Body.Close()
   204  
   205  	ps := &ListSessionResponse{}
   206  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   207  		return nil, err
   208  	}
   209  
   210  	return ps, err
   211  }
   212  
   213  // Lists Session records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   214  func (c *ApiService) ListSession(ServiceSid string, params *ListSessionParams) ([]ProxyV1Session, error) {
   215  	response, errors := c.StreamSession(ServiceSid, params)
   216  
   217  	records := make([]ProxyV1Session, 0)
   218  	for record := range response {
   219  		records = append(records, record)
   220  	}
   221  
   222  	if err := <-errors; err != nil {
   223  		return nil, err
   224  	}
   225  
   226  	return records, nil
   227  }
   228  
   229  // Streams Session records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   230  func (c *ApiService) StreamSession(ServiceSid string, params *ListSessionParams) (chan ProxyV1Session, chan error) {
   231  	if params == nil {
   232  		params = &ListSessionParams{}
   233  	}
   234  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   235  
   236  	recordChannel := make(chan ProxyV1Session, 1)
   237  	errorChannel := make(chan error, 1)
   238  
   239  	response, err := c.PageSession(ServiceSid, params, "", "")
   240  	if err != nil {
   241  		errorChannel <- err
   242  		close(recordChannel)
   243  		close(errorChannel)
   244  	} else {
   245  		go c.streamSession(response, params, recordChannel, errorChannel)
   246  	}
   247  
   248  	return recordChannel, errorChannel
   249  }
   250  
   251  func (c *ApiService) streamSession(response *ListSessionResponse, params *ListSessionParams, recordChannel chan ProxyV1Session, errorChannel chan error) {
   252  	curRecord := 1
   253  
   254  	for response != nil {
   255  		responseRecords := response.Sessions
   256  		for item := range responseRecords {
   257  			recordChannel <- responseRecords[item]
   258  			curRecord += 1
   259  			if params.Limit != nil && *params.Limit < curRecord {
   260  				close(recordChannel)
   261  				close(errorChannel)
   262  				return
   263  			}
   264  		}
   265  
   266  		record, err := client.GetNext(c.baseURL, response, c.getNextListSessionResponse)
   267  		if err != nil {
   268  			errorChannel <- err
   269  			break
   270  		} else if record == nil {
   271  			break
   272  		}
   273  
   274  		response = record.(*ListSessionResponse)
   275  	}
   276  
   277  	close(recordChannel)
   278  	close(errorChannel)
   279  }
   280  
   281  func (c *ApiService) getNextListSessionResponse(nextPageUrl string) (interface{}, error) {
   282  	if nextPageUrl == "" {
   283  		return nil, nil
   284  	}
   285  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   286  	if err != nil {
   287  		return nil, err
   288  	}
   289  
   290  	defer resp.Body.Close()
   291  
   292  	ps := &ListSessionResponse{}
   293  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   294  		return nil, err
   295  	}
   296  	return ps, nil
   297  }
   298  
   299  // Optional parameters for the method 'UpdateSession'
   300  type UpdateSessionParams struct {
   301  	// The [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date when the Session should expire. If this is value is present, it overrides the `ttl` value.
   302  	DateExpiry *time.Time `json:"DateExpiry,omitempty"`
   303  	// The time, in seconds, when the session will expire. The time is measured from the last Session create or the Session's last Interaction.
   304  	Ttl *int `json:"Ttl,omitempty"`
   305  	//
   306  	Status *string `json:"Status,omitempty"`
   307  }
   308  
   309  func (params *UpdateSessionParams) SetDateExpiry(DateExpiry time.Time) *UpdateSessionParams {
   310  	params.DateExpiry = &DateExpiry
   311  	return params
   312  }
   313  func (params *UpdateSessionParams) SetTtl(Ttl int) *UpdateSessionParams {
   314  	params.Ttl = &Ttl
   315  	return params
   316  }
   317  func (params *UpdateSessionParams) SetStatus(Status string) *UpdateSessionParams {
   318  	params.Status = &Status
   319  	return params
   320  }
   321  
   322  // Update a specific Session.
   323  func (c *ApiService) UpdateSession(ServiceSid string, Sid string, params *UpdateSessionParams) (*ProxyV1Session, error) {
   324  	path := "/v1/Services/{ServiceSid}/Sessions/{Sid}"
   325  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   326  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   327  
   328  	data := url.Values{}
   329  	headers := make(map[string]interface{})
   330  
   331  	if params != nil && params.DateExpiry != nil {
   332  		data.Set("DateExpiry", fmt.Sprint((*params.DateExpiry).Format(time.RFC3339)))
   333  	}
   334  	if params != nil && params.Ttl != nil {
   335  		data.Set("Ttl", fmt.Sprint(*params.Ttl))
   336  	}
   337  	if params != nil && params.Status != nil {
   338  		data.Set("Status", *params.Status)
   339  	}
   340  
   341  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   342  	if err != nil {
   343  		return nil, err
   344  	}
   345  
   346  	defer resp.Body.Close()
   347  
   348  	ps := &ProxyV1Session{}
   349  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   350  		return nil, err
   351  	}
   352  
   353  	return ps, err
   354  }