advanced_usage/
advanced_usage.rs1use hackmd_api_client_rs::{ApiClient, ApiClientOptions, RetryConfig, CreateNoteOptions, UpdateNoteOptions, NotePermissionRole, CommentPermissionType};
2use std::time::Duration;
3
4#[tokio::main]
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 let options = ApiClientOptions {
8 wrap_response_errors: true,
9 timeout: Some(Duration::from_secs(30)),
10 retry_config: Some(RetryConfig {
11 max_retries: 3,
12 base_delay: Duration::from_millis(200),
13 }),
14 };
15
16 let client = ApiClient::with_options(
17 "your_access_token_here",
18 None, Some(options),
20 )?;
21
22 println!("=== HackMD API Client Advanced Example ===\n");
23
24 println!("š Getting user information...");
26 match client.get_me().await {
27 Ok(user) => {
28 println!("ā
User: {} ({})", user.name, user.email.unwrap_or("no email".to_string()));
29 println!(" Path: {}", user.user_path);
30 println!(" Teams: {}", user.teams.len());
31 for team in &user.teams {
32 println!(" - {} ({})", team.name, team.path);
33 }
34 }
35 Err(e) => {
36 eprintln!("ā Error getting user info: {}", e);
37 return Ok(());
38 }
39 }
40
41 println!();
42
43 println!("š Creating a new note...");
45 let note_options = CreateNoteOptions {
46 title: Some("Advanced Rust Example".to_string()),
47 content: Some("# Advanced HackMD Rust Client Example\n\n## Features\n\n- ā
Async/await support\n- ā
Retry mechanism with exponential backoff\n- ā
Comprehensive error handling\n- ā
Type-safe API\n\n## Code\n\n```rust\nlet client = ApiClient::new(\"token\")?;\nlet user = client.get_me().await?;\nprintln!(\"Hello, {}!\", user.name);\n```\n\nThis note was created using the Rust HackMD API client! š¦".to_string()),
48 read_permission: Some(NotePermissionRole::SignedIn),
49 write_permission: Some(NotePermissionRole::Owner),
50 comment_permission: Some(CommentPermissionType::Owners),
51 permalink: Some(format!("rust-example-{}", chrono::Utc::now().timestamp())),
52 };
53
54 let created_note = match client.create_note(¬e_options).await {
55 Ok(note) => {
56 println!("ā
Created note: {} (ID: {})", note.note.title, note.note.id);
57 println!(" Short ID: {}", note.note.short_id);
58 println!(" Publish Link: {}", note.note.publish_link);
59 note
60 }
61 Err(e) => {
62 eprintln!("ā Error creating note: {}", e);
63 return Ok(());
64 }
65 };
66
67 println!();
68
69 println!("āļø Updating note content...");
71 let updated_content = format!(
72 "{}\n\n## Update\n\nThis content was updated at {} using the Rust API client.\n\n### Statistics\n\n- Original content length: {} characters\n- Update timestamp: {}\n- API client: hackmd-api-client-rs v0.1.0",
73 created_note.content,
74 chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC"),
75 created_note.content.len(),
76 chrono::Utc::now().timestamp()
77 );
78
79 match client.update_note_content(&created_note.note.id, &updated_content).await {
80 Ok(_) => println!("ā
Note content updated successfully"),
81 Err(e) => eprintln!("ā Error updating note: {}", e),
82 }
83
84 println!();
85
86 println!("š Updating note permissions...");
88 let permission_update = UpdateNoteOptions {
89 content: None,
90 read_permission: Some(NotePermissionRole::Guest), write_permission: Some(NotePermissionRole::SignedIn),
92 permalink: None,
93 };
94
95 match client.update_note(&created_note.note.id, &permission_update).await {
96 Ok(_) => println!("ā
Note permissions updated successfully"),
97 Err(e) => eprintln!("ā Error updating permissions: {}", e),
98 }
99
100 println!();
101
102 println!("š Fetching updated note...");
104 match client.get_note(&created_note.note.id).await {
105 Ok(note) => {
106 println!("ā
Fetched note: {}", note.note.title);
107 println!(" Content length: {} characters", note.content.len());
108 println!(" Read permission: {:?}", note.note.read_permission);
109 println!(" Write permission: {:?}", note.note.write_permission);
110 println!(" Last changed: {}", note.note.last_changed_at);
111 }
112 Err(e) => eprintln!("ā Error fetching note: {}", e),
113 }
114
115 println!();
116
117 println!("š Getting note list...");
119 match client.get_note_list().await {
120 Ok(notes) => {
121 println!("ā
Found {} notes", notes.len());
122 println!(" Recent notes:");
123 for note in notes.iter().take(5) {
124 println!(" - {} ({})", note.title, note.short_id);
125 println!(" Tags: {}", note.tags.join(", "));
126 println!(" Last changed: {}", note.last_changed_at);
127 }
128 }
129 Err(e) => eprintln!("ā Error getting notes: {}", e),
130 }
131
132 println!();
133
134 println!("š„ Getting teams...");
136 match client.get_teams().await {
137 Ok(teams) => {
138 if teams.is_empty() {
139 println!("ā¹ļø No teams found");
140 } else {
141 println!("ā
Found {} teams", teams.len());
142 for team in &teams {
143 println!(" - Team: {} ({})", team.name, team.path);
144 println!(" Description: {}", team.description);
145
146 match client.get_team_notes(&team.path).await {
148 Ok(team_notes) => {
149 println!(" Notes: {} notes", team_notes.len());
150 for note in team_notes.iter().take(3) {
151 println!(" - {}", note.title);
152 }
153 }
154 Err(e) => eprintln!(" ā Error getting team notes: {}", e),
155 }
156 }
157 }
158 }
159 Err(e) => eprintln!("ā Error getting teams: {}", e),
160 }
161
162 println!("\nš Advanced example completed!");
163 println!(" Created note ID: {}", created_note.note.id);
164 println!(" View at: {}", created_note.note.publish_link);
165
166 Ok(())
167}