Trait CsvStreamResponse

Source
pub trait CsvStreamResponse {
    // Required method
    fn csv_stream<'a, 'b, T>(
        self,
        max_obj_len: usize,
        with_csv_header: bool,
        delimiter: u8,
    ) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'b
       where T: for<'de> Deserialize<'de>;
}
Available on crate feature csv only.
Expand description

Extension trait for reqwest::Response that provides streaming support for the CSV format.

Required Methods§

Source

fn csv_stream<'a, 'b, T>( self, max_obj_len: usize, with_csv_header: bool, delimiter: u8, ) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'b
where T: for<'de> Deserialize<'de>,

Streams the response as CSV, where each line is a CSV row.

The stream will Deserialize entries as type T with a maximum size of max_obj_len bytes. If max_obj_len is usize::MAX, lines will be read until a newline (\n) character is reached.

If with_csv_header is true, the stream will skip the first row (the CSV header).

The delimiter is the byte value of the delimiter character.

§Example
use futures::stream::BoxStream as _;
use reqwest_streams::CsvStreamResponse as _;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize)]
struct MyTestStructure {
    some_test_field: String
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    const MAX_OBJ_LEN: usize = 64 * 1024;

    let _stream = reqwest::get("http://localhost:8080/csv")
        .await?
        .csv_stream::<MyTestStructure>(MAX_OBJ_LEN, true, b',');

    Ok(())
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl CsvStreamResponse for Response

Source§

fn csv_stream<'a, 'b, T>( self, max_obj_len: usize, with_csv_header: bool, delimiter: u8, ) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'b
where T: for<'de> Deserialize<'de>,

Implementors§