-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlist.go
More file actions
185 lines (169 loc) · 5.64 KB
/
Copy pathlist.go
File metadata and controls
185 lines (169 loc) · 5.64 KB
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
package e2b
import (
"context"
"net/url"
"sort"
"strings"
apiclient "github.com/eric642/e2b-go-sdk/internal/api"
)
// SandboxListOptions filters and paginates a sandbox list. The zero value
// lists all sandboxes the caller can see, one server-defined page at a time.
type SandboxListOptions struct {
// Metadata filters by exact key/value pairs (all must match).
Metadata map[string]string
// State filters by one or more states. Empty means all states.
State []SandboxState
// Limit caps the number of items per page. 0 uses the server default.
Limit int32
// NextToken resumes from a previous page's cursor. Usually left empty;
// the paginator manages it internally.
NextToken string
}
// SandboxPaginator iterates sandbox list pages, following the server's
// cursor (the x-next-token response header). Obtain one from Client.List.
//
// A SandboxPaginator is stateful and not safe for concurrent use.
type SandboxPaginator struct {
c *Client
opts SandboxListOptions
next string
hasNext bool
}
// HasNext reports whether another page is available. It is true before the
// first NextItems call; afterwards it reflects the x-next-token header of the
// most recent response.
func (p *SandboxPaginator) HasNext() bool { return p.hasNext }
// NextToken returns the cursor for the next page, or "" when exhausted. It can
// be persisted and replayed via SandboxListOptions.NextToken.
func (p *SandboxPaginator) NextToken() string { return p.next }
// NextItems fetches the next page of sandboxes. Call it only while HasNext
// reports true; once exhausted it returns (nil, nil). After the call HasNext
// reflects whether further pages remain.
func (p *SandboxPaginator) NextItems(ctx context.Context) ([]SandboxInfo, error) {
if !p.hasNext {
return nil, nil
}
params := &apiclient.GetV2SandboxesParams{}
if md := encodeMetadataQuery(p.opts.Metadata); md != "" {
params.Metadata = &md
}
if len(p.opts.State) > 0 {
states := make([]apiclient.SandboxState, 0, len(p.opts.State))
for _, s := range p.opts.State {
states = append(states, apiclient.SandboxState(s))
}
params.State = &states
}
if p.opts.Limit > 0 {
lim := apiclient.PaginationLimit(p.opts.Limit)
params.Limit = &lim
}
if p.next != "" {
tok := apiclient.PaginationNextToken(p.next)
params.NextToken = &tok
}
resp, err := p.c.apiCli.GetV2Sandboxes(ctx, params)
if err != nil {
return nil, mapHTTPOrCtx(err)
}
defer resp.Body.Close()
// mapHTTPErr returns nil for 2xx without reading the body, leaving it for
// ParseGetV2SandboxesResponse below. On an error status it consumes the
// body, so we must not also parse it.
if err := mapHTTPErr(resp, ""); err != nil {
return nil, err
}
parsed, err := apiclient.ParseGetV2SandboxesResponse(resp)
if err != nil {
return nil, newSandboxError("parse list response", err)
}
// Advance the cursor from the x-next-token response header.
token := ""
if parsed.HTTPResponse != nil {
token = parsed.HTTPResponse.Header.Get("x-next-token")
}
p.next = token
p.hasNext = token != ""
if parsed.JSON200 == nil {
return nil, nil
}
listed := *parsed.JSON200
out := make([]SandboxInfo, 0, len(listed))
for i := range listed {
out = append(out, *sandboxInfoFromListed(&listed[i]))
}
return out, nil
}
// List returns a paginator over the sandboxes visible to this Client. The
// request is not issued until the first NextItems call.
//
// Example:
//
// p := c.List(ctx, e2b.SandboxListOptions{State: []e2b.SandboxState{e2b.SandboxStateRunning}})
// for p.HasNext() {
// page, err := p.NextItems(ctx)
// if err != nil {
// return err
// }
// for _, s := range page {
// fmt.Println(s.SandboxID)
// }
// }
//
// SandboxInfo records returned here are lighter than Sandbox.GetInfo: see
// sandboxInfoFromListed.
func (c *Client) List(_ context.Context, opts SandboxListOptions) *SandboxPaginator {
return &SandboxPaginator{c: c, opts: opts, next: opts.NextToken, hasNext: true}
}
// ListAll drains every page and returns all matching sandboxes. Prefer List
// for large result sets where streaming pages avoids buffering everything.
func (c *Client) ListAll(ctx context.Context, opts SandboxListOptions) ([]SandboxInfo, error) {
p := c.List(ctx, opts)
var all []SandboxInfo
for p.HasNext() {
page, err := p.NextItems(ctx)
if err != nil {
return all, err
}
all = append(all, page...)
}
return all, nil
}
// List returns a paginator over sandboxes using a one-shot Client built from
// cfg.
//
// Deprecated: prefer NewClient(cfg).List, which reuses one control-plane REST
// client across calls.
func List(ctx context.Context, cfg Config, opts SandboxListOptions) (*SandboxPaginator, error) {
c, err := NewClient(cfg)
if err != nil {
return nil, err
}
return c.List(ctx, opts), nil
}
// ListAll drains every page using a one-shot Client built from cfg.
//
// Deprecated: prefer NewClient(cfg).ListAll (see List).
func ListAll(ctx context.Context, cfg Config, opts SandboxListOptions) ([]SandboxInfo, error) {
c, err := NewClient(cfg)
if err != nil {
return nil, err
}
return c.ListAll(ctx, opts)
}
// encodeMetadataQuery builds the metadata filter the API expects: each key and
// value URL-encoded, joined as key=value pairs with "&". The generated request
// builder then URL-encodes this whole string as a single query value, so the
// per-pair encoding survives the round trip (matching the Python/JS SDKs).
// Pairs are sorted for deterministic output.
func encodeMetadataQuery(md map[string]string) string {
if len(md) == 0 {
return ""
}
parts := make([]string, 0, len(md))
for k, v := range md {
parts = append(parts, url.QueryEscape(k)+"="+url.QueryEscape(v))
}
sort.Strings(parts)
return strings.Join(parts, "&")
}