use url::Url;
use crate::{
errors::OapiError,
rest::post::{Post, PostNoStream},
uploads::UploadPart,
};
#[derive(Debug, Clone, serde::Serialize)]
pub struct AddUploadPartRequest<'a> {
#[serde(skip)]
pub upload_id: &'a str,
pub data: Vec<u8>,
}
impl Post for AddUploadPartRequest<'_> {
fn is_streaming(&self) -> bool {
false
}
fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
url.path_segments_mut()
.map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
.push("uploads")
.push(self.upload_id)
.push("parts");
Ok(url.to_string())
}
}
impl PostNoStream for AddUploadPartRequest<'_> {
type Response = UploadPart;
async fn get_response_string(
&self,
client: &reqwest::Client,
url: &str,
key: &str,
) -> Result<String, OapiError> {
let part = reqwest::multipart::Part::bytes(self.data.clone());
let form = reqwest::multipart::Form::new().part("data", part);
let response = client
.post(url)
.header("Accept", "application/json")
.bearer_auth(key)
.multipart(form)
.send()
.await?;
crate::rest::response_text_checked(response).await
}
}