github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/users.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 'CreateUser'
    27  type CreateUserParams struct {
    28  	// The X-Twilio-Webhook-Enabled HTTP request header
    29  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
    30  	// The application-defined string that uniquely identifies the resource's User within the [Conversation Service](https://www.twilio.com/docs/conversations/api/service-resource). This value is often a username or an email address, and is case-sensitive.
    31  	Identity *string `json:"Identity,omitempty"`
    32  	// The string that you assigned to describe the resource.
    33  	FriendlyName *string `json:"FriendlyName,omitempty"`
    34  	// The JSON Object string that stores application-specific data. If attributes have not been set, `{}` is returned.
    35  	Attributes *string `json:"Attributes,omitempty"`
    36  	// The SID of a service-level [Role](https://www.twilio.com/docs/conversations/api/role-resource) to assign to the user.
    37  	RoleSid *string `json:"RoleSid,omitempty"`
    38  }
    39  
    40  func (params *CreateUserParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateUserParams {
    41  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
    42  	return params
    43  }
    44  func (params *CreateUserParams) SetIdentity(Identity string) *CreateUserParams {
    45  	params.Identity = &Identity
    46  	return params
    47  }
    48  func (params *CreateUserParams) SetFriendlyName(FriendlyName string) *CreateUserParams {
    49  	params.FriendlyName = &FriendlyName
    50  	return params
    51  }
    52  func (params *CreateUserParams) SetAttributes(Attributes string) *CreateUserParams {
    53  	params.Attributes = &Attributes
    54  	return params
    55  }
    56  func (params *CreateUserParams) SetRoleSid(RoleSid string) *CreateUserParams {
    57  	params.RoleSid = &RoleSid
    58  	return params
    59  }
    60  
    61  // Add a new conversation user to your account's default service
    62  func (c *ApiService) CreateUser(params *CreateUserParams) (*ConversationsV1User, error) {
    63  	path := "/v1/Users"
    64  
    65  	data := url.Values{}
    66  	headers := make(map[string]interface{})
    67  
    68  	if params != nil && params.Identity != nil {
    69  		data.Set("Identity", *params.Identity)
    70  	}
    71  	if params != nil && params.FriendlyName != nil {
    72  		data.Set("FriendlyName", *params.FriendlyName)
    73  	}
    74  	if params != nil && params.Attributes != nil {
    75  		data.Set("Attributes", *params.Attributes)
    76  	}
    77  	if params != nil && params.RoleSid != nil {
    78  		data.Set("RoleSid", *params.RoleSid)
    79  	}
    80  
    81  	if params != nil && params.XTwilioWebhookEnabled != nil {
    82  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
    83  	}
    84  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	defer resp.Body.Close()
    90  
    91  	ps := &ConversationsV1User{}
    92  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	return ps, err
    97  }
    98  
    99  // Optional parameters for the method 'DeleteUser'
   100  type DeleteUserParams struct {
   101  	// The X-Twilio-Webhook-Enabled HTTP request header
   102  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
   103  }
   104  
   105  func (params *DeleteUserParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteUserParams {
   106  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
   107  	return params
   108  }
   109  
   110  // Remove a conversation user from your account's default service
   111  func (c *ApiService) DeleteUser(Sid string, params *DeleteUserParams) error {
   112  	path := "/v1/Users/{Sid}"
   113  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   114  
   115  	data := url.Values{}
   116  	headers := make(map[string]interface{})
   117  
   118  	if params != nil && params.XTwilioWebhookEnabled != nil {
   119  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   120  	}
   121  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	defer resp.Body.Close()
   127  
   128  	return nil
   129  }
   130  
   131  // Fetch a conversation user from your account's default service
   132  func (c *ApiService) FetchUser(Sid string) (*ConversationsV1User, error) {
   133  	path := "/v1/Users/{Sid}"
   134  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   135  
   136  	data := url.Values{}
   137  	headers := make(map[string]interface{})
   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 := &ConversationsV1User{}
   147  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	return ps, err
   152  }
   153  
   154  // Optional parameters for the method 'ListUser'
   155  type ListUserParams struct {
   156  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   157  	PageSize *int `json:"PageSize,omitempty"`
   158  	// Max number of records to return.
   159  	Limit *int `json:"limit,omitempty"`
   160  }
   161  
   162  func (params *ListUserParams) SetPageSize(PageSize int) *ListUserParams {
   163  	params.PageSize = &PageSize
   164  	return params
   165  }
   166  func (params *ListUserParams) SetLimit(Limit int) *ListUserParams {
   167  	params.Limit = &Limit
   168  	return params
   169  }
   170  
   171  // Retrieve a single page of User records from the API. Request is executed immediately.
   172  func (c *ApiService) PageUser(params *ListUserParams, pageToken, pageNumber string) (*ListUserResponse, error) {
   173  	path := "/v1/Users"
   174  
   175  	data := url.Values{}
   176  	headers := make(map[string]interface{})
   177  
   178  	if params != nil && params.PageSize != nil {
   179  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   180  	}
   181  
   182  	if pageToken != "" {
   183  		data.Set("PageToken", pageToken)
   184  	}
   185  	if pageNumber != "" {
   186  		data.Set("Page", pageNumber)
   187  	}
   188  
   189  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   190  	if err != nil {
   191  		return nil, err
   192  	}
   193  
   194  	defer resp.Body.Close()
   195  
   196  	ps := &ListUserResponse{}
   197  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   198  		return nil, err
   199  	}
   200  
   201  	return ps, err
   202  }
   203  
   204  // Lists User records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   205  func (c *ApiService) ListUser(params *ListUserParams) ([]ConversationsV1User, error) {
   206  	response, errors := c.StreamUser(params)
   207  
   208  	records := make([]ConversationsV1User, 0)
   209  	for record := range response {
   210  		records = append(records, record)
   211  	}
   212  
   213  	if err := <-errors; err != nil {
   214  		return nil, err
   215  	}
   216  
   217  	return records, nil
   218  }
   219  
   220  // Streams User records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   221  func (c *ApiService) StreamUser(params *ListUserParams) (chan ConversationsV1User, chan error) {
   222  	if params == nil {
   223  		params = &ListUserParams{}
   224  	}
   225  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   226  
   227  	recordChannel := make(chan ConversationsV1User, 1)
   228  	errorChannel := make(chan error, 1)
   229  
   230  	response, err := c.PageUser(params, "", "")
   231  	if err != nil {
   232  		errorChannel <- err
   233  		close(recordChannel)
   234  		close(errorChannel)
   235  	} else {
   236  		go c.streamUser(response, params, recordChannel, errorChannel)
   237  	}
   238  
   239  	return recordChannel, errorChannel
   240  }
   241  
   242  func (c *ApiService) streamUser(response *ListUserResponse, params *ListUserParams, recordChannel chan ConversationsV1User, errorChannel chan error) {
   243  	curRecord := 1
   244  
   245  	for response != nil {
   246  		responseRecords := response.Users
   247  		for item := range responseRecords {
   248  			recordChannel <- responseRecords[item]
   249  			curRecord += 1
   250  			if params.Limit != nil && *params.Limit < curRecord {
   251  				close(recordChannel)
   252  				close(errorChannel)
   253  				return
   254  			}
   255  		}
   256  
   257  		record, err := client.GetNext(c.baseURL, response, c.getNextListUserResponse)
   258  		if err != nil {
   259  			errorChannel <- err
   260  			break
   261  		} else if record == nil {
   262  			break
   263  		}
   264  
   265  		response = record.(*ListUserResponse)
   266  	}
   267  
   268  	close(recordChannel)
   269  	close(errorChannel)
   270  }
   271  
   272  func (c *ApiService) getNextListUserResponse(nextPageUrl string) (interface{}, error) {
   273  	if nextPageUrl == "" {
   274  		return nil, nil
   275  	}
   276  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   277  	if err != nil {
   278  		return nil, err
   279  	}
   280  
   281  	defer resp.Body.Close()
   282  
   283  	ps := &ListUserResponse{}
   284  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   285  		return nil, err
   286  	}
   287  	return ps, nil
   288  }
   289  
   290  // Optional parameters for the method 'UpdateUser'
   291  type UpdateUserParams struct {
   292  	// The X-Twilio-Webhook-Enabled HTTP request header
   293  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
   294  	// The string that you assigned to describe the resource.
   295  	FriendlyName *string `json:"FriendlyName,omitempty"`
   296  	// The JSON Object string that stores application-specific data. If attributes have not been set, `{}` is returned.
   297  	Attributes *string `json:"Attributes,omitempty"`
   298  	// The SID of a service-level [Role](https://www.twilio.com/docs/conversations/api/role-resource) to assign to the user.
   299  	RoleSid *string `json:"RoleSid,omitempty"`
   300  }
   301  
   302  func (params *UpdateUserParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateUserParams {
   303  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
   304  	return params
   305  }
   306  func (params *UpdateUserParams) SetFriendlyName(FriendlyName string) *UpdateUserParams {
   307  	params.FriendlyName = &FriendlyName
   308  	return params
   309  }
   310  func (params *UpdateUserParams) SetAttributes(Attributes string) *UpdateUserParams {
   311  	params.Attributes = &Attributes
   312  	return params
   313  }
   314  func (params *UpdateUserParams) SetRoleSid(RoleSid string) *UpdateUserParams {
   315  	params.RoleSid = &RoleSid
   316  	return params
   317  }
   318  
   319  // Update an existing conversation user in your account's default service
   320  func (c *ApiService) UpdateUser(Sid string, params *UpdateUserParams) (*ConversationsV1User, error) {
   321  	path := "/v1/Users/{Sid}"
   322  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   323  
   324  	data := url.Values{}
   325  	headers := make(map[string]interface{})
   326  
   327  	if params != nil && params.FriendlyName != nil {
   328  		data.Set("FriendlyName", *params.FriendlyName)
   329  	}
   330  	if params != nil && params.Attributes != nil {
   331  		data.Set("Attributes", *params.Attributes)
   332  	}
   333  	if params != nil && params.RoleSid != nil {
   334  		data.Set("RoleSid", *params.RoleSid)
   335  	}
   336  
   337  	if params != nil && params.XTwilioWebhookEnabled != nil {
   338  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   339  	}
   340  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   341  	if err != nil {
   342  		return nil, err
   343  	}
   344  
   345  	defer resp.Body.Close()
   346  
   347  	ps := &ConversationsV1User{}
   348  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   349  		return nil, err
   350  	}
   351  
   352  	return ps, err
   353  }