use reqwest;
use std::error::Error;
use crate::{
downloader::Downloader,
};
#[derive(Debug, Clone)]
pub struct ArPublic {
gateway: String,
http_client: reqwest::Client,
downloader: Downloader,
}
impl ArPublic {
pub fn new() -> Self {
Self {
gateway: "https://arweave.net/".to_string(),
http_client: reqwest::Client::new(),
downloader: Downloader::default(),
}
}
pub fn gateway(&self) -> String {
self.gateway.clone()
}
pub fn set_gateway(&mut self, address: &str) {
self.gateway = address.to_string();
}
pub fn http_client(&self) -> &reqwest::Client {
&self.http_client
}
pub async fn transaction(&self, id: &str)
-> Result<String, Box<dyn Error>> {
let mut api_url: String = self.gateway.clone();
api_url.push_str("tx/");
api_url.push_str(id);
let res =self.http_client().get(&api_url).send().await?;
Ok(res.text().await?)
}
pub async fn chunk(&self, offset: &str)
-> Result<String, Box<dyn Error>> {
let mut api_url: String = self.gateway.clone();
api_url.push_str("chunk/");
api_url.push_str(offset);
let res =self.http_client().get(&api_url).send().await?;
Ok(res.text().await?)
}
pub async fn transaction_offset_size(&self, id: &str)
-> Result<String, Box<dyn Error>> {
let mut api_url: String = self.gateway.clone();
api_url.push_str("tx/");
api_url.push_str(id);
api_url.push_str("/offset");
let res =self.http_client().get(&api_url).send().await?;
Ok(res.text().await?)
}
pub fn downloader(&self) -> &Downloader {
&self.downloader
}
pub async fn new_download(&mut self, id: &str)
-> Result<(), Box<dyn Error>> {
self.downloader.new_download(&self.clone(), id).await?;
Ok(())
}
pub async fn download(&mut self)
-> Result<String, Box<dyn Error>> {
Ok(self.downloader.download(&self.clone()).await?)
}
}