github.com/twilio/twilio-go@v1.20.1/rest/sync/v1/services_documents_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 Document Permission. 27 func (c *ApiService) DeleteDocumentPermission(ServiceSid string, DocumentSid string, Identity string) error { 28 path := "/v1/Services/{ServiceSid}/Documents/{DocumentSid}/Permissions/{Identity}" 29 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 30 path = strings.Replace(path, "{"+"DocumentSid"+"}", DocumentSid, -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 Document Permission. 47 func (c *ApiService) FetchDocumentPermission(ServiceSid string, DocumentSid string, Identity string) (*SyncV1DocumentPermission, error) { 48 path := "/v1/Services/{ServiceSid}/Documents/{DocumentSid}/Permissions/{Identity}" 49 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 50 path = strings.Replace(path, "{"+"DocumentSid"+"}", DocumentSid, -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 := &SyncV1DocumentPermission{} 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 'ListDocumentPermission' 72 type ListDocumentPermissionParams 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 *ListDocumentPermissionParams) SetPageSize(PageSize int) *ListDocumentPermissionParams { 80 params.PageSize = &PageSize 81 return params 82 } 83 func (params *ListDocumentPermissionParams) SetLimit(Limit int) *ListDocumentPermissionParams { 84 params.Limit = &Limit 85 return params 86 } 87 88 // Retrieve a single page of DocumentPermission records from the API. Request is executed immediately. 89 func (c *ApiService) PageDocumentPermission(ServiceSid string, DocumentSid string, params *ListDocumentPermissionParams, pageToken, pageNumber string) (*ListDocumentPermissionResponse, error) { 90 path := "/v1/Services/{ServiceSid}/Documents/{DocumentSid}/Permissions" 91 92 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 93 path = strings.Replace(path, "{"+"DocumentSid"+"}", DocumentSid, -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 := &ListDocumentPermissionResponse{} 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 DocumentPermission 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) ListDocumentPermission(ServiceSid string, DocumentSid string, params *ListDocumentPermissionParams) ([]SyncV1DocumentPermission, error) { 126 response, errors := c.StreamDocumentPermission(ServiceSid, DocumentSid, params) 127 128 records := make([]SyncV1DocumentPermission, 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 DocumentPermission 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) StreamDocumentPermission(ServiceSid string, DocumentSid string, params *ListDocumentPermissionParams) (chan SyncV1DocumentPermission, chan error) { 142 if params == nil { 143 params = &ListDocumentPermissionParams{} 144 } 145 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 146 147 recordChannel := make(chan SyncV1DocumentPermission, 1) 148 errorChannel := make(chan error, 1) 149 150 response, err := c.PageDocumentPermission(ServiceSid, DocumentSid, params, "", "") 151 if err != nil { 152 errorChannel <- err 153 close(recordChannel) 154 close(errorChannel) 155 } else { 156 go c.streamDocumentPermission(response, params, recordChannel, errorChannel) 157 } 158 159 return recordChannel, errorChannel 160 } 161 162 func (c *ApiService) streamDocumentPermission(response *ListDocumentPermissionResponse, params *ListDocumentPermissionParams, recordChannel chan SyncV1DocumentPermission, 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.getNextListDocumentPermissionResponse) 178 if err != nil { 179 errorChannel <- err 180 break 181 } else if record == nil { 182 break 183 } 184 185 response = record.(*ListDocumentPermissionResponse) 186 } 187 188 close(recordChannel) 189 close(errorChannel) 190 } 191 192 func (c *ApiService) getNextListDocumentPermissionResponse(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 := &ListDocumentPermissionResponse{} 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 'UpdateDocumentPermission' 211 type UpdateDocumentPermissionParams struct { 212 // Whether the identity can read the Sync Document. Default value is `false`. 213 Read *bool `json:"Read,omitempty"` 214 // Whether the identity can update the Sync Document. Default value is `false`. 215 Write *bool `json:"Write,omitempty"` 216 // Whether the identity can delete the Sync Document. Default value is `false`. 217 Manage *bool `json:"Manage,omitempty"` 218 } 219 220 func (params *UpdateDocumentPermissionParams) SetRead(Read bool) *UpdateDocumentPermissionParams { 221 params.Read = &Read 222 return params 223 } 224 func (params *UpdateDocumentPermissionParams) SetWrite(Write bool) *UpdateDocumentPermissionParams { 225 params.Write = &Write 226 return params 227 } 228 func (params *UpdateDocumentPermissionParams) SetManage(Manage bool) *UpdateDocumentPermissionParams { 229 params.Manage = &Manage 230 return params 231 } 232 233 // Update an identity's access to a specific Sync Document. 234 func (c *ApiService) UpdateDocumentPermission(ServiceSid string, DocumentSid string, Identity string, params *UpdateDocumentPermissionParams) (*SyncV1DocumentPermission, error) { 235 path := "/v1/Services/{ServiceSid}/Documents/{DocumentSid}/Permissions/{Identity}" 236 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 237 path = strings.Replace(path, "{"+"DocumentSid"+"}", DocumentSid, -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 := &SyncV1DocumentPermission{} 261 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 262 return nil, err 263 } 264 265 return ps, err 266 }