bitcoind_cache/store/
http.rs

1use crate::store::{AsyncStoreResult, Store, StoreError};
2use reqwest::Body;
3
4#[derive(Clone)]
5pub struct HttpStore {
6    host: String,
7}
8
9impl HttpStore {
10    pub fn new(host: String) -> Self {
11        Self { host }
12    }
13}
14
15impl Store for HttpStore {
16    fn get_object(&self, filename: String) -> AsyncStoreResult<Option<Vec<u8>>> {
17        Box::pin(async move {
18            match reqwest::get(format!("{}/{}", self.host, filename)).await {
19                Ok(response) => {
20                    if response.status().is_success() {
21                        match response.bytes().await {
22                            Ok(content) => Ok(Some(content.to_vec())),
23                            Err(e) => Err(StoreError::Reqwest(e)),
24                        }
25                    } else {
26                        Ok(None)
27                    }
28                }
29                Err(e) => Err(StoreError::Reqwest(e)),
30            }
31        })
32    }
33
34    fn put_object<'a>(&'a self, filename: String, content: &'a [u8]) -> AsyncStoreResult<()> {
35        Box::pin(async move {
36            let body = content.to_vec();
37            let body: Body = body.into();
38            let client = reqwest::Client::new();
39            client
40                .put(format!("{}/{}", self.host, filename))
41                .body(body)
42                .send()
43                .await
44                .map(|_| ())
45                .map_err(StoreError::Reqwest)
46        })
47    }
48}