use crate::inner::{create_client, uncompress};
use crate::{VkApiError, VkApiResult};
use bytes::Buf;
use cfg_if::cfg_if;
use reqwest::header::{ACCEPT, ACCEPT_ENCODING, CONTENT_ENCODING};
pub use reqwest::multipart::Form;
use reqwest::Client;
use std::io::Read;
#[derive(Clone, Debug)]
pub struct VkUploader {
client: Client,
}
impl VkUploader {
pub async fn upload<U: AsRef<str> + Send>(&self, url: U, form: Form) -> VkApiResult<String> {
cfg_if! {
if #[cfg(feature = "compression_gzip")] {
let encoding ="gzip";
} else {
let encoding ="identity";
}
}
let req = self
.client
.post(url.as_ref())
.header(ACCEPT_ENCODING, encoding)
.header(ACCEPT, "application/json")
.multipart(form);
let response = req.send().await.map_err(VkApiError::Request)?;
let headers = response.headers();
let content_encoding = headers.get(CONTENT_ENCODING).cloned();
let body = response.bytes().await.map_err(VkApiError::Request)?;
let mut body = uncompress(content_encoding, body.reader())?;
let mut response = String::new();
body.read_to_string(&mut response).map_err(VkApiError::IO)?;
Ok(response)
}
}
impl From<Client> for VkUploader {
fn from(client: Client) -> Self {
Self { client }
}
}
impl Default for VkUploader {
fn default() -> Self {
Self {
client: create_client(),
}
}
}