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
use crate::request::{execute_get, CommonOptions, RequestError};
use crate::util::{make_url, URI_SKYNET_PREFIX};
use sp_std::{prelude::Vec, str};
#[derive(Debug)]
pub enum DownloadError {
RequestError(RequestError),
Utf8Error(str::Utf8Error),
}
impl From<RequestError> for DownloadError {
fn from(err: RequestError) -> Self {
Self::RequestError(err)
}
}
impl From<str::Utf8Error> for DownloadError {
fn from(err: str::Utf8Error) -> Self {
Self::Utf8Error(err)
}
}
#[derive(Debug)]
pub struct DownloadOptions<'a> {
pub common: CommonOptions<'a>,
pub endpoint_download: &'a str,
}
impl Default for DownloadOptions<'_> {
fn default() -> Self {
Self {
common: Default::default(),
endpoint_download: "/",
}
}
}
pub fn download_bytes(
skylink: &str,
opts: Option<&DownloadOptions>,
) -> Result<Vec<u8>, DownloadError> {
let default = Default::default();
let opts = opts.unwrap_or(&default);
let skylink = if let Some(stripped) = skylink.strip_prefix(URI_SKYNET_PREFIX) {
stripped
} else {
skylink
};
let url = make_url(&[opts.common.portal_url, opts.endpoint_download, skylink]);
let response = execute_get(str::from_utf8(&url)?, &opts.common)?;
Ok(response.body().collect::<Vec<u8>>())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::str_to_bytes;
use sp_core::offchain::{testing, OffchainWorkerExt};
use sp_io::TestExternalities;
const DATA_LINK: &str = "MABdWWku6YETM2zooGCjQi26Rs4a6Hb74q26i-vMMcximQ";
const EXPECTED_JSON: &str = "{ message: \"hi there!\" }";
#[test]
fn should_download_from_data_link() {
let (offchain, state) = testing::TestOffchainExt::new();
let mut t = TestExternalities::default();
t.register_extension(OffchainWorkerExt::new(offchain));
state.write().expect_request(testing::PendingRequest {
method: "GET".into(),
uri: "https://siasky.net/MABdWWku6YETM2zooGCjQi26Rs4a6Hb74q26i-vMMcximQ".into(),
response: Some(str_to_bytes(EXPECTED_JSON)),
response_headers: vec![("Skynet-Skylink".to_owned(), DATA_LINK.to_owned())],
sent: true,
..Default::default()
});
t.execute_with(|| {
let data_returned = download_bytes(DATA_LINK, None).unwrap();
assert_eq!(data_returned, str_to_bytes(EXPECTED_JSON));
})
}
#[test]
fn should_download_with_custom_portal_url() {
const CUSTOM_PORTAL_URL: &str = "https://siasky.dev";
let (offchain, state) = testing::TestOffchainExt::new();
let mut t = TestExternalities::default();
t.register_extension(OffchainWorkerExt::new(offchain));
state.write().expect_request(testing::PendingRequest {
method: "GET".into(),
uri: "https://siasky.dev/MABdWWku6YETM2zooGCjQi26Rs4a6Hb74q26i-vMMcximQ".into(),
response: Some(str_to_bytes(EXPECTED_JSON)),
response_headers: vec![("Skynet-Skylink".to_owned(), DATA_LINK.to_owned())],
sent: true,
..Default::default()
});
t.execute_with(|| {
let data_returned = download_bytes(
DATA_LINK,
Some(&DownloadOptions {
common: CommonOptions {
portal_url: CUSTOM_PORTAL_URL,
..Default::default()
},
..Default::default()
}),
)
.unwrap();
assert_eq!(data_returned, str_to_bytes(EXPECTED_JSON));
})
}
}