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
use crate::client::WalrusClient;
use crate::error::WalrusError;
use crate::models::{BlobMetadata, BlobStoreResult, QuiltMetadata, QuiltStoreResponse};
use tokio::runtime::Runtime;
/// `BlockingWalrusClient` is a blocking Walrus API client.
/// It provides a synchronous interface by internally using an asynchronous `WalrusClient` and blocking the current thread.
pub struct BlockingWalrusClient {
async_client: WalrusClient,
runtime: Runtime,
}
impl BlockingWalrusClient {
/// Creates a new `BlockingWalrusClient` instance.
///
/// # Arguments
/// - `aggregator_url`: The URL string for the Walrus Aggregator service.
/// - `publisher_url`: The URL string for the Walrus Publisher service.
///
/// # Returns
/// - `Ok(BlockingWalrusClient)`: Successfully created a client instance.
/// - `Err(WalrusError)`: If the provided URL is invalid or the Tokio runtime creation fails.
pub fn new(aggregator_url: &str, publisher_url: &str) -> Result<Self, WalrusError> {
let async_client = WalrusClient::new(aggregator_url, publisher_url)?;
let runtime = Runtime::new().map_err(|e| WalrusError::Other(e.to_string()))?;
Ok(Self {
async_client,
runtime,
})
}
/// Stores a Blob to the Walrus Publisher service (blocking version).
///
/// This method blocks the current thread until the Blob storage operation is complete.
///
/// # Arguments
/// - `data`: The Blob data to store.
/// - `epochs`: Optional, the number of epochs for the Blob's lifecycle.
/// - `deletable`: Optional, indicates if the Blob is deletable.
/// - `permanent`: Optional, indicates if the Blob is permanently stored.
/// - `send_object_to`: Optional, specifies where to send the object.
///
/// # Returns
/// - `Ok(BlobStoreResult)`: Successfully stored the Blob and returned the result.
/// - `Err(WalrusError)`: If storing failed.
pub fn store_blob(
&self,
data: impl Into<reqwest::Body> + Send,
epochs: Option<u64>,
deletable: Option<bool>,
permanent: Option<bool>,
send_object_to: Option<&str>,
) -> Result<BlobStoreResult, WalrusError> {
self.runtime.block_on(self.async_client.store_blob(
data,
epochs,
deletable,
permanent,
send_object_to,
))
}
/// Reads Blob data by Blob ID from the Walrus Aggregator service (blocking version).
///
/// This method blocks the current thread until the Blob read operation is complete.
///
/// # Arguments
/// - `blob_id`: The unique identifier of the Blob.
///
/// # Returns
/// - `Ok(Vec<u8>)`: Successfully read the Blob data.
/// - `Err(WalrusError)`: If reading failed.
pub fn read_blob_by_id(&self, blob_id: &str) -> Result<Vec<u8>, WalrusError> {
self.runtime
.block_on(self.async_client.read_blob_by_id(blob_id))
}
/// Reads Blob data by object ID from the Walrus Aggregator service (blocking version).
///
/// This method blocks the current thread until the Blob read operation is complete.
///
/// # Arguments
/// - `object_id`: The unique identifier of the object.
///
/// # Returns
/// - `Ok(Vec<u8>)`: Successfully read the Blob data.
/// - `Err(WalrusError)`: If reading failed.
pub fn read_blob_by_object_id(&self, object_id: &str) -> Result<Vec<u8>, WalrusError> {
self.runtime
.block_on(self.async_client.read_blob_by_object_id(object_id))
}
/// Stores a Quilt (multiple files) to the Walrus Publisher service (blocking version).
///
/// This method blocks the current thread until the Quilt storage operation is complete.
///
/// # Arguments
/// - `files`: A vector of tuples containing the filename and file content.
/// - `metadata`: Optional, metadata for the Quilt.
/// - `epochs`: Optional, the number of epochs for the Quilt's lifecycle.
/// - `deletable`: Optional, indicates if the Quilt is deletable.
/// - `permanent`: Optional, indicates if the Quilt is permanently stored.
/// - `send_object_to`: Optional, specifies where to send the object.
///
/// # Returns
/// - `Ok(QuiltStoreResponse)`: Successfully stored the Quilt and returned the result.
/// - `Err(WalrusError)`: If storing failed.
pub fn store_quilt(
&self,
files: Vec<(&str, Vec<u8>)>,
metadata: Option<Vec<QuiltMetadata>>,
epochs: Option<u64>,
deletable: Option<bool>,
permanent: Option<bool>,
send_object_to: Option<&str>,
) -> Result<QuiltStoreResponse, WalrusError> {
self.runtime.block_on(self.async_client.store_quilt(
files,
metadata,
epochs,
deletable,
permanent,
send_object_to,
))
}
/// Reads Quilt Blob data by Quilt Patch ID from the Walrus Aggregator service (blocking version).
///
/// This method blocks the current thread until the Quilt Blob read operation is complete.
///
/// # Arguments
/// - `quilt_patch_id`: The unique identifier of the Quilt Patch.
///
/// # Returns
/// - `Ok(Vec<u8>)`: Successfully read the Quilt Blob data.
/// - `Err(WalrusError)`: If reading failed.
pub fn read_quilt_blob_by_patch_id(
&self,
quilt_patch_id: &str,
) -> Result<Vec<u8>, WalrusError> {
self.runtime.block_on(
self.async_client
.read_quilt_blob_by_patch_id(quilt_patch_id),
)
}
/// Reads Quilt Blob data by Quilt ID and identifier from the Walrus Aggregator service (blocking version).
///
/// This method blocks the current thread until the Quilt Blob read operation is complete.
///
/// # Arguments
/// - `quilt_id`: The unique identifier of the Quilt.
/// - `identifier`: The identifier of the Blob within the Quilt.
///
/// # Returns
/// - `Ok(Vec<u8>)`: Successfully read the Quilt Blob data.
/// - `Err(WalrusError)`: If reading failed.
pub fn read_quilt_blob_by_quilt_id_and_identifier(
&self,
quilt_id: &str,
identifier: &str,
) -> Result<Vec<u8>, WalrusError> {
self.runtime.block_on(
self.async_client
.read_quilt_blob_by_quilt_id_and_identifier(quilt_id, identifier),
)
}
/// Retrieves metadata for a Blob by its Blob ID from the Walrus Aggregator service (blocking version).
///
/// This method blocks the current thread until the Blob metadata retrieval operation is complete.
///
/// # Arguments
/// - `blob_id`: The unique identifier of the Blob.
///
/// # Returns
/// - `Ok(BlobMetadata)`: Successfully retrieved the Blob metadata.
/// - `Err(WalrusError)`: If retrieval failed.
pub fn get_blob_metadata(&self, blob_id: &str) -> Result<BlobMetadata, WalrusError> {
self.runtime
.block_on(self.async_client.get_blob_metadata(blob_id))
}
}