github.com/twilio/twilio-go@v1.20.1/rest/ip_messaging/v2/services_roles.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 'CreateRole'
    27  type CreateRoleParams struct {
    28  	//
    29  	FriendlyName *string `json:"FriendlyName,omitempty"`
    30  	//
    31  	Type *string `json:"Type,omitempty"`
    32  	//
    33  	Permission *[]string `json:"Permission,omitempty"`
    34  }
    35  
    36  func (params *CreateRoleParams) SetFriendlyName(FriendlyName string) *CreateRoleParams {
    37  	params.FriendlyName = &FriendlyName
    38  	return params
    39  }
    40  func (params *CreateRoleParams) SetType(Type string) *CreateRoleParams {
    41  	params.Type = &Type
    42  	return params
    43  }
    44  func (params *CreateRoleParams) SetPermission(Permission []string) *CreateRoleParams {
    45  	params.Permission = &Permission
    46  	return params
    47  }
    48  
    49  //
    50  func (c *ApiService) CreateRole(ServiceSid string, params *CreateRoleParams) (*IpMessagingV2Role, error) {
    51  	path := "/v2/Services/{ServiceSid}/Roles"
    52  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    53  
    54  	data := url.Values{}
    55  	headers := make(map[string]interface{})
    56  
    57  	if params != nil && params.FriendlyName != nil {
    58  		data.Set("FriendlyName", *params.FriendlyName)
    59  	}
    60  	if params != nil && params.Type != nil {
    61  		data.Set("Type", *params.Type)
    62  	}
    63  	if params != nil && params.Permission != nil {
    64  		for _, item := range *params.Permission {
    65  			data.Add("Permission", item)
    66  		}
    67  	}
    68  
    69  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	defer resp.Body.Close()
    75  
    76  	ps := &IpMessagingV2Role{}
    77  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	return ps, err
    82  }
    83  
    84  //
    85  func (c *ApiService) DeleteRole(ServiceSid string, Sid string) error {
    86  	path := "/v2/Services/{ServiceSid}/Roles/{Sid}"
    87  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    88  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    89  
    90  	data := url.Values{}
    91  	headers := make(map[string]interface{})
    92  
    93  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	defer resp.Body.Close()
    99  
   100  	return nil
   101  }
   102  
   103  //
   104  func (c *ApiService) FetchRole(ServiceSid string, Sid string) (*IpMessagingV2Role, error) {
   105  	path := "/v2/Services/{ServiceSid}/Roles/{Sid}"
   106  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   107  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   108  
   109  	data := url.Values{}
   110  	headers := make(map[string]interface{})
   111  
   112  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	defer resp.Body.Close()
   118  
   119  	ps := &IpMessagingV2Role{}
   120  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	return ps, err
   125  }
   126  
   127  // Optional parameters for the method 'ListRole'
   128  type ListRoleParams struct {
   129  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   130  	PageSize *int `json:"PageSize,omitempty"`
   131  	// Max number of records to return.
   132  	Limit *int `json:"limit,omitempty"`
   133  }
   134  
   135  func (params *ListRoleParams) SetPageSize(PageSize int) *ListRoleParams {
   136  	params.PageSize = &PageSize
   137  	return params
   138  }
   139  func (params *ListRoleParams) SetLimit(Limit int) *ListRoleParams {
   140  	params.Limit = &Limit
   141  	return params
   142  }
   143  
   144  // Retrieve a single page of Role records from the API. Request is executed immediately.
   145  func (c *ApiService) PageRole(ServiceSid string, params *ListRoleParams, pageToken, pageNumber string) (*ListRoleResponse, error) {
   146  	path := "/v2/Services/{ServiceSid}/Roles"
   147  
   148  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   149  
   150  	data := url.Values{}
   151  	headers := make(map[string]interface{})
   152  
   153  	if params != nil && params.PageSize != nil {
   154  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   155  	}
   156  
   157  	if pageToken != "" {
   158  		data.Set("PageToken", pageToken)
   159  	}
   160  	if pageNumber != "" {
   161  		data.Set("Page", pageNumber)
   162  	}
   163  
   164  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   165  	if err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	defer resp.Body.Close()
   170  
   171  	ps := &ListRoleResponse{}
   172  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   173  		return nil, err
   174  	}
   175  
   176  	return ps, err
   177  }
   178  
   179  // Lists Role records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   180  func (c *ApiService) ListRole(ServiceSid string, params *ListRoleParams) ([]IpMessagingV2Role, error) {
   181  	response, errors := c.StreamRole(ServiceSid, params)
   182  
   183  	records := make([]IpMessagingV2Role, 0)
   184  	for record := range response {
   185  		records = append(records, record)
   186  	}
   187  
   188  	if err := <-errors; err != nil {
   189  		return nil, err
   190  	}
   191  
   192  	return records, nil
   193  }
   194  
   195  // Streams Role records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   196  func (c *ApiService) StreamRole(ServiceSid string, params *ListRoleParams) (chan IpMessagingV2Role, chan error) {
   197  	if params == nil {
   198  		params = &ListRoleParams{}
   199  	}
   200  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   201  
   202  	recordChannel := make(chan IpMessagingV2Role, 1)
   203  	errorChannel := make(chan error, 1)
   204  
   205  	response, err := c.PageRole(ServiceSid, params, "", "")
   206  	if err != nil {
   207  		errorChannel <- err
   208  		close(recordChannel)
   209  		close(errorChannel)
   210  	} else {
   211  		go c.streamRole(response, params, recordChannel, errorChannel)
   212  	}
   213  
   214  	return recordChannel, errorChannel
   215  }
   216  
   217  func (c *ApiService) streamRole(response *ListRoleResponse, params *ListRoleParams, recordChannel chan IpMessagingV2Role, errorChannel chan error) {
   218  	curRecord := 1
   219  
   220  	for response != nil {
   221  		responseRecords := response.Roles
   222  		for item := range responseRecords {
   223  			recordChannel <- responseRecords[item]
   224  			curRecord += 1
   225  			if params.Limit != nil && *params.Limit < curRecord {
   226  				close(recordChannel)
   227  				close(errorChannel)
   228  				return
   229  			}
   230  		}
   231  
   232  		record, err := client.GetNext(c.baseURL, response, c.getNextListRoleResponse)
   233  		if err != nil {
   234  			errorChannel <- err
   235  			break
   236  		} else if record == nil {
   237  			break
   238  		}
   239  
   240  		response = record.(*ListRoleResponse)
   241  	}
   242  
   243  	close(recordChannel)
   244  	close(errorChannel)
   245  }
   246  
   247  func (c *ApiService) getNextListRoleResponse(nextPageUrl string) (interface{}, error) {
   248  	if nextPageUrl == "" {
   249  		return nil, nil
   250  	}
   251  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   252  	if err != nil {
   253  		return nil, err
   254  	}
   255  
   256  	defer resp.Body.Close()
   257  
   258  	ps := &ListRoleResponse{}
   259  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   260  		return nil, err
   261  	}
   262  	return ps, nil
   263  }
   264  
   265  // Optional parameters for the method 'UpdateRole'
   266  type UpdateRoleParams struct {
   267  	//
   268  	Permission *[]string `json:"Permission,omitempty"`
   269  }
   270  
   271  func (params *UpdateRoleParams) SetPermission(Permission []string) *UpdateRoleParams {
   272  	params.Permission = &Permission
   273  	return params
   274  }
   275  
   276  //
   277  func (c *ApiService) UpdateRole(ServiceSid string, Sid string, params *UpdateRoleParams) (*IpMessagingV2Role, error) {
   278  	path := "/v2/Services/{ServiceSid}/Roles/{Sid}"
   279  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   280  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   281  
   282  	data := url.Values{}
   283  	headers := make(map[string]interface{})
   284  
   285  	if params != nil && params.Permission != nil {
   286  		for _, item := range *params.Permission {
   287  			data.Add("Permission", item)
   288  		}
   289  	}
   290  
   291  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   292  	if err != nil {
   293  		return nil, err
   294  	}
   295  
   296  	defer resp.Body.Close()
   297  
   298  	ps := &IpMessagingV2Role{}
   299  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   300  		return nil, err
   301  	}
   302  
   303  	return ps, err
   304  }