[][src]Trait conjure_runtime::Body

pub trait Body {
    fn content_length(&self) -> Option<u64>;
fn content_type(&self) -> HeaderValue;
fn write<'life0, 'life1, 'async_trait>(
        self: Pin<&'life0 mut Self>,
        w: Pin<&'life1 mut BodyWriter>
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        'life1: 'async_trait,
        Self: 'async_trait
;
fn reset<'life0, 'async_trait>(
        self: Pin<&'life0 mut Self>
    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
; fn full_body(&self) -> Option<Bytes> { ... } }

A request body.

This trait can be most easily implemented with the async-trait crate. While it supports both in-memory and streaming bodies, in-memory bodies can use the BytesBody type rather than creating a new implementation of this trait.

Examples

use async_trait::async_trait;
use conjure_runtime::{Body, BodyWriter};
use conjure_error::Error;
use http::HeaderValue;
use std::pin::Pin;
use tokio::io::AsyncWriteExt;

pub struct SimpleBody;

#[async_trait]
impl Body for SimpleBody {
    fn content_length(&self) -> Option<u64> {
        None
    }

    fn content_type(&self) -> HeaderValue {
        HeaderValue::from_static("application/octet-stream")
    }

    async fn write(self: Pin<&mut Self>, mut w: Pin<&mut BodyWriter>) -> Result<(), Error> {
        w.write_all(b"hello world").await.map_err(Error::internal_safe)
    }

    async fn reset(self: Pin<&mut Self>) -> bool {
        true
    }
}

Required methods

fn content_length(&self) -> Option<u64>

Returns the length of the body if known.

fn content_type(&self) -> HeaderValue

Returns the content type of the body.

fn write<'life0, 'life1, 'async_trait>(
    self: Pin<&'life0 mut Self>,
    w: Pin<&'life1 mut BodyWriter>
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>> where
    'life0: 'async_trait,
    'life1: 'async_trait,
    Self: 'async_trait, 

Writes the body data out.

fn reset<'life0, 'async_trait>(
    self: Pin<&'life0 mut Self>
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>> where
    'life0: 'async_trait,
    Self: 'async_trait, 

Resets the body to its start.

Returns true iff the body was successfully reset.

Requests with non-resettable bodies cannot be retried.

Loading content...

Provided methods

fn full_body(&self) -> Option<Bytes>

Returns the entire body if it is fully buffered.

write will only be called if this method returns None.

The default implementation returns None.

Loading content...

Implementors

impl Body for BytesBody[src]

Loading content...