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
//! Progress reporting hooks for uploads and downloads.
//!
//! The core client stays generic over any progress sink that implements
//! [`TransferProgress`]. Under the optional `indicatif` feature,
//! [`indicatif::ProgressBar`] implements this trait directly.
//!
//! # Examples
//!
//! ```rust
//! #[cfg(feature = "indicatif")]
//! fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! use indicatif::{ProgressBar, ProgressStyle};
//! use tokio::io::{AsyncReadExt, AsyncWriteExt};
//! use tokio::net::TcpListener;
//! use zenodo_rs::{ArtifactSelector, Auth, Endpoint, RecordId, RecordSelector, ZenodoClient};
//!
//! tokio::runtime::Runtime::new()?.block_on(async {
//! let listener = TcpListener::bind("127.0.0.1:0").await?;
//! let address = listener.local_addr()?;
//! let base = format!("http://{address}/api/");
//! let record_body = format!(
//! concat!(
//! r#"{{"id":123,"recid":123,"metadata":{{"title":"Example"}},"files":["#,
//! r#"{{"id":"f-123","key":"artifact.bin","size":5,"links":{{"self":"{}download/123/artifact.bin"}}}}"#,
//! r#"],"links":{{}}}}"#
//! ),
//! base,
//! );
//!
//! let server = tokio::spawn(async move {
//! let responses = [
//! format!(
//! "HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\n\r\n{}",
//! record_body.len(),
//! record_body,
//! )
//! .into_bytes(),
//! b"HTTP/1.1 200 OK\r\ncontent-type: application/octet-stream\r\ncontent-length: 5\r\n\r\nhello".to_vec(),
//! ];
//!
//! for response in responses {
//! let (mut stream, _) = listener.accept().await?;
//! let mut buffer = [0_u8; 2048];
//! let _ = stream.read(&mut buffer).await;
//! stream.write_all(&response).await?;
//! stream.shutdown().await?;
//! }
//!
//! Ok::<(), Box<dyn std::error::Error + Send + Sync>>(())
//! });
//!
//! let client = ZenodoClient::builder(Auth::new("token"))
//! .endpoint(Endpoint::Custom(base.parse()?))
//! .build()?;
//! let bar = ProgressBar::new(0);
//! bar.set_style(ProgressStyle::with_template(
//! "{bar:20.cyan/blue} {bytes}/{total_bytes}",
//! )?);
//! let suffix = std::time::SystemTime::now()
//! .duration_since(std::time::UNIX_EPOCH)?
//! .as_nanos();
//! let path = std::env::temp_dir().join(format!(
//! "zenodo-rs-progress-{}-{suffix}.bin",
//! std::process::id(),
//! ));
//!
//! let download = client
//! .download_artifact_with_progress(
//! &ArtifactSelector::FileByKey {
//! record: RecordSelector::RecordId(RecordId(123)),
//! key: "artifact.bin".into(),
//! latest: false,
//! },
//! &path,
//! bar.clone(),
//! )
//! .await?;
//!
//! assert_eq!(download.bytes_written, 5);
//! assert_eq!(std::fs::read(&path)?, b"hello");
//! assert_eq!(bar.position(), 5);
//!
//! let _ = std::fs::remove_file(&path);
//! server.await??;
//! Ok::<(), Box<dyn std::error::Error + Send + Sync>>(())
//! })
//! }
//!
//! #[cfg(not(feature = "indicatif"))]
//! fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! Ok(())
//! }
//! ```
//!
//! Pass `bar.clone()` into the progress-aware upload and download helpers when
//! you want a real terminal progress bar during transfers.
/// Progress sink for streaming uploads and downloads.
///
/// Implement this trait when you want upload and download helpers to report
/// byte-level transfer progress into your own logging, UI, or terminal
/// progress bar implementation.