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
//! 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")]
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! use axum::{
//! body::Body,
//! extract::State,
//! http::{header, HeaderValue},
//! routing::get,
//! Json, Router,
//! };
//! use indicatif::{ProgressBar, ProgressStyle};
//! use serde_json::json;
//! use std::sync::Arc;
//! use zenodo_rs::{ArtifactSelector, Auth, Endpoint, RecordId, ZenodoClient};
//!
//! #[derive(Clone)]
//! struct AppState {
//! base: Arc<String>,
//! }
//!
//! async fn record(State(state): State<AppState>) -> Json<serde_json::Value> {
//! Json(json!({
//! "id": 123,
//! "recid": 123,
//! "metadata": { "title": "Example" },
//! "files": [{
//! "id": "f-123",
//! "key": "artifact.bin",
//! "size": 5,
//! "links": {
//! "self": format!("{}download/123/artifact.bin", state.base),
//! }
//! }],
//! "links": {}
//! }))
//! }
//!
//! async fn artifact() -> axum::response::Response {
//! let mut response = axum::response::Response::new(Body::from("hello"));
//! response.headers_mut().insert(
//! header::CONTENT_TYPE,
//! HeaderValue::from_static("application/octet-stream"),
//! );
//! response
//! }
//!
//! let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
//! let address = listener.local_addr()?;
//! let base = Arc::new(format!("http://{address}/api/"));
//! let app = Router::new()
//! .route("/api/records/123", get(record))
//! .route("/api/download/123/artifact.bin", get(artifact))
//! .with_state(AppState { base: Arc::clone(&base) });
//! let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel::<()>();
//! let server = tokio::spawn(async move {
//! axum::serve(listener, app)
//! .with_graceful_shutdown(async {
//! let _ = shutdown_rx.await;
//! })
//! .await
//! });
//!
//! 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 temp_dir = tempfile::tempdir()?;
//! let path = temp_dir.path().join("artifact.bin");
//!
//! let resolved = client
//! .download_artifact_with_progress(
//! &ArtifactSelector::latest_file(RecordId(123), "artifact.bin"),
//! &path,
//! bar.clone(),
//! )
//! .await?;
//!
//! assert_eq!(resolved.bytes_written, 5);
//! assert_eq!(std::fs::read(&path)?, b"hello");
//! assert_eq!(bar.position(), 5);
//! let _ = shutdown_tx.send(());
//! server.await??;
//! Ok(())
//! }
//!
//! #[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.