Skip to main content

stellar_registry_cli/
github.rs

1pub struct Fetcher<'a> {
2    github: &'a str,
3    package: &'a str,
4    bin_ver: &'a str,
5}
6impl<'a> Fetcher<'a> {
7    pub fn new<T: AsRef<str>>(github: &'a T, package: &'a T, bin_ver: &'a T) -> Self {
8        Self {
9            github: github.as_ref(),
10            package: package.as_ref(),
11            bin_ver: bin_ver.as_ref(),
12        }
13    }
14
15    pub async fn fetch(&self) -> Result<Vec<u8>, reqwest::Error> {
16        let Fetcher {
17            github,
18            package,
19            bin_ver,
20        } = self;
21        let url = format!(
22            "https://github.com/{github}/releases/download/{package}-v{bin_ver}/{package}_v{bin_ver}.wasm"
23        );
24        let client = reqwest::Client::new();
25        let response = client
26            .get(url)
27            .header("User-Agent", "stellar-registry-cli")
28            .send()
29            .await?;
30
31        if !response.status().is_success() {
32            return Err(response.error_for_status().unwrap_err());
33        }
34        Ok(response.bytes().await?.to_vec())
35    }
36}