bothan_core/ipfs/
client.rs1use reqwest::{Client, StatusCode};
6
7use crate::ipfs::error::Error;
8
9pub struct IpfsClient {
10 url: String,
11 client: Client,
12}
13
14impl IpfsClient {
15 pub fn new(url: String, client: Client) -> Self {
16 Self { url, client }
17 }
18
19 pub async fn get_ipfs<T: AsRef<str>>(&self, hash: T) -> Result<String, Error> {
20 let url = format!("{}/ipfs/{}", self.url, hash.as_ref());
21 let resp = self
22 .client
23 .get(url)
24 .send()
25 .await
26 .map_err(|e| Error::RequestFailed(e.to_string()))?;
27
28 match resp.status() {
29 StatusCode::OK => resp
30 .text()
31 .await
32 .map_err(|e| Error::RequestFailed(e.to_string())),
33 StatusCode::UNPROCESSABLE_ENTITY => Err(Error::DoesNotExist),
34 _ => Err(Error::NonZeroStatus(resp.status().as_u16())),
35 }
36 }
37}