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