-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathservices_assets_versions.go
192 lines (156 loc) · 5.49 KB
/
services_assets_versions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* This code was generated by
* ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __
* | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/
* | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \
*
* Twilio - Serverless
* This is the public Twilio REST API.
*
* NOTE: This class is auto generated by OpenAPI Generator.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package openapi
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/twilio/twilio-go/client"
)
// Retrieve a specific Asset Version.
func (c *ApiService) FetchAssetVersion(ServiceSid string, AssetSid string, Sid string) (*ServerlessV1AssetVersion, error) {
path := "/v1/Services/{ServiceSid}/Assets/{AssetSid}/Versions/{Sid}"
path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
path = strings.Replace(path, "{"+"AssetSid"+"}", AssetSid, -1)
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
data := url.Values{}
headers := map[string]interface{}{
"Content-Type": "application/x-www-form-urlencoded",
}
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ServerlessV1AssetVersion{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}
// Optional parameters for the method 'ListAssetVersion'
type ListAssetVersionParams struct {
// How many resources to return in each list page. The default is 50, and the maximum is 1000.
PageSize *int `json:"PageSize,omitempty"`
// Max number of records to return.
Limit *int `json:"limit,omitempty"`
}
func (params *ListAssetVersionParams) SetPageSize(PageSize int) *ListAssetVersionParams {
params.PageSize = &PageSize
return params
}
func (params *ListAssetVersionParams) SetLimit(Limit int) *ListAssetVersionParams {
params.Limit = &Limit
return params
}
// Retrieve a single page of AssetVersion records from the API. Request is executed immediately.
func (c *ApiService) PageAssetVersion(ServiceSid string, AssetSid string, params *ListAssetVersionParams, pageToken, pageNumber string) (*ListAssetVersionResponse, error) {
path := "/v1/Services/{ServiceSid}/Assets/{AssetSid}/Versions"
path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
path = strings.Replace(path, "{"+"AssetSid"+"}", AssetSid, -1)
data := url.Values{}
headers := map[string]interface{}{
"Content-Type": "application/x-www-form-urlencoded",
}
if params != nil && params.PageSize != nil {
data.Set("PageSize", fmt.Sprint(*params.PageSize))
}
if pageToken != "" {
data.Set("PageToken", pageToken)
}
if pageNumber != "" {
data.Set("Page", pageNumber)
}
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ListAssetVersionResponse{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}
// Lists AssetVersion records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
func (c *ApiService) ListAssetVersion(ServiceSid string, AssetSid string, params *ListAssetVersionParams) ([]ServerlessV1AssetVersion, error) {
response, errors := c.StreamAssetVersion(ServiceSid, AssetSid, params)
records := make([]ServerlessV1AssetVersion, 0)
for record := range response {
records = append(records, record)
}
if err := <-errors; err != nil {
return nil, err
}
return records, nil
}
// Streams AssetVersion records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
func (c *ApiService) StreamAssetVersion(ServiceSid string, AssetSid string, params *ListAssetVersionParams) (chan ServerlessV1AssetVersion, chan error) {
if params == nil {
params = &ListAssetVersionParams{}
}
params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
recordChannel := make(chan ServerlessV1AssetVersion, 1)
errorChannel := make(chan error, 1)
response, err := c.PageAssetVersion(ServiceSid, AssetSid, params, "", "")
if err != nil {
errorChannel <- err
close(recordChannel)
close(errorChannel)
} else {
go c.streamAssetVersion(response, params, recordChannel, errorChannel)
}
return recordChannel, errorChannel
}
func (c *ApiService) streamAssetVersion(response *ListAssetVersionResponse, params *ListAssetVersionParams, recordChannel chan ServerlessV1AssetVersion, errorChannel chan error) {
curRecord := 1
for response != nil {
responseRecords := response.AssetVersions
for item := range responseRecords {
recordChannel <- responseRecords[item]
curRecord += 1
if params.Limit != nil && *params.Limit < curRecord {
close(recordChannel)
close(errorChannel)
return
}
}
record, err := client.GetNext(c.baseURL, response, c.getNextListAssetVersionResponse)
if err != nil {
errorChannel <- err
break
} else if record == nil {
break
}
response = record.(*ListAssetVersionResponse)
}
close(recordChannel)
close(errorChannel)
}
func (c *ApiService) getNextListAssetVersionResponse(nextPageUrl string) (interface{}, error) {
if nextPageUrl == "" {
return nil, nil
}
resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ListAssetVersionResponse{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, nil
}