github.com/twilio/twilio-go@v1.20.1/rest/ip_messaging/v2/services_users.go (about)

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