github.com/twilio/twilio-go@v1.20.1/rest/sync/v1/services_lists_permissions.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Sync
     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  // Delete a specific Sync List Permission.
    27  func (c *ApiService) DeleteSyncListPermission(ServiceSid string, ListSid string, Identity string) error {
    28  	path := "/v1/Services/{ServiceSid}/Lists/{ListSid}/Permissions/{Identity}"
    29  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    30  	path = strings.Replace(path, "{"+"ListSid"+"}", ListSid, -1)
    31  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
    32  
    33  	data := url.Values{}
    34  	headers := make(map[string]interface{})
    35  
    36  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	defer resp.Body.Close()
    42  
    43  	return nil
    44  }
    45  
    46  // Fetch a specific Sync List Permission.
    47  func (c *ApiService) FetchSyncListPermission(ServiceSid string, ListSid string, Identity string) (*SyncV1SyncListPermission, error) {
    48  	path := "/v1/Services/{ServiceSid}/Lists/{ListSid}/Permissions/{Identity}"
    49  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    50  	path = strings.Replace(path, "{"+"ListSid"+"}", ListSid, -1)
    51  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
    52  
    53  	data := url.Values{}
    54  	headers := make(map[string]interface{})
    55  
    56  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	defer resp.Body.Close()
    62  
    63  	ps := &SyncV1SyncListPermission{}
    64  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	return ps, err
    69  }
    70  
    71  // Optional parameters for the method 'ListSyncListPermission'
    72  type ListSyncListPermissionParams struct {
    73  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    74  	PageSize *int `json:"PageSize,omitempty"`
    75  	// Max number of records to return.
    76  	Limit *int `json:"limit,omitempty"`
    77  }
    78  
    79  func (params *ListSyncListPermissionParams) SetPageSize(PageSize int) *ListSyncListPermissionParams {
    80  	params.PageSize = &PageSize
    81  	return params
    82  }
    83  func (params *ListSyncListPermissionParams) SetLimit(Limit int) *ListSyncListPermissionParams {
    84  	params.Limit = &Limit
    85  	return params
    86  }
    87  
    88  // Retrieve a single page of SyncListPermission records from the API. Request is executed immediately.
    89  func (c *ApiService) PageSyncListPermission(ServiceSid string, ListSid string, params *ListSyncListPermissionParams, pageToken, pageNumber string) (*ListSyncListPermissionResponse, error) {
    90  	path := "/v1/Services/{ServiceSid}/Lists/{ListSid}/Permissions"
    91  
    92  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    93  	path = strings.Replace(path, "{"+"ListSid"+"}", ListSid, -1)
    94  
    95  	data := url.Values{}
    96  	headers := make(map[string]interface{})
    97  
    98  	if params != nil && params.PageSize != nil {
    99  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   100  	}
   101  
   102  	if pageToken != "" {
   103  		data.Set("PageToken", pageToken)
   104  	}
   105  	if pageNumber != "" {
   106  		data.Set("Page", pageNumber)
   107  	}
   108  
   109  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	defer resp.Body.Close()
   115  
   116  	ps := &ListSyncListPermissionResponse{}
   117  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	return ps, err
   122  }
   123  
   124  // Lists SyncListPermission records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   125  func (c *ApiService) ListSyncListPermission(ServiceSid string, ListSid string, params *ListSyncListPermissionParams) ([]SyncV1SyncListPermission, error) {
   126  	response, errors := c.StreamSyncListPermission(ServiceSid, ListSid, params)
   127  
   128  	records := make([]SyncV1SyncListPermission, 0)
   129  	for record := range response {
   130  		records = append(records, record)
   131  	}
   132  
   133  	if err := <-errors; err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	return records, nil
   138  }
   139  
   140  // Streams SyncListPermission records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   141  func (c *ApiService) StreamSyncListPermission(ServiceSid string, ListSid string, params *ListSyncListPermissionParams) (chan SyncV1SyncListPermission, chan error) {
   142  	if params == nil {
   143  		params = &ListSyncListPermissionParams{}
   144  	}
   145  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   146  
   147  	recordChannel := make(chan SyncV1SyncListPermission, 1)
   148  	errorChannel := make(chan error, 1)
   149  
   150  	response, err := c.PageSyncListPermission(ServiceSid, ListSid, params, "", "")
   151  	if err != nil {
   152  		errorChannel <- err
   153  		close(recordChannel)
   154  		close(errorChannel)
   155  	} else {
   156  		go c.streamSyncListPermission(response, params, recordChannel, errorChannel)
   157  	}
   158  
   159  	return recordChannel, errorChannel
   160  }
   161  
   162  func (c *ApiService) streamSyncListPermission(response *ListSyncListPermissionResponse, params *ListSyncListPermissionParams, recordChannel chan SyncV1SyncListPermission, errorChannel chan error) {
   163  	curRecord := 1
   164  
   165  	for response != nil {
   166  		responseRecords := response.Permissions
   167  		for item := range responseRecords {
   168  			recordChannel <- responseRecords[item]
   169  			curRecord += 1
   170  			if params.Limit != nil && *params.Limit < curRecord {
   171  				close(recordChannel)
   172  				close(errorChannel)
   173  				return
   174  			}
   175  		}
   176  
   177  		record, err := client.GetNext(c.baseURL, response, c.getNextListSyncListPermissionResponse)
   178  		if err != nil {
   179  			errorChannel <- err
   180  			break
   181  		} else if record == nil {
   182  			break
   183  		}
   184  
   185  		response = record.(*ListSyncListPermissionResponse)
   186  	}
   187  
   188  	close(recordChannel)
   189  	close(errorChannel)
   190  }
   191  
   192  func (c *ApiService) getNextListSyncListPermissionResponse(nextPageUrl string) (interface{}, error) {
   193  	if nextPageUrl == "" {
   194  		return nil, nil
   195  	}
   196  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  
   201  	defer resp.Body.Close()
   202  
   203  	ps := &ListSyncListPermissionResponse{}
   204  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   205  		return nil, err
   206  	}
   207  	return ps, nil
   208  }
   209  
   210  // Optional parameters for the method 'UpdateSyncListPermission'
   211  type UpdateSyncListPermissionParams struct {
   212  	// Whether the identity can read the Sync List and its Items. Default value is `false`.
   213  	Read *bool `json:"Read,omitempty"`
   214  	// Whether the identity can create, update, and delete Items in the Sync List. Default value is `false`.
   215  	Write *bool `json:"Write,omitempty"`
   216  	// Whether the identity can delete the Sync List. Default value is `false`.
   217  	Manage *bool `json:"Manage,omitempty"`
   218  }
   219  
   220  func (params *UpdateSyncListPermissionParams) SetRead(Read bool) *UpdateSyncListPermissionParams {
   221  	params.Read = &Read
   222  	return params
   223  }
   224  func (params *UpdateSyncListPermissionParams) SetWrite(Write bool) *UpdateSyncListPermissionParams {
   225  	params.Write = &Write
   226  	return params
   227  }
   228  func (params *UpdateSyncListPermissionParams) SetManage(Manage bool) *UpdateSyncListPermissionParams {
   229  	params.Manage = &Manage
   230  	return params
   231  }
   232  
   233  // Update an identity's access to a specific Sync List.
   234  func (c *ApiService) UpdateSyncListPermission(ServiceSid string, ListSid string, Identity string, params *UpdateSyncListPermissionParams) (*SyncV1SyncListPermission, error) {
   235  	path := "/v1/Services/{ServiceSid}/Lists/{ListSid}/Permissions/{Identity}"
   236  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   237  	path = strings.Replace(path, "{"+"ListSid"+"}", ListSid, -1)
   238  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
   239  
   240  	data := url.Values{}
   241  	headers := make(map[string]interface{})
   242  
   243  	if params != nil && params.Read != nil {
   244  		data.Set("Read", fmt.Sprint(*params.Read))
   245  	}
   246  	if params != nil && params.Write != nil {
   247  		data.Set("Write", fmt.Sprint(*params.Write))
   248  	}
   249  	if params != nil && params.Manage != nil {
   250  		data.Set("Manage", fmt.Sprint(*params.Manage))
   251  	}
   252  
   253  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   254  	if err != nil {
   255  		return nil, err
   256  	}
   257  
   258  	defer resp.Body.Close()
   259  
   260  	ps := &SyncV1SyncListPermission{}
   261  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   262  		return nil, err
   263  	}
   264  
   265  	return ps, err
   266  }