-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy paths3.rs
More file actions
50 lines (43 loc) · 1.42 KB
/
s3.rs
File metadata and controls
50 lines (43 loc) · 1.42 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
use aligned_sdk::core::types::VerificationData;
#[derive(Debug)]
#[allow(dead_code)]
pub enum GetBatchProofsError {
FetchingS3Batch(String),
Deserialization(String),
EmptyBody(String),
StatusFailed((u16, String)),
ReqwestClientFailed(String),
}
// needed to make S3 bucket work
const DEFAULT_USER_AGENT: &str = "proof-aggregator/aligned-layer";
pub async fn get_aligned_batch_from_s3(
url: String,
) -> Result<Vec<VerificationData>, GetBatchProofsError> {
let client = reqwest::Client::builder()
.user_agent(DEFAULT_USER_AGENT)
.build()
.map_err(|e| GetBatchProofsError::ReqwestClientFailed(e.to_string()))?;
let response = client
.get(url)
.send()
.await
.map_err(|e| GetBatchProofsError::FetchingS3Batch(e.to_string()))?;
if !response.status().is_success() {
return Err(GetBatchProofsError::StatusFailed((
response.status().as_u16(),
response
.status()
.canonical_reason()
.unwrap_or("")
.to_string(),
)));
}
let bytes = response
.bytes()
.await
.map_err(|e| GetBatchProofsError::EmptyBody(e.to_string()))?;
let bytes: &[u8] = bytes.iter().as_slice();
let data: Vec<VerificationData> = ciborium::from_reader(bytes)
.map_err(|e| GetBatchProofsError::Deserialization(e.to_string()))?;
Ok(data)
}