Struct qiniu_http_client::Part
source · pub struct Part<B> { /* private fields */ }
Expand description
Multipart 表单组件
Implementations§
source§impl<'a> Part<SyncPartBody<'a>>
impl<'a> Part<SyncPartBody<'a>>
sourcepub fn stream(value: impl Read + 'a) -> Self
pub fn stream(value: impl Read + 'a) -> Self
设置阻塞 Multipart 的请求体为输入流
Examples found in repository?
src/client/request/multipart.rs (line 294)
283 284 285 286 287 288 289 290 291 292 293 294 295
pub fn file_path<S: AsRef<OsStr> + ?Sized>(path: &S) -> IoResult<Self> {
let path = Path::new(path);
let file = File::open(path)?;
let mut metadata = PartMetadata::default().mime(mime_guess::from_path(path).first_or_octet_stream());
if let Some(file_name) = path.file_name() {
let file_name = match file_name.to_string_lossy() {
Cow::Borrowed(str) => FileName::from(str),
Cow::Owned(string) => FileName::from(string),
};
metadata = metadata.file_name(file_name);
}
Ok(SyncPart::stream(file).metadata(metadata))
}
source§impl<'a> Part<AsyncPartBody<'a>>
impl<'a> Part<AsyncPartBody<'a>>
sourcepub fn stream(value: impl AsyncRead + Send + Unpin + 'a) -> Self
pub fn stream(value: impl AsyncRead + Send + Unpin + 'a) -> Self
设置异步 Multipart 的请求体为异步输入流
Examples found in repository?
src/client/request/multipart.rs (line 431)
420 421 422 423 424 425 426 427 428 429 430 431 432
pub async fn file_path<S: AsRef<OsStr> + ?Sized>(path: &S) -> IoResult<AsyncPart<'a>> {
let path = Path::new(path);
let file = File::open(&path).await?;
let mut metadata = PartMetadata::default().mime(mime_guess::from_path(path).first_or_octet_stream());
if let Some(file_name) = path.file_name() {
let file_name = match file_name.to_string_lossy() {
Cow::Borrowed(str) => FileName::from(str),
Cow::Owned(string) => FileName::from(string),
};
metadata = metadata.file_name(file_name);
}
Ok(AsyncPart::stream(file).metadata(metadata))
}
source§impl<B> Part<B>
impl<B> Part<B>
sourcepub fn metadata(self, metadata: PartMetadata) -> Self
pub fn metadata(self, metadata: PartMetadata) -> Self
设置 Multipart 表单组件的元信息
Examples found in repository?
src/client/request/multipart.rs (line 294)
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
pub fn file_path<S: AsRef<OsStr> + ?Sized>(path: &S) -> IoResult<Self> {
let path = Path::new(path);
let file = File::open(path)?;
let mut metadata = PartMetadata::default().mime(mime_guess::from_path(path).first_or_octet_stream());
if let Some(file_name) = path.file_name() {
let file_name = match file_name.to_string_lossy() {
Cow::Borrowed(str) => FileName::from(str),
Cow::Owned(string) => FileName::from(string),
};
metadata = metadata.file_name(file_name);
}
Ok(SyncPart::stream(file).metadata(metadata))
}
}
/// 阻塞 Multipart
pub type SyncMultipart<'a> = Multipart<SyncPart<'a>>;
impl<'a> SyncMultipart<'a> {
pub(in super::super) fn into_read(mut self) -> Box<dyn Read + 'a> {
if self.fields.is_empty() {
return Box::new(Cursor::new([]));
}
let (name, part) = self.fields.pop_front().unwrap();
let chain = Box::new(self.part_stream(&name, part)) as Box<dyn Read + 'a>;
let fields = take(&mut self.fields);
Box::new(
fields
.into_iter()
.fold(chain, |readable, (name, part)| {
Box::new(readable.chain(self.part_stream(&name, part))) as Box<dyn Read + 'a>
})
.chain(Cursor::new(b"--"))
.chain(Cursor::new(self.boundary.to_owned()))
.chain(Cursor::new(b"--\r\n")),
)
}
fn part_stream(&self, name: &str, part: SyncPart<'a>) -> impl Read + 'a {
Cursor::new(b"--")
.chain(Cursor::new(self.boundary.to_owned()))
.chain(Cursor::new(b"\r\n"))
.chain(Cursor::new(encode_headers(name, &part.meta)))
.chain(Cursor::new(b"\r\n\r\n"))
.chain(part.body)
.chain(Cursor::new(b"\r\n"))
}
}
impl Read for SyncPartBody<'_> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
match &mut self.0 {
SyncPartBodyInner::Bytes(bytes) => bytes.read(buf),
SyncPartBodyInner::Stream(stream) => stream.read(buf),
}
}
}
}
pub use sync_part::{SyncMultipart, SyncPart, SyncPartBody};
#[cfg(feature = "async")]
mod async_part {
use super::*;
use async_std::{fs::File, path::Path};
use futures::io::{AsyncRead, AsyncReadExt, Cursor};
use std::{
fmt::{self, Debug},
io::Result as IoResult,
mem::take,
pin::Pin,
task::{Context, Poll},
};
type AsyncStream<'a> = Box<dyn AsyncRead + Send + Unpin + 'a>;
enum AsyncPartBodyInner<'a> {
Bytes(Cursor<Cow<'a, [u8]>>),
Stream(AsyncStream<'a>),
}
impl Debug for AsyncPartBodyInner<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bytes(bytes) => f.debug_tuple("Bytes").field(bytes).finish(),
Self::Stream(_) => f.debug_tuple("Stream").finish(),
}
}
}
/// 异步 Multipart 表单组件请求体
#[derive(Debug)]
#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
pub struct AsyncPartBody<'a>(AsyncPartBodyInner<'a>);
/// 异步 Multipart 表单组件
#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
pub type AsyncPart<'a> = Part<AsyncPartBody<'a>>;
impl<'a> AsyncPart<'a> {
/// 设置异步 Multipart 的请求体为字符串
#[inline]
#[must_use]
pub fn text(value: impl Into<Cow<'a, str>>) -> Self {
let bytes = match value.into() {
Cow::Borrowed(slice) => Cow::Borrowed(slice.as_bytes()),
Cow::Owned(string) => Cow::Owned(string.into_bytes()),
};
Self {
body: AsyncPartBody(AsyncPartBodyInner::Bytes(Cursor::new(bytes))),
meta: Default::default(),
}
}
/// 设置异步 Multipart 的请求体为内存数据
#[inline]
#[must_use]
pub fn bytes(value: impl Into<Cow<'a, [u8]>>) -> Self {
Self {
body: AsyncPartBody(AsyncPartBodyInner::Bytes(Cursor::new(value.into()))),
meta: Default::default(),
}
}
/// 设置异步 Multipart 的请求体为异步输入流
#[inline]
#[must_use]
pub fn stream(value: impl AsyncRead + Send + Unpin + 'a) -> Self {
Self {
body: AsyncPartBody(AsyncPartBodyInner::Stream(Box::new(value))),
meta: Default::default(),
}
}
/// 设置异步 Multipart 的请求体为文件
pub async fn file_path<S: AsRef<OsStr> + ?Sized>(path: &S) -> IoResult<AsyncPart<'a>> {
let path = Path::new(path);
let file = File::open(&path).await?;
let mut metadata = PartMetadata::default().mime(mime_guess::from_path(path).first_or_octet_stream());
if let Some(file_name) = path.file_name() {
let file_name = match file_name.to_string_lossy() {
Cow::Borrowed(str) => FileName::from(str),
Cow::Owned(string) => FileName::from(string),
};
metadata = metadata.file_name(file_name);
}
Ok(AsyncPart::stream(file).metadata(metadata))
}
Trait Implementations§
Auto Trait Implementations§
impl<B> RefUnwindSafe for Part<B>where
B: RefUnwindSafe,
impl<B> Send for Part<B>where
B: Send,
impl<B> Sync for Part<B>where
B: Sync,
impl<B> Unpin for Part<B>where
B: Unpin,
impl<B> UnwindSafe for Part<B>where
B: UnwindSafe,
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Pipes by value. This is generally the method you want to use. Read more
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
Borrows
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
Mutably borrows
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
Borrows
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
Mutably borrows
self
, then passes self.as_mut()
into the pipe
function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Immutable access to the
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
Mutable access to the
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
Immutable access to the
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
Mutable access to the
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
Immutable access to the
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Mutable access to the
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
Calls
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
Calls
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Calls
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
Calls
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
Calls
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
Calls
.tap_ref_mut()
only in debug builds, and is erased in release
builds.