pub struct ApiClient { /* private fields */ }
Implementationsยง
Sourceยงimpl ApiClient
impl ApiClient
Sourcepub fn new(access_token: &str) -> Result<Self>
pub fn new(access_token: &str) -> Result<Self>
Examples found in repository?
examples/basic_usage.rs (line 6)
4async fn main() -> Result<(), Box<dyn std::error::Error>> {
5 // Create API client
6 let client = ApiClient::new("your_access_token_here")?;
7
8 // Get user information
9 match client.get_me().await {
10 Ok(user) => {
11 println!("User: {} ({})", user.name, user.email.unwrap_or_default());
12 println!("User path: {}", user.user_path);
13 println!("Teams: {}", user.teams.len());
14 }
15 Err(e) => {
16 eprintln!("Error getting user info: {}", e);
17 }
18 }
19
20 // Create a new note
21 let note_options = CreateNoteOptions {
22 title: Some("Test Note from Rust".to_string()),
23 content: Some(
24 "# Hello from Rust\n\nThis note was created using the Rust HackMD API client."
25 .to_string(),
26 ),
27 read_permission: None,
28 write_permission: None,
29 comment_permission: None,
30 permalink: None,
31 };
32
33 match client.create_note(¬e_options).await {
34 Ok(note) => {
35 println!("Created note: {} (ID: {})", note.note.title, note.note.id);
36
37 // Update note content
38 let updated_content = "# Updated from Rust\n\nThis content has been updated!";
39 match client
40 .update_note_content(¬e.note.id, updated_content)
41 .await
42 {
43 Ok(_) => println!("Note content updated successfully"),
44 Err(e) => eprintln!("Error updating note: {}", e),
45 }
46 }
47 Err(e) => {
48 eprintln!("Error creating note: {}", e);
49 }
50 }
51
52 // Get notes list
53 match client.get_note_list().await {
54 Ok(notes) => {
55 println!("Found {} notes", notes.len());
56 for note in notes.iter().take(5) {
57 println!(" - {} ({})", note.title, note.id);
58 }
59 }
60 Err(e) => {
61 eprintln!("Error getting notes: {}", e);
62 }
63 }
64
65 Ok(())
66}
pub fn with_base_url(access_token: &str, base_url: &str) -> Result<Self>
Sourcepub fn with_options(
access_token: &str,
base_url: Option<&str>,
options: Option<ApiClientOptions>,
) -> Result<Self>
pub fn with_options( access_token: &str, base_url: Option<&str>, options: Option<ApiClientOptions>, ) -> Result<Self>
Examples found in repository?
examples/advanced_usage.rs (lines 16-20)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 // Example with custom configuration
7 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, // Use default API endpoint
19 Some(options),
20 )?;
21
22 println!("=== HackMD API Client Advanced Example ===\n");
23
24 // 1. Get user information
25 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 // 2. Create a new note with various permissions
44 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 // 3. Update the note content
70 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 // 4. Update note permissions
87 println!("๐ Updating note permissions...");
88 let permission_update = UpdateNoteOptions {
89 content: None,
90 read_permission: Some(NotePermissionRole::Guest), // Make it publicly readable
91 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 // 5. Get updated note
103 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 // 6. List recent notes
118 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 // 7. Get teams and team notes (if any)
135 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 // Get team notes
147 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}
Sourcepub async fn get_me(&self) -> Result<User>
pub async fn get_me(&self) -> Result<User>
Examples found in repository?
examples/basic_usage.rs (line 9)
4async fn main() -> Result<(), Box<dyn std::error::Error>> {
5 // Create API client
6 let client = ApiClient::new("your_access_token_here")?;
7
8 // Get user information
9 match client.get_me().await {
10 Ok(user) => {
11 println!("User: {} ({})", user.name, user.email.unwrap_or_default());
12 println!("User path: {}", user.user_path);
13 println!("Teams: {}", user.teams.len());
14 }
15 Err(e) => {
16 eprintln!("Error getting user info: {}", e);
17 }
18 }
19
20 // Create a new note
21 let note_options = CreateNoteOptions {
22 title: Some("Test Note from Rust".to_string()),
23 content: Some(
24 "# Hello from Rust\n\nThis note was created using the Rust HackMD API client."
25 .to_string(),
26 ),
27 read_permission: None,
28 write_permission: None,
29 comment_permission: None,
30 permalink: None,
31 };
32
33 match client.create_note(¬e_options).await {
34 Ok(note) => {
35 println!("Created note: {} (ID: {})", note.note.title, note.note.id);
36
37 // Update note content
38 let updated_content = "# Updated from Rust\n\nThis content has been updated!";
39 match client
40 .update_note_content(¬e.note.id, updated_content)
41 .await
42 {
43 Ok(_) => println!("Note content updated successfully"),
44 Err(e) => eprintln!("Error updating note: {}", e),
45 }
46 }
47 Err(e) => {
48 eprintln!("Error creating note: {}", e);
49 }
50 }
51
52 // Get notes list
53 match client.get_note_list().await {
54 Ok(notes) => {
55 println!("Found {} notes", notes.len());
56 for note in notes.iter().take(5) {
57 println!(" - {} ({})", note.title, note.id);
58 }
59 }
60 Err(e) => {
61 eprintln!("Error getting notes: {}", e);
62 }
63 }
64
65 Ok(())
66}
More examples
examples/advanced_usage.rs (line 26)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 // Example with custom configuration
7 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, // Use default API endpoint
19 Some(options),
20 )?;
21
22 println!("=== HackMD API Client Advanced Example ===\n");
23
24 // 1. Get user information
25 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 // 2. Create a new note with various permissions
44 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 // 3. Update the note content
70 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 // 4. Update note permissions
87 println!("๐ Updating note permissions...");
88 let permission_update = UpdateNoteOptions {
89 content: None,
90 read_permission: Some(NotePermissionRole::Guest), // Make it publicly readable
91 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 // 5. Get updated note
103 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 // 6. List recent notes
118 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 // 7. Get teams and team notes (if any)
135 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 // Get team notes
147 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}
pub async fn get_history(&self) -> Result<Vec<Note>>
Sourcepub async fn get_note_list(&self) -> Result<Vec<Note>>
pub async fn get_note_list(&self) -> Result<Vec<Note>>
Examples found in repository?
examples/basic_usage.rs (line 53)
4async fn main() -> Result<(), Box<dyn std::error::Error>> {
5 // Create API client
6 let client = ApiClient::new("your_access_token_here")?;
7
8 // Get user information
9 match client.get_me().await {
10 Ok(user) => {
11 println!("User: {} ({})", user.name, user.email.unwrap_or_default());
12 println!("User path: {}", user.user_path);
13 println!("Teams: {}", user.teams.len());
14 }
15 Err(e) => {
16 eprintln!("Error getting user info: {}", e);
17 }
18 }
19
20 // Create a new note
21 let note_options = CreateNoteOptions {
22 title: Some("Test Note from Rust".to_string()),
23 content: Some(
24 "# Hello from Rust\n\nThis note was created using the Rust HackMD API client."
25 .to_string(),
26 ),
27 read_permission: None,
28 write_permission: None,
29 comment_permission: None,
30 permalink: None,
31 };
32
33 match client.create_note(¬e_options).await {
34 Ok(note) => {
35 println!("Created note: {} (ID: {})", note.note.title, note.note.id);
36
37 // Update note content
38 let updated_content = "# Updated from Rust\n\nThis content has been updated!";
39 match client
40 .update_note_content(¬e.note.id, updated_content)
41 .await
42 {
43 Ok(_) => println!("Note content updated successfully"),
44 Err(e) => eprintln!("Error updating note: {}", e),
45 }
46 }
47 Err(e) => {
48 eprintln!("Error creating note: {}", e);
49 }
50 }
51
52 // Get notes list
53 match client.get_note_list().await {
54 Ok(notes) => {
55 println!("Found {} notes", notes.len());
56 for note in notes.iter().take(5) {
57 println!(" - {} ({})", note.title, note.id);
58 }
59 }
60 Err(e) => {
61 eprintln!("Error getting notes: {}", e);
62 }
63 }
64
65 Ok(())
66}
More examples
examples/advanced_usage.rs (line 119)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 // Example with custom configuration
7 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, // Use default API endpoint
19 Some(options),
20 )?;
21
22 println!("=== HackMD API Client Advanced Example ===\n");
23
24 // 1. Get user information
25 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 // 2. Create a new note with various permissions
44 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 // 3. Update the note content
70 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 // 4. Update note permissions
87 println!("๐ Updating note permissions...");
88 let permission_update = UpdateNoteOptions {
89 content: None,
90 read_permission: Some(NotePermissionRole::Guest), // Make it publicly readable
91 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 // 5. Get updated note
103 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 // 6. List recent notes
118 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 // 7. Get teams and team notes (if any)
135 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 // Get team notes
147 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}
Sourcepub async fn get_note(&self, note_id: &str) -> Result<SingleNote>
pub async fn get_note(&self, note_id: &str) -> Result<SingleNote>
Examples found in repository?
examples/advanced_usage.rs (line 104)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 // Example with custom configuration
7 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, // Use default API endpoint
19 Some(options),
20 )?;
21
22 println!("=== HackMD API Client Advanced Example ===\n");
23
24 // 1. Get user information
25 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 // 2. Create a new note with various permissions
44 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 // 3. Update the note content
70 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 // 4. Update note permissions
87 println!("๐ Updating note permissions...");
88 let permission_update = UpdateNoteOptions {
89 content: None,
90 read_permission: Some(NotePermissionRole::Guest), // Make it publicly readable
91 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 // 5. Get updated note
103 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 // 6. List recent notes
118 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 // 7. Get teams and team notes (if any)
135 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 // Get team notes
147 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}
Sourcepub async fn create_note(
&self,
payload: &CreateNoteOptions,
) -> Result<SingleNote>
pub async fn create_note( &self, payload: &CreateNoteOptions, ) -> Result<SingleNote>
Examples found in repository?
examples/basic_usage.rs (line 33)
4async fn main() -> Result<(), Box<dyn std::error::Error>> {
5 // Create API client
6 let client = ApiClient::new("your_access_token_here")?;
7
8 // Get user information
9 match client.get_me().await {
10 Ok(user) => {
11 println!("User: {} ({})", user.name, user.email.unwrap_or_default());
12 println!("User path: {}", user.user_path);
13 println!("Teams: {}", user.teams.len());
14 }
15 Err(e) => {
16 eprintln!("Error getting user info: {}", e);
17 }
18 }
19
20 // Create a new note
21 let note_options = CreateNoteOptions {
22 title: Some("Test Note from Rust".to_string()),
23 content: Some(
24 "# Hello from Rust\n\nThis note was created using the Rust HackMD API client."
25 .to_string(),
26 ),
27 read_permission: None,
28 write_permission: None,
29 comment_permission: None,
30 permalink: None,
31 };
32
33 match client.create_note(¬e_options).await {
34 Ok(note) => {
35 println!("Created note: {} (ID: {})", note.note.title, note.note.id);
36
37 // Update note content
38 let updated_content = "# Updated from Rust\n\nThis content has been updated!";
39 match client
40 .update_note_content(¬e.note.id, updated_content)
41 .await
42 {
43 Ok(_) => println!("Note content updated successfully"),
44 Err(e) => eprintln!("Error updating note: {}", e),
45 }
46 }
47 Err(e) => {
48 eprintln!("Error creating note: {}", e);
49 }
50 }
51
52 // Get notes list
53 match client.get_note_list().await {
54 Ok(notes) => {
55 println!("Found {} notes", notes.len());
56 for note in notes.iter().take(5) {
57 println!(" - {} ({})", note.title, note.id);
58 }
59 }
60 Err(e) => {
61 eprintln!("Error getting notes: {}", e);
62 }
63 }
64
65 Ok(())
66}
More examples
examples/advanced_usage.rs (line 54)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 // Example with custom configuration
7 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, // Use default API endpoint
19 Some(options),
20 )?;
21
22 println!("=== HackMD API Client Advanced Example ===\n");
23
24 // 1. Get user information
25 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 // 2. Create a new note with various permissions
44 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 // 3. Update the note content
70 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 // 4. Update note permissions
87 println!("๐ Updating note permissions...");
88 let permission_update = UpdateNoteOptions {
89 content: None,
90 read_permission: Some(NotePermissionRole::Guest), // Make it publicly readable
91 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 // 5. Get updated note
103 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 // 6. List recent notes
118 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 // 7. Get teams and team notes (if any)
135 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 // Get team notes
147 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}
Sourcepub async fn update_note_content(
&self,
note_id: &str,
content: &str,
) -> Result<SingleNote>
pub async fn update_note_content( &self, note_id: &str, content: &str, ) -> Result<SingleNote>
Examples found in repository?
examples/basic_usage.rs (line 40)
4async fn main() -> Result<(), Box<dyn std::error::Error>> {
5 // Create API client
6 let client = ApiClient::new("your_access_token_here")?;
7
8 // Get user information
9 match client.get_me().await {
10 Ok(user) => {
11 println!("User: {} ({})", user.name, user.email.unwrap_or_default());
12 println!("User path: {}", user.user_path);
13 println!("Teams: {}", user.teams.len());
14 }
15 Err(e) => {
16 eprintln!("Error getting user info: {}", e);
17 }
18 }
19
20 // Create a new note
21 let note_options = CreateNoteOptions {
22 title: Some("Test Note from Rust".to_string()),
23 content: Some(
24 "# Hello from Rust\n\nThis note was created using the Rust HackMD API client."
25 .to_string(),
26 ),
27 read_permission: None,
28 write_permission: None,
29 comment_permission: None,
30 permalink: None,
31 };
32
33 match client.create_note(¬e_options).await {
34 Ok(note) => {
35 println!("Created note: {} (ID: {})", note.note.title, note.note.id);
36
37 // Update note content
38 let updated_content = "# Updated from Rust\n\nThis content has been updated!";
39 match client
40 .update_note_content(¬e.note.id, updated_content)
41 .await
42 {
43 Ok(_) => println!("Note content updated successfully"),
44 Err(e) => eprintln!("Error updating note: {}", e),
45 }
46 }
47 Err(e) => {
48 eprintln!("Error creating note: {}", e);
49 }
50 }
51
52 // Get notes list
53 match client.get_note_list().await {
54 Ok(notes) => {
55 println!("Found {} notes", notes.len());
56 for note in notes.iter().take(5) {
57 println!(" - {} ({})", note.title, note.id);
58 }
59 }
60 Err(e) => {
61 eprintln!("Error getting notes: {}", e);
62 }
63 }
64
65 Ok(())
66}
More examples
examples/advanced_usage.rs (line 79)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 // Example with custom configuration
7 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, // Use default API endpoint
19 Some(options),
20 )?;
21
22 println!("=== HackMD API Client Advanced Example ===\n");
23
24 // 1. Get user information
25 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 // 2. Create a new note with various permissions
44 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 // 3. Update the note content
70 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 // 4. Update note permissions
87 println!("๐ Updating note permissions...");
88 let permission_update = UpdateNoteOptions {
89 content: None,
90 read_permission: Some(NotePermissionRole::Guest), // Make it publicly readable
91 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 // 5. Get updated note
103 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 // 6. List recent notes
118 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 // 7. Get teams and team notes (if any)
135 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 // Get team notes
147 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}
Sourcepub async fn update_note(
&self,
note_id: &str,
payload: &UpdateNoteOptions,
) -> Result<SingleNote>
pub async fn update_note( &self, note_id: &str, payload: &UpdateNoteOptions, ) -> Result<SingleNote>
Examples found in repository?
examples/advanced_usage.rs (line 95)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 // Example with custom configuration
7 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, // Use default API endpoint
19 Some(options),
20 )?;
21
22 println!("=== HackMD API Client Advanced Example ===\n");
23
24 // 1. Get user information
25 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 // 2. Create a new note with various permissions
44 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 // 3. Update the note content
70 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 // 4. Update note permissions
87 println!("๐ Updating note permissions...");
88 let permission_update = UpdateNoteOptions {
89 content: None,
90 read_permission: Some(NotePermissionRole::Guest), // Make it publicly readable
91 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 // 5. Get updated note
103 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 // 6. List recent notes
118 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 // 7. Get teams and team notes (if any)
135 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 // Get team notes
147 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}
pub async fn delete_note(&self, note_id: &str) -> Result<()>
Sourcepub async fn get_teams(&self) -> Result<Vec<Team>>
pub async fn get_teams(&self) -> Result<Vec<Team>>
Examples found in repository?
examples/advanced_usage.rs (line 136)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 // Example with custom configuration
7 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, // Use default API endpoint
19 Some(options),
20 )?;
21
22 println!("=== HackMD API Client Advanced Example ===\n");
23
24 // 1. Get user information
25 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 // 2. Create a new note with various permissions
44 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 // 3. Update the note content
70 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 // 4. Update note permissions
87 println!("๐ Updating note permissions...");
88 let permission_update = UpdateNoteOptions {
89 content: None,
90 read_permission: Some(NotePermissionRole::Guest), // Make it publicly readable
91 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 // 5. Get updated note
103 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 // 6. List recent notes
118 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 // 7. Get teams and team notes (if any)
135 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 // Get team notes
147 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}
Sourcepub async fn get_team_notes(&self, team_path: &str) -> Result<Vec<Note>>
pub async fn get_team_notes(&self, team_path: &str) -> Result<Vec<Note>>
Examples found in repository?
examples/advanced_usage.rs (line 147)
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6 // Example with custom configuration
7 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, // Use default API endpoint
19 Some(options),
20 )?;
21
22 println!("=== HackMD API Client Advanced Example ===\n");
23
24 // 1. Get user information
25 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 // 2. Create a new note with various permissions
44 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 // 3. Update the note content
70 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 // 4. Update note permissions
87 println!("๐ Updating note permissions...");
88 let permission_update = UpdateNoteOptions {
89 content: None,
90 read_permission: Some(NotePermissionRole::Guest), // Make it publicly readable
91 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 // 5. Get updated note
103 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 // 6. List recent notes
118 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 // 7. Get teams and team notes (if any)
135 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 // Get team notes
147 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}
pub async fn create_team_note( &self, team_path: &str, payload: &CreateNoteOptions, ) -> Result<SingleNote>
pub async fn update_team_note_content( &self, team_path: &str, note_id: &str, content: &str, ) -> Result<()>
pub async fn update_team_note( &self, team_path: &str, note_id: &str, payload: &UpdateNoteOptions, ) -> Result<()>
pub async fn delete_team_note( &self, team_path: &str, note_id: &str, ) -> Result<()>
Auto Trait Implementationsยง
impl Freeze for ApiClient
impl !RefUnwindSafe for ApiClient
impl Send for ApiClient
impl Sync for ApiClient
impl Unpin for ApiClient
impl !UnwindSafe for ApiClient
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more