use url::Url;
use crate::{
errors::OapiError,
rest::RequestOptions,
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,
base_url: &str,
options: &RequestOptions,
) -> Result<String, OapiError> {
let part = reqwest::multipart::Part::bytes(self.data.clone());
let form = reqwest::multipart::Form::new().part("data", part);
let url = self.build_url(base_url)?;
crate::rest::post::post_multipart_json(client, url, form, options).await
}
}