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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use super::Client;
use super::Result;
use crate::proto::sui::rpc::v2::Balance;
use crate::proto::sui::rpc::v2::DynamicField;
use crate::proto::sui::rpc::v2::ListBalancesRequest;
use crate::proto::sui::rpc::v2::ListDynamicFieldsRequest;
use crate::proto::sui::rpc::v2::ListOwnedObjectsRequest;
use crate::proto::sui::rpc::v2::ListPackageVersionsRequest;
use crate::proto::sui::rpc::v2::Object;
use crate::proto::sui::rpc::v2::PackageVersion;
use futures::stream;
use futures::stream::Stream;
impl Client {
/// Creates a stream of objects based on the provided request.
///
/// The stream handles pagination automatically by using the page_token from responses
/// to fetch subsequent pages. The original request's page_token is used as the starting point.
///
/// # Arguments
/// * `request` - The initial `ListOwnedObjectsRequest` with search criteria
///
/// # Returns
/// A stream that yields `Result<Object>` instances. If any RPC call fails, the
/// tonic::Status from that request is returned.
pub fn list_owned_objects(
&self,
request: impl tonic::IntoRequest<ListOwnedObjectsRequest>,
) -> impl Stream<Item = Result<Object>> + 'static {
let client = self.clone();
let request = request.into_request();
stream::unfold(
(
Vec::new().into_iter(), // current batch of objects
true, // has_next_page
request, // request (page_token will be updated as we paginate)
client, // client for making requests
),
move |(mut iter, has_next_page, mut request, mut client)| async move {
if let Some(item) = iter.next() {
return Some((Ok(item), (iter, has_next_page, request, client)));
}
if has_next_page {
let new_request = tonic::Request::from_parts(
request.metadata().clone(),
request.extensions().clone(),
request.get_ref().clone(),
);
match client.state_client().list_owned_objects(new_request).await {
Ok(response) => {
let response = response.into_inner();
let mut iter = response.objects.into_iter();
let has_next_page = response.next_page_token.is_some();
request.get_mut().page_token = response.next_page_token;
iter.next()
.map(|item| (Ok(item), (iter, has_next_page, request, client)))
}
Err(e) => {
// Return error and terminate stream
request.get_mut().page_token = None;
Some((Err(e), (Vec::new().into_iter(), false, request, client)))
}
}
} else {
None
}
},
)
}
/// Creates a stream of `DynamicField`s based on the provided request.
///
/// The stream handles pagination automatically by using the page_token from responses
/// to fetch subsequent pages. The original request's page_token is used as the starting point.
///
/// # Arguments
/// * `request` - The initial `ListDynamicFieldsRequest` with search criteria
///
/// # Returns
/// A stream that yields `Result<DynamicField>` instances. If any RPC call fails, the
/// tonic::Status from that request is returned.
pub fn list_dynamic_fields(
&self,
request: impl tonic::IntoRequest<ListDynamicFieldsRequest>,
) -> impl Stream<Item = Result<DynamicField>> + 'static {
let client = self.clone();
let request = request.into_request();
stream::unfold(
(
Vec::new().into_iter(), // current batch of objects
true, // has_next_page
request, // request (page_token will be updated as we paginate)
client, // client for making requests
),
move |(mut iter, has_next_page, mut request, mut client)| async move {
if let Some(item) = iter.next() {
return Some((Ok(item), (iter, has_next_page, request, client)));
}
if has_next_page {
let new_request = tonic::Request::from_parts(
request.metadata().clone(),
request.extensions().clone(),
request.get_ref().clone(),
);
match client.state_client().list_dynamic_fields(new_request).await {
Ok(response) => {
let response = response.into_inner();
let mut iter = response.dynamic_fields.into_iter();
let has_next_page = response.next_page_token.is_some();
request.get_mut().page_token = response.next_page_token;
iter.next()
.map(|item| (Ok(item), (iter, has_next_page, request, client)))
}
Err(e) => {
// Return error and terminate stream
request.get_mut().page_token = None;
Some((Err(e), (Vec::new().into_iter(), false, request, client)))
}
}
} else {
None
}
},
)
}
/// Creates a stream of `Balance`s based on the provided request.
///
/// The stream handles pagination automatically by using the page_token from responses
/// to fetch subsequent pages. The original request's page_token is used as the starting point.
///
/// # Arguments
/// * `request` - The initial `ListBalancesRequest` with search criteria
///
/// # Returns
/// A stream that yields `Result<Balance>` instances. If any RPC call fails, the
/// tonic::Status from that request is returned.
pub fn list_balances(
&self,
request: impl tonic::IntoRequest<ListBalancesRequest>,
) -> impl Stream<Item = Result<Balance>> + 'static {
let client = self.clone();
let request = request.into_request();
stream::unfold(
(
Vec::new().into_iter(), // current batch of objects
true, // has_next_page
request, // request (page_token will be updated as we paginate)
client, // client for making requests
),
move |(mut iter, has_next_page, mut request, mut client)| async move {
if let Some(item) = iter.next() {
return Some((Ok(item), (iter, has_next_page, request, client)));
}
if has_next_page {
let new_request = tonic::Request::from_parts(
request.metadata().clone(),
request.extensions().clone(),
request.get_ref().clone(),
);
match client.state_client().list_balances(new_request).await {
Ok(response) => {
let response = response.into_inner();
let mut iter = response.balances.into_iter();
let has_next_page = response.next_page_token.is_some();
request.get_mut().page_token = response.next_page_token;
iter.next()
.map(|item| (Ok(item), (iter, has_next_page, request, client)))
}
Err(e) => {
// Return error and terminate stream
request.get_mut().page_token = None;
Some((Err(e), (Vec::new().into_iter(), false, request, client)))
}
}
} else {
None
}
},
)
}
/// Creates a stream of `PackageVersion`s based on the provided request.
///
/// The stream handles pagination automatically by using the page_token from responses
/// to fetch subsequent pages. The original request's page_token is used as the starting point.
///
/// # Arguments
/// * `request` - The initial `ListPackageVersionsRequest` with search criteria
///
/// # Returns
/// A stream that yields `Result<PackageVersion>` instances. If any RPC call fails, the
/// tonic::Status from that request is returned.
pub fn list_package_versions(
&self,
request: impl tonic::IntoRequest<ListPackageVersionsRequest>,
) -> impl Stream<Item = Result<PackageVersion>> + 'static {
let client = self.clone();
let request = request.into_request();
stream::unfold(
(
Vec::new().into_iter(), // current batch of objects
true, // has_next_page
request, // request (page_token will be updated as we paginate)
client, // client for making requests
),
move |(mut iter, has_next_page, mut request, mut client)| async move {
if let Some(item) = iter.next() {
return Some((Ok(item), (iter, has_next_page, request, client)));
}
if has_next_page {
let new_request = tonic::Request::from_parts(
request.metadata().clone(),
request.extensions().clone(),
request.get_ref().clone(),
);
match client
.package_client()
.list_package_versions(new_request)
.await
{
Ok(response) => {
let response = response.into_inner();
let mut iter = response.versions.into_iter();
let has_next_page = response.next_page_token.is_some();
request.get_mut().page_token = response.next_page_token;
iter.next()
.map(|item| (Ok(item), (iter, has_next_page, request, client)))
}
Err(e) => {
// Return error and terminate stream
request.get_mut().page_token = None;
Some((Err(e), (Vec::new().into_iter(), false, request, client)))
}
}
} else {
None
}
},
)
}
}