-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_sdk_demo.rs
More file actions
129 lines (114 loc) · 5.31 KB
/
Copy pathclient_sdk_demo.rs
File metadata and controls
129 lines (114 loc) · 5.31 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
// SPDX-License-Identifier: Apache-2.0
//! File System Client SDK Demo
//!
//! This example demonstrates how to use the high-level file system client SDK
//! for performing file operations on the Layer 1 file system.
//!
//! NOTE: This is a demonstration of the intended API. To actually run this,
//! you would need:
//! - A running parachain node with the DriveRegistry pallet
//! - A storage provider node for Layer 0 storage
//!
//! Run with: `cargo run --example client_sdk_demo` (when dependencies are ready)
use file_system_client::{FileSystemClient, FsClientError};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== File System Client SDK Demo ===\n");
// This is pseudocode to demonstrate the intended API
println!("This example shows the intended usage pattern of the FileSystemClient.\n");
println!("To run this in production, you would:");
println!("1. Start a parachain node: ./target/release/parachain-node --dev");
println!("2. Start a storage provider: ./target/release/storage-provider-node");
println!("3. Configure endpoints below\n");
demo_api_usage().await
}
async fn demo_api_usage() -> Result<(), FsClientError> {
println!("Step 1: Initialize the client");
println!("-------------------------------");
println!("let mut fs_client = FileSystemClient::new(");
println!(" \"ws://127.0.0.1:2222\", // Parachain RPC");
println!(" \"http://127.0.0.1:3333\" // Storage provider HTTP");
println!();
").await?;\n");
// Demonstration of expected workflow
println!("Step 2: Create a new drive");
println!("-------------------------------");
println!("let bucket_id = 1;");
println!("let drive_id = fs_client.create_drive(bucket_id, Some(\"My Drive\")).await?;");
println!("println!(\"Created drive: {{}}\", drive_id);\n");
println!("Step 3: Upload a file");
println!("-------------------------------");
println!("let file_content = b\"Hello, decentralized storage!\";");
println!("fs_client.upload_file(");
println!(" drive_id,");
println!(" \"/documents/hello.txt\",");
println!(" file_content,");
println!(" bucket_id,");
println!(").await?;");
println!("println!(\"Uploaded file: /documents/hello.txt\");\n");
println!("Step 4: Create a directory");
println!("-------------------------------");
println!("fs_client.create_directory(drive_id, \"/images\", bucket_id).await?;");
println!("println!(\"Created directory: /images\");\n");
println!("Step 5: List directory contents");
println!("-------------------------------");
println!("let entries = fs_client.list_directory(drive_id, \"/\").await?;");
println!("for entry in entries {{");
println!(" println!(\" - {{}} ({{}} bytes)\", entry.name, entry.size);");
println!("}}\n");
println!("Step 6: Download a file");
println!("-------------------------------");
println!("let downloaded = fs_client.download_file(");
println!(" drive_id,");
println!(" \"/documents/hello.txt\"");
println!(").await?;");
println!("let content = String::from_utf8(downloaded).unwrap();");
println!("println!(\"Downloaded content: {{}}\", content);\n");
println!("Step 7: Query drive root CID");
println!("-------------------------------");
println!("let root_cid = fs_client.get_root_cid(drive_id).await?;");
println!("println!(\"Current root CID: {{}}\", hex::encode(root_cid.as_bytes()));\n");
println!("\n=== Complete Workflow Example ===\n");
show_complete_example();
Ok(())
}
fn show_complete_example() {
println!("```rust");
println!("use file_system_client::{{FileSystemClient, FsClientError}};");
println!();
println!("#[tokio::main]");
println!("async fn main() -> Result<(), FsClientError> {{");
println!(" // Initialize client");
println!(" let mut client = FileSystemClient::new(");
println!(" \"ws://127.0.0.1:2222\",");
println!(" \"http://127.0.0.1:3333\",");
println!(" ).await?;");
println!();
println!(" // Create drive");
println!(" let bucket_id = 1;");
println!(" let drive_id = client.create_drive(bucket_id, Some(\"My Drive\")).await?;");
println!();
println!(" // Upload multiple files");
println!(" let files = vec![");
println!(" (\"/README.md\", include_bytes!(\"README.md\")),");
println!(" (\"/src/main.rs\", include_bytes!(\"src/main.rs\")),");
println!(" (\"/docs/guide.pdf\", include_bytes!(\"docs/guide.pdf\")),");
println!(" ];");
println!();
println!(" for (path, content) in files {{");
println!(" client.upload_file(drive_id, path, content, bucket_id).await?;");
println!(" println!(\"Uploaded: {{}}\", path);");
println!(" }}");
println!();
println!(" // List root directory");
println!(" let entries = client.list_directory(drive_id, \"/\").await?;");
println!(" println!(\"Root contains {{}} entries\", entries.len());");
println!();
println!(" // Download a file");
println!(" let readme = client.download_file(drive_id, \"/README.md\").await?;");
println!(" println!(\"README: {{}}\", String::from_utf8_lossy(&readme));");
println!();
println!(" Ok(())");
println!("}}");
println!("```");
}